Provide incr() and decr() as static methods of Cache class

This commit is contained in:
Kijin Sung 2020-12-15 02:13:38 +09:00
parent c275702fbf
commit 02b2d54275
2 changed files with 54 additions and 8 deletions

View file

@ -236,7 +236,7 @@ class Cache
* @param string $key * @param string $key
* @return bool * @return bool
*/ */
public static function delete($key) public static function delete(string $key): bool
{ {
if (self::$_driver !== null) if (self::$_driver !== null)
{ {
@ -256,7 +256,7 @@ class Cache
* @param string $key * @param string $key
* @return bool * @return bool
*/ */
public static function exists($key) public static function exists(string $key): bool
{ {
if (self::$_driver !== null) 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. * Clear a group of keys from the cache.
* *
@ -276,7 +320,7 @@ class Cache
* @param string $group_name * @param string $group_name
* @return bool * @return bool
*/ */
public static function clearGroup($group_name) public static function clearGroup(string $group_name): bool
{ {
if (self::$_driver !== null) if (self::$_driver !== null)
{ {
@ -297,7 +341,7 @@ class Cache
* *
* @return bool * @return bool
*/ */
public static function clearAll() public static function clearAll(): bool
{ {
if (self::$_driver !== null) if (self::$_driver !== null)
{ {
@ -315,7 +359,7 @@ class Cache
* @param string $group_name * @param string $group_name
* @return int * @return int
*/ */
public static function getGroupVersion($group_name) public static function getGroupVersion(string $group_name): int
{ {
if (isset(self::$_group_versions[$group_name])) if (isset(self::$_group_versions[$group_name]))
{ {
@ -340,7 +384,7 @@ class Cache
* @param string $key * @param string $key
* @return string * @return string
*/ */
public static function getRealKey($key) public static function getRealKey(string $key): string
{ {
if (preg_match('/^([^:]+):(.+)$/i', $key, $matches)) if (preg_match('/^([^:]+):(.+)$/i', $key, $matches))
{ {

View file

@ -111,9 +111,11 @@ class CacheTest extends \Codeception\TestCase\Test
$prefix = Rhymix\Framework\Cache::getPrefix(); $prefix = Rhymix\Framework\Cache::getPrefix();
$this->assertEquals(1, Rhymix\Framework\Cache::getDriverInstance()->incr($prefix . 'foo', 1)); $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(45, Rhymix\Framework\Cache::getDriverInstance()->incr($prefix . 'bar', 3));
$this->assertEquals(-1, Rhymix\Framework\Cache::getDriverInstance()->decr($prefix . 'foo', 2)); $this->assertEquals(60, Rhymix\Framework\Cache::incr('bar', 15));
$this->assertEquals(49, Rhymix\Framework\Cache::getDriverInstance()->decr($prefix . 'bar', -4)); $this->assertEquals(20, Rhymix\Framework\Cache::getDriverInstance()->incr($prefix . 'bar', -40));
} }
public function testClearAll() public function testClearAll()