mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #68 from utopia-php/feat/cache-touch
This commit is contained in:
13 files changed
+216
-1
No files matched your search
@@ -24,6 +24,13 @@ interface Adapter
|
||||
*/
|
||||
public function save(string $key, array|string $data, string $hash = ''): bool|string|array;
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $hash optional
|
||||
* @return bool
|
||||
*/
|
||||
public function touch(string $key, string $hash = ''): bool;
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return string[]
|
||||
|
||||
@@ -97,6 +97,24 @@ class Filesystem implements Adapter
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $hash optional
|
||||
* @return bool
|
||||
*/
|
||||
public function touch(string $key, string $hash = ''): bool
|
||||
{
|
||||
$file = $this->getPath($key);
|
||||
|
||||
if (! file_exists($file) || ! touch($file)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
clearstatcache(true, $file);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return string[]
|
||||
|
||||
@@ -87,6 +87,27 @@ class Hazelcast implements Adapter
|
||||
return ($this->execute(fn () => $this->memcached->set($key, json_encode($cache)))) ? $data : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $hash optional
|
||||
* @return bool
|
||||
*/
|
||||
public function touch(string $key, string $hash = ''): bool
|
||||
{
|
||||
$cache = $this->execute(fn () => $this->memcached->get($key));
|
||||
if (is_string($cache)) {
|
||||
$cache = json_decode($cache, true);
|
||||
}
|
||||
|
||||
if (! is_array($cache)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$cache['time'] = time();
|
||||
|
||||
return (bool) $this->execute(fn () => $this->memcached->set($key, json_encode($cache)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return string[]
|
||||
|
||||
@@ -89,6 +89,24 @@ class Memcached implements Adapter
|
||||
return $this->execute(fn () => $this->memcached->set($key, $cache)) ? $data : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $hash optional
|
||||
* @return bool
|
||||
*/
|
||||
public function touch(string $key, string $hash = ''): bool
|
||||
{
|
||||
/** @var array{time: int, data: string|array<int|string, mixed>}|false */
|
||||
$cache = $this->execute(fn () => $this->memcached->get($key));
|
||||
if ($cache === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$cache['time'] = time();
|
||||
|
||||
return (bool) $this->execute(fn () => $this->memcached->set($key, $cache));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return string[]
|
||||
|
||||
@@ -76,6 +76,25 @@ class Memory implements Adapter
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $hash optional
|
||||
* @return bool
|
||||
*/
|
||||
public function touch(string $key, string $hash = ''): bool
|
||||
{
|
||||
if (empty($key) || ! isset($this->store[$key])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var array{time: int, data: string|array<int|string, mixed>} $saved */
|
||||
$saved = $this->store[$key];
|
||||
$saved['time'] = time();
|
||||
$this->store[$key] = $saved;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return string[]
|
||||
|
||||
@@ -53,6 +53,16 @@ class None implements Adapter
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $hash optional
|
||||
* @return bool
|
||||
*/
|
||||
public function touch(string $key, string $hash = ''): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return string[]
|
||||
|
||||
@@ -59,6 +59,16 @@ class Pool implements Adapter
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function touch(string $key, string $hash = ''): bool
|
||||
{
|
||||
/**
|
||||
* @var bool $result
|
||||
*/
|
||||
$result = $this->delegate(__FUNCTION__, \func_get_args());
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function list(string $key): array
|
||||
{
|
||||
/**
|
||||
|
||||
@@ -152,6 +152,35 @@ class Redis implements Adapter
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $hash optional
|
||||
* @return bool
|
||||
*/
|
||||
public function touch(string $key, string $hash = ''): bool
|
||||
{
|
||||
if (empty($hash)) {
|
||||
$hash = $key;
|
||||
}
|
||||
|
||||
$redis_string = $this->execute(fn () => $this->redis->hGet($key, $hash));
|
||||
|
||||
if ($redis_string === false || $redis_string === null || ! is_string($redis_string)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
/** @var array{time: int, data: mixed} $cache */
|
||||
$cache = json_decode($redis_string, true, flags: JSON_THROW_ON_ERROR);
|
||||
$cache['time'] = time();
|
||||
$value = json_encode($cache, flags: JSON_THROW_ON_ERROR);
|
||||
} catch (Throwable $th) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->execute(fn () => $this->redis->hSet($key, $hash, $value)) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return string[]
|
||||
|
||||
@@ -103,7 +103,7 @@ class RedisCluster implements Adapter
|
||||
/** @var string|false */
|
||||
$redis_string = $this->execute(fn () => $this->redis->hGet($key, $hash));
|
||||
|
||||
if ($redis_string === false) {
|
||||
if ($redis_string === false || ! is_string($redis_string)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -151,6 +151,36 @@ class RedisCluster implements Adapter
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $hash optional
|
||||
* @return bool
|
||||
*/
|
||||
public function touch(string $key, string $hash = ''): bool
|
||||
{
|
||||
if (empty($hash)) {
|
||||
$hash = $key;
|
||||
}
|
||||
|
||||
/** @var string|false */
|
||||
$redis_string = $this->execute(fn () => $this->redis->hGet($key, $hash));
|
||||
|
||||
if ($redis_string === false || ! is_string($redis_string)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
/** @var array{time: int, data: mixed} $cache */
|
||||
$cache = json_decode($redis_string, true, flags: JSON_THROW_ON_ERROR);
|
||||
$cache['time'] = time();
|
||||
$value = json_encode($cache, flags: JSON_THROW_ON_ERROR);
|
||||
} catch (Throwable $th) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->execute(fn () => $this->redis->hSet($key, $hash, $value)) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return string[]
|
||||
|
||||
@@ -92,6 +92,16 @@ class Sharding implements Adapter
|
||||
return $this->getAdapter($key)->save($key, $data, $hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $hash optional
|
||||
* @return bool
|
||||
*/
|
||||
public function touch(string $key, string $hash = ''): bool
|
||||
{
|
||||
return $this->getAdapter($key)->touch($key, $hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return string[]
|
||||
|
||||
@@ -106,6 +106,29 @@ class Cache
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh a cache entry timestamp without replacing its data.
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $hash optional
|
||||
* @return bool
|
||||
*/
|
||||
public function touch(string $key, string $hash = ''): bool
|
||||
{
|
||||
$key = $this->caseSensitive ? $key : strtolower($key);
|
||||
$hash = $this->caseSensitive ? $hash : strtolower($hash);
|
||||
|
||||
$start = microtime(true);
|
||||
$result = $this->adapter->touch($key, $hash);
|
||||
$duration = microtime(true) - $start;
|
||||
$this->operationDuration?->record($duration, [
|
||||
'operation' => 'touch',
|
||||
'adapter' => $this->adapter->getName($key),
|
||||
]);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of keys.
|
||||
*
|
||||
|
||||
@@ -57,6 +57,21 @@ abstract class Base extends TestCase
|
||||
$this->assertEquals(false, $data);
|
||||
}
|
||||
|
||||
public function testCacheTouch(): void
|
||||
{
|
||||
$result = self::$cache->save('touch-key', 'touch data', 'touch-key');
|
||||
$this->assertEquals('touch data', $result);
|
||||
|
||||
sleep(3);
|
||||
|
||||
$this->assertEquals(false, self::$cache->load('touch-key', 2, 'touch-key'));
|
||||
$this->assertEquals(true, self::$cache->touch('touch-key', 'touch-key'));
|
||||
$this->assertEquals('touch data', self::$cache->load('touch-key', 2, 'touch-key'));
|
||||
$this->assertEquals(false, self::$cache->touch('missing-touch-key', 'missing-touch-key'));
|
||||
|
||||
self::$cache->purge('touch-key');
|
||||
}
|
||||
|
||||
public function testCaseInsensitivity(): void
|
||||
{
|
||||
// Ensure case in-sensitivity first (configured in adapter's setUp)
|
||||
|
||||
@@ -60,6 +60,11 @@ class NoneTest extends Base
|
||||
$this->assertEquals(true, $result);
|
||||
}
|
||||
|
||||
public function testCacheTouch(): void
|
||||
{
|
||||
$this->assertEquals(false, self::$cache->touch($this->key));
|
||||
}
|
||||
|
||||
public function testCaseInsensitivity(): void
|
||||
{
|
||||
// None adapter does not expect case sensitivity/insensitivy
|
||||
|
||||
Reference in new issue
Block a user