From 02b2d542750ee324364b91414bc3a261bd53b093 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Tue, 15 Dec 2020 02:13:38 +0900 Subject: [PATCH] Provide incr() and decr() as static methods of Cache class --- common/framework/cache.php | 56 ++++++++++++++++++++++++++---- tests/unit/framework/CacheTest.php | 6 ++-- 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/common/framework/cache.php b/common/framework/cache.php index c406a57ff..ec627352b 100644 --- a/common/framework/cache.php +++ b/common/framework/cache.php @@ -236,7 +236,7 @@ class Cache * @param string $key * @return bool */ - public static function delete($key) + public static function delete(string $key): bool { if (self::$_driver !== null) { @@ -256,7 +256,7 @@ class Cache * @param string $key * @return bool */ - public static function exists($key) + public static function exists(string $key): bool { if (self::$_driver !== null) { @@ -268,6 +268,50 @@ class Cache } } + /** + * Increase the value of a key by $amount. + * + * If the key does not exist, this method assumes that the current value is zero. + * This method returns the new value, or -1 on failure. + * + * @param string $key + * @param int $amount (optional) + * @return int + */ + public static function incr(string $key, int $amount = 1): int + { + if (self::$_driver !== null) + { + return self::$_driver->incr(self::getRealKey($key), $amount); + } + else + { + return -1; + } + } + + /** + * Decrease the value of a key by $amount. + * + * If the key does not exist, this method assumes that the current value is zero. + * This method returns the new value, or -1 on failure. + * + * @param string $key + * @param int $amount (optional) + * @return int + */ + public static function decr(string $key, int $amount = 1): int + { + if (self::$_driver !== null) + { + return self::$_driver->decr(self::getRealKey($key), $amount); + } + else + { + return -1; + } + } + /** * Clear a group of keys from the cache. * @@ -276,7 +320,7 @@ class Cache * @param string $group_name * @return bool */ - public static function clearGroup($group_name) + public static function clearGroup(string $group_name): bool { if (self::$_driver !== null) { @@ -297,7 +341,7 @@ class Cache * * @return bool */ - public static function clearAll() + public static function clearAll(): bool { if (self::$_driver !== null) { @@ -315,7 +359,7 @@ class Cache * @param string $group_name * @return int */ - public static function getGroupVersion($group_name) + public static function getGroupVersion(string $group_name): int { if (isset(self::$_group_versions[$group_name])) { @@ -340,7 +384,7 @@ class Cache * @param string $key * @return string */ - public static function getRealKey($key) + public static function getRealKey(string $key): string { if (preg_match('/^([^:]+):(.+)$/i', $key, $matches)) { diff --git a/tests/unit/framework/CacheTest.php b/tests/unit/framework/CacheTest.php index d8997e903..e0cdaef9c 100644 --- a/tests/unit/framework/CacheTest.php +++ b/tests/unit/framework/CacheTest.php @@ -111,9 +111,11 @@ class CacheTest extends \Codeception\TestCase\Test $prefix = Rhymix\Framework\Cache::getPrefix(); $this->assertEquals(1, Rhymix\Framework\Cache::getDriverInstance()->incr($prefix . 'foo', 1)); + $this->assertEquals(8, Rhymix\Framework\Cache::getDriverInstance()->incr($prefix . 'foo', 7)); + $this->assertEquals(-7, Rhymix\Framework\Cache::decr('foo', 15)); $this->assertEquals(45, Rhymix\Framework\Cache::getDriverInstance()->incr($prefix . 'bar', 3)); - $this->assertEquals(-1, Rhymix\Framework\Cache::getDriverInstance()->decr($prefix . 'foo', 2)); - $this->assertEquals(49, Rhymix\Framework\Cache::getDriverInstance()->decr($prefix . 'bar', -4)); + $this->assertEquals(60, Rhymix\Framework\Cache::incr('bar', 15)); + $this->assertEquals(20, Rhymix\Framework\Cache::getDriverInstance()->incr($prefix . 'bar', -40)); } public function testClearAll()