mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #125 from utopia-php/fix/cache-empty-object-fidelity
fix(cache): preserve empty nested JSON objects (DAT-2310)
This commit is contained in:
1 parent
6f8cb03a5c
commit
92e02dab63
11 files changed
+139
-6
No files matched your search
@@ -42,7 +42,7 @@ class Hazelcast implements Adapter, Retryable
|
|||||||
{
|
{
|
||||||
$cache = $this->execute(fn(): mixed => $this->memcached->get($key));
|
$cache = $this->execute(fn(): mixed => $this->memcached->get($key));
|
||||||
if (\is_string($cache)) {
|
if (\is_string($cache)) {
|
||||||
$cache = json_decode($cache, true);
|
$cache = Json::decode($cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! \is_array($cache)) {
|
if (! \is_array($cache)) {
|
||||||
@@ -82,7 +82,7 @@ class Hazelcast implements Adapter, Retryable
|
|||||||
{
|
{
|
||||||
$cache = $this->execute(fn(): mixed => $this->memcached->get($key));
|
$cache = $this->execute(fn(): mixed => $this->memcached->get($key));
|
||||||
if (\is_string($cache)) {
|
if (\is_string($cache)) {
|
||||||
$cache = json_decode($cache, true);
|
$cache = Json::decode($cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! \is_array($cache)) {
|
if (! \is_array($cache)) {
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Utopia\Cache\Adapter;
|
||||||
|
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
|
final class Json
|
||||||
|
{
|
||||||
|
public static function decode(string $value, int $flags = 0): mixed
|
||||||
|
{
|
||||||
|
if (preg_match('/\{\s*\}/', $value) === 0) {
|
||||||
|
return json_decode($value, true, flags: $flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::toAssociative(json_decode($value, flags: $flags));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function toAssociative(mixed $value): mixed
|
||||||
|
{
|
||||||
|
if ($value instanceof stdClass) {
|
||||||
|
$properties = get_object_vars($value);
|
||||||
|
if ($properties === []) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$value = $properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (\is_array($value)) {
|
||||||
|
foreach ($value as $key => $item) {
|
||||||
|
$value[$key] = self::toAssociative($item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace Utopia\Cache\Adapter\Redis;
|
namespace Utopia\Cache\Adapter\Redis;
|
||||||
|
|
||||||
use JsonException;
|
use JsonException;
|
||||||
|
use Utopia\Cache\Adapter\Json;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cache envelope codec shared by the Redis-family adapters.
|
* Cache envelope codec shared by the Redis-family adapters.
|
||||||
@@ -34,7 +35,7 @@ final class Envelope
|
|||||||
*/
|
*/
|
||||||
public static function decode(string $value, int $ttl, int $now): mixed
|
public static function decode(string $value, int $ttl, int $now): mixed
|
||||||
{
|
{
|
||||||
$cache = json_decode($value, true);
|
$cache = Json::decode($value);
|
||||||
if (! \is_array($cache) || ! isset($cache['time'], $cache['data']) || ! \is_int($cache['time'])) {
|
if (! \is_array($cache) || ! isset($cache['time'], $cache['data']) || ! \is_int($cache['time'])) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -53,7 +54,7 @@ final class Envelope
|
|||||||
public static function touch(string $value, int $newTime): string|false
|
public static function touch(string $value, int $newTime): string|false
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$cache = json_decode($value, true, flags: JSON_THROW_ON_ERROR);
|
$cache = Json::decode($value, JSON_THROW_ON_ERROR);
|
||||||
} catch (JsonException) {
|
} catch (JsonException) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ class RedisCluster implements Adapter, Retryable
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$cache = json_decode($redis_string, true);
|
$cache = Json::decode($redis_string);
|
||||||
|
|
||||||
// A purged key keeps its field until re-cached, holding a value that
|
// A purged key keeps its field until re-cached, holding a value that
|
||||||
// is not an envelope.
|
// is not an envelope.
|
||||||
@@ -123,7 +123,7 @@ class RedisCluster implements Adapter, Retryable
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
/** @var array{time: int, data: mixed} $cache */
|
/** @var array{time: int, data: mixed} $cache */
|
||||||
$cache = json_decode($redis_string, true, flags: JSON_THROW_ON_ERROR);
|
$cache = Json::decode($redis_string, JSON_THROW_ON_ERROR);
|
||||||
$cache['time'] = time();
|
$cache['time'] = time();
|
||||||
$value = json_encode($cache, flags: JSON_THROW_ON_ERROR);
|
$value = json_encode($cache, flags: JSON_THROW_ON_ERROR);
|
||||||
} catch (Throwable) {
|
} catch (Throwable) {
|
||||||
|
|||||||
@@ -9,10 +9,13 @@ use MemcachedException;
|
|||||||
use Utopia\Cache\Adapter\Hazelcast as HazelcastAdapter;
|
use Utopia\Cache\Adapter\Hazelcast as HazelcastAdapter;
|
||||||
use Utopia\Cache\Cache;
|
use Utopia\Cache\Cache;
|
||||||
use Utopia\Tests\Base;
|
use Utopia\Tests\Base;
|
||||||
|
use Utopia\Tests\Scope\EmptyObjectFidelity;
|
||||||
use Utopia\Tests\Services;
|
use Utopia\Tests\Services;
|
||||||
|
|
||||||
final class HazelcastTest extends Base
|
final class HazelcastTest extends Base
|
||||||
{
|
{
|
||||||
|
use EmptyObjectFidelity;
|
||||||
|
|
||||||
public static function setUpBeforeClass(): void
|
public static function setUpBeforeClass(): void
|
||||||
{
|
{
|
||||||
$memcached = new Memcached();
|
$memcached = new Memcached();
|
||||||
|
|||||||
@@ -181,6 +181,32 @@ final class MultiplexingTest extends TestCase
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testEmptyObjectFidelity(): void
|
||||||
|
{
|
||||||
|
$this->runCo(function (): void {
|
||||||
|
$cache = $this->makeCache();
|
||||||
|
$data = [
|
||||||
|
'empty' => new \stdClass(),
|
||||||
|
'nested' => ['empty' => new \stdClass()],
|
||||||
|
'list' => [new \stdClass(), ['x' => 1]],
|
||||||
|
'emptyArray' => [],
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->assertSame($data, $cache->save('empty-object-fidelity', $data, 'empty-object-fidelity'));
|
||||||
|
$this->assertSame(
|
||||||
|
'{"empty":{},"nested":{"empty":{}},"list":[{},{"x":1}],"emptyArray":[]}',
|
||||||
|
json_encode($cache->load('empty-object-fidelity', 60, 'empty-object-fidelity')),
|
||||||
|
);
|
||||||
|
$this->assertTrue($cache->touch('empty-object-fidelity', 'empty-object-fidelity'));
|
||||||
|
$this->assertSame(
|
||||||
|
'{"empty":{},"nested":{"empty":{}},"list":[{},{"x":1}],"emptyArray":[]}',
|
||||||
|
json_encode($cache->load('empty-object-fidelity', 60, 'empty-object-fidelity')),
|
||||||
|
);
|
||||||
|
|
||||||
|
$cache->purge('empty-object-fidelity');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public function testLargeJsonPayload(): void
|
public function testLargeJsonPayload(): void
|
||||||
{
|
{
|
||||||
$this->runCo(function (): void {
|
$this->runCo(function (): void {
|
||||||
|
|||||||
@@ -10,10 +10,13 @@ use RedisClusterException;
|
|||||||
use Utopia\Cache\Adapter\RedisCluster as RedisAdapter;
|
use Utopia\Cache\Adapter\RedisCluster as RedisAdapter;
|
||||||
use Utopia\Cache\Cache;
|
use Utopia\Cache\Cache;
|
||||||
use Utopia\Tests\Base;
|
use Utopia\Tests\Base;
|
||||||
|
use Utopia\Tests\Scope\EmptyObjectFidelity;
|
||||||
use Utopia\Tests\Services;
|
use Utopia\Tests\Services;
|
||||||
|
|
||||||
final class RedisClusterTest extends Base
|
final class RedisClusterTest extends Base
|
||||||
{
|
{
|
||||||
|
use EmptyObjectFidelity;
|
||||||
|
|
||||||
private const float TIMEOUT = 1.5;
|
private const float TIMEOUT = 1.5;
|
||||||
|
|
||||||
protected static RedisCluster $redis;
|
protected static RedisCluster $redis;
|
||||||
|
|||||||
@@ -10,10 +10,13 @@ use RedisException;
|
|||||||
use Utopia\Cache\Adapter\Redis as RedisAdapter;
|
use Utopia\Cache\Adapter\Redis as RedisAdapter;
|
||||||
use Utopia\Cache\Cache;
|
use Utopia\Cache\Cache;
|
||||||
use Utopia\Tests\Base;
|
use Utopia\Tests\Base;
|
||||||
|
use Utopia\Tests\Scope\EmptyObjectFidelity;
|
||||||
use Utopia\Tests\Services;
|
use Utopia\Tests\Services;
|
||||||
|
|
||||||
final class RedisTest extends Base
|
final class RedisTest extends Base
|
||||||
{
|
{
|
||||||
|
use EmptyObjectFidelity;
|
||||||
|
|
||||||
public static function setUpBeforeClass(): void
|
public static function setUpBeforeClass(): void
|
||||||
{
|
{
|
||||||
$redis = new Redis();
|
$redis = new Redis();
|
||||||
|
|||||||
@@ -10,10 +10,13 @@ use Utopia\Cache\Adapter\Redis as RedisAdapter;
|
|||||||
use Utopia\Cache\Adapter\Sharding;
|
use Utopia\Cache\Adapter\Sharding;
|
||||||
use Utopia\Cache\Cache;
|
use Utopia\Cache\Cache;
|
||||||
use Utopia\Tests\Base;
|
use Utopia\Tests\Base;
|
||||||
|
use Utopia\Tests\Scope\EmptyObjectFidelity;
|
||||||
use Utopia\Tests\Services;
|
use Utopia\Tests\Services;
|
||||||
|
|
||||||
final class ShardingTest extends Base
|
final class ShardingTest extends Base
|
||||||
{
|
{
|
||||||
|
use EmptyObjectFidelity;
|
||||||
|
|
||||||
public static function setUpBeforeClass(): void
|
public static function setUpBeforeClass(): void
|
||||||
{
|
{
|
||||||
$shards = [];
|
$shards = [];
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Utopia\Tests\Scope;
|
||||||
|
|
||||||
|
trait EmptyObjectFidelity
|
||||||
|
{
|
||||||
|
public function testEmptyObjectFidelity(): void
|
||||||
|
{
|
||||||
|
$key = 'empty-object-fidelity';
|
||||||
|
$data = [
|
||||||
|
'empty' => new \stdClass(),
|
||||||
|
'nested' => ['empty' => new \stdClass()],
|
||||||
|
'list' => [new \stdClass(), ['x' => 1]],
|
||||||
|
'emptyArray' => [],
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->assertSame($data, self::$cache->save($key, $data, $key));
|
||||||
|
$this->assertSame(
|
||||||
|
'{"empty":{},"nested":{"empty":{}},"list":[{},{"x":1}],"emptyArray":[]}',
|
||||||
|
json_encode(self::$cache->load($key, 60, $key)),
|
||||||
|
);
|
||||||
|
$this->assertTrue(self::$cache->touch($key, $key));
|
||||||
|
$this->assertSame(
|
||||||
|
'{"empty":{},"nested":{"empty":{}},"list":[{},{"x":1}],"emptyArray":[]}',
|
||||||
|
json_encode(self::$cache->load($key, 60, $key)),
|
||||||
|
);
|
||||||
|
|
||||||
|
self::$cache->purge($key);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -76,6 +76,29 @@ final class RedisEnvelopeTest extends TestCase
|
|||||||
$this->assertSame($data, Envelope::decode($encoded, 60, 130));
|
$this->assertSame($data, Envelope::decode($encoded, 60, 130));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDecodePreservesEmptyObjects(): void
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'empty' => new \stdClass(),
|
||||||
|
'nested' => ['empty' => new \stdClass()],
|
||||||
|
'list' => [new \stdClass(), ['x' => 1]],
|
||||||
|
'emptyArray' => [],
|
||||||
|
];
|
||||||
|
$encoded = Envelope::encode($data, 100);
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
'{"empty":{},"nested":{"empty":{}},"list":[{},{"x":1}],"emptyArray":[]}',
|
||||||
|
json_encode(Envelope::decode($encoded, 60, 130)),
|
||||||
|
);
|
||||||
|
|
||||||
|
$touched = Envelope::touch($encoded, 120);
|
||||||
|
$this->assertIsString($touched);
|
||||||
|
$this->assertSame(
|
||||||
|
'{"empty":{},"nested":{"empty":{}},"list":[{},{"x":1}],"emptyArray":[]}',
|
||||||
|
json_encode(Envelope::decode($touched, 60, 130)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function testTouchRewritesTime(): void
|
public function testTouchRewritesTime(): void
|
||||||
{
|
{
|
||||||
$encoded = Envelope::encode('value', 100);
|
$encoded = Envelope::encode('value', 100);
|
||||||
|
|||||||
Reference in new issue
Block a user