From deb5c61ad45a7d000161868bc17ed3f6b9e0ba18 Mon Sep 17 00:00:00 2001 From: Agung Sugiarto Date: Fri, 15 Jan 2021 11:05:23 +0700 Subject: [PATCH 1/6] Implement cache remember --- system/Cache/CacheInterface.php | 15 +++++++++++ system/Cache/Handlers/DummyHandler.php | 17 ++++++++++++ system/Cache/Handlers/FileHandler.php | 26 +++++++++++++++++++ system/Cache/Handlers/MemcachedHandler.php | 26 +++++++++++++++++++ system/Cache/Handlers/PredisHandler.php | 26 +++++++++++++++++++ system/Cache/Handlers/RedisHandler.php | 26 +++++++++++++++++++ system/Cache/Handlers/WincacheHandler.php | 26 +++++++++++++++++++ system/Test/Mock/MockCache.php | 26 +++++++++++++++++++ .../Cache/Handlers/DummyHandlerTest.php | 9 +++++++ .../system/Cache/Handlers/FileHandlerTest.php | 13 ++++++++++ .../Cache/Handlers/MemcachedHandlerTest.php | 13 ++++++++++ .../Cache/Handlers/PredisHandlerTest.php | 13 ++++++++++ .../Cache/Handlers/RedisHandlerTest.php | 13 ++++++++++ 13 files changed, 249 insertions(+) diff --git a/system/Cache/CacheInterface.php b/system/Cache/CacheInterface.php index a632b8de7687..5dacab228791 100644 --- a/system/Cache/CacheInterface.php +++ b/system/Cache/CacheInterface.php @@ -11,6 +11,8 @@ namespace CodeIgniter\Cache; +use Closure; + /** * Cache interface */ @@ -34,6 +36,19 @@ public function get(string $key); //-------------------------------------------------------------------- + /** + * Get an item from the cache, or execute the given Closure and store the result. + * + * @param string $key Cache item name + * @param integer $ttl Time to live + * @param Closure $callback Callback return value + * + * @return mixed + */ + public function remember(string $key, int $ttl, Closure $callback); + + //-------------------------------------------------------------------- + /** * Saves an item to the cache store. * diff --git a/system/Cache/Handlers/DummyHandler.php b/system/Cache/Handlers/DummyHandler.php index b37d6b376479..d17af176539e 100644 --- a/system/Cache/Handlers/DummyHandler.php +++ b/system/Cache/Handlers/DummyHandler.php @@ -12,6 +12,7 @@ namespace CodeIgniter\Cache\Handlers; use CodeIgniter\Cache\CacheInterface; +use Closure; /** * Dummy cache handler @@ -42,6 +43,22 @@ public function get(string $key) //-------------------------------------------------------------------- + /** + * Get an item from the cache, or execute the given Closure and store the result. + * + * @param string $key Cache item name + * @param integer $ttl Time to live + * @param Closure $callback Callback return value + * + * @return mixed + */ + public function remember(string $key, int $ttl, Closure $callback) + { + return null; + } + + //-------------------------------------------------------------------- + /** * Saves an item to the cache store. * diff --git a/system/Cache/Handlers/FileHandler.php b/system/Cache/Handlers/FileHandler.php index e99874e9d173..4876a85e8a62 100644 --- a/system/Cache/Handlers/FileHandler.php +++ b/system/Cache/Handlers/FileHandler.php @@ -14,6 +14,7 @@ use CodeIgniter\Cache\CacheInterface; use CodeIgniter\Cache\Exceptions\CacheException; use Config\Cache; +use Closure; /** * File system cache handler @@ -84,6 +85,31 @@ public function get(string $key) //-------------------------------------------------------------------- + /** + * Get an item from the cache, or execute the given Closure and store the result. + * + * @param string $key Cache item name + * @param integer $ttl Time to live + * @param Closure $callback Callback return value + * + * @return mixed + */ + public function remember(string $key, int $ttl, Closure $callback) + { + $value = $this->get($key); + + if (! is_null($value)) + { + return $value; + } + + $this->save($key, $value = $callback(), $ttl); + + return $value; + } + + //-------------------------------------------------------------------- + /** * Saves an item to the cache store. * diff --git a/system/Cache/Handlers/MemcachedHandler.php b/system/Cache/Handlers/MemcachedHandler.php index 84d6539bcc81..ec1a603a99a7 100644 --- a/system/Cache/Handlers/MemcachedHandler.php +++ b/system/Cache/Handlers/MemcachedHandler.php @@ -14,6 +14,7 @@ use CodeIgniter\Cache\CacheInterface; use CodeIgniter\Exceptions\CriticalError; use Config\Cache; +use Closure; use Exception; use Memcache; use Memcached; @@ -196,6 +197,31 @@ public function get(string $key) //-------------------------------------------------------------------- + /** + * Get an item from the cache, or execute the given Closure and store the result. + * + * @param string $key Cache item name + * @param integer $ttl Time to live + * @param Closure $callback Callback return value + * + * @return mixed + */ + public function remember(string $key, int $ttl, Closure $callback) + { + $value = $this->get($key); + + if (! is_null($value)) + { + return $value; + } + + $this->save($key, $value = $callback(), $ttl); + + return $value; + } + + //-------------------------------------------------------------------- + /** * Saves an item to the cache store. * diff --git a/system/Cache/Handlers/PredisHandler.php b/system/Cache/Handlers/PredisHandler.php index 81d982dd5f1b..615cf8977071 100644 --- a/system/Cache/Handlers/PredisHandler.php +++ b/system/Cache/Handlers/PredisHandler.php @@ -14,6 +14,7 @@ use CodeIgniter\Cache\CacheInterface; use CodeIgniter\Exceptions\CriticalError; use Config\Cache; +use Closure; use Exception; use Predis\Client; @@ -131,6 +132,31 @@ public function get(string $key) //-------------------------------------------------------------------- + /** + * Get an item from the cache, or execute the given Closure and store the result. + * + * @param string $key Cache item name + * @param integer $ttl Time to live + * @param Closure $callback Callback return value + * + * @return mixed + */ + public function remember(string $key, int $ttl, Closure $callback) + { + $value = $this->get($key); + + if (! is_null($value)) + { + return $value; + } + + $this->save($key, $value = $callback(), $ttl); + + return $value; + } + + //-------------------------------------------------------------------- + /** * Saves an item to the cache store. * diff --git a/system/Cache/Handlers/RedisHandler.php b/system/Cache/Handlers/RedisHandler.php index 5af9f1f07fbf..985b58cc7d52 100644 --- a/system/Cache/Handlers/RedisHandler.php +++ b/system/Cache/Handlers/RedisHandler.php @@ -14,6 +14,7 @@ use CodeIgniter\Cache\CacheInterface; use CodeIgniter\Exceptions\CriticalError; use Config\Cache; +use Closure; use Redis; use RedisException; @@ -163,6 +164,31 @@ public function get(string $key) //-------------------------------------------------------------------- + /** + * Get an item from the cache, or execute the given Closure and store the result. + * + * @param string $key Cache item name + * @param integer $ttl Time to live + * @param Closure $callback Callback return value + * + * @return mixed + */ + public function remember(string $key, int $ttl, Closure $callback) + { + $value = $this->get($key); + + if (! is_null($value)) + { + return $value; + } + + $this->save($key, $value = $callback(), $ttl); + + return $value; + } + + //-------------------------------------------------------------------- + /** * Saves an item to the cache store. * diff --git a/system/Cache/Handlers/WincacheHandler.php b/system/Cache/Handlers/WincacheHandler.php index a37aba2556be..d6dd61699766 100644 --- a/system/Cache/Handlers/WincacheHandler.php +++ b/system/Cache/Handlers/WincacheHandler.php @@ -13,6 +13,7 @@ use CodeIgniter\Cache\CacheInterface; use Config\Cache; +use Closure; /** * Cache handler for WinCache from Microsoft & IIS. @@ -76,6 +77,31 @@ public function get(string $key) //-------------------------------------------------------------------- + /** + * Get an item from the cache, or execute the given Closure and store the result. + * + * @param string $key Cache item name + * @param integer $ttl Time to live + * @param Closure $callback Callback return value + * + * @return mixed + */ + public function remember(string $key, int $ttl, Closure $callback) + { + $value = $this->get($key); + + if (! is_null($value)) + { + return $value; + } + + $this->save($key, $value = $callback(), $ttl); + + return $value; + } + + //-------------------------------------------------------------------- + /** * Saves an item to the cache store. * diff --git a/system/Test/Mock/MockCache.php b/system/Test/Mock/MockCache.php index 093c98469787..67356cf50f62 100644 --- a/system/Test/Mock/MockCache.php +++ b/system/Test/Mock/MockCache.php @@ -12,6 +12,7 @@ namespace CodeIgniter\Test\Mock; use CodeIgniter\Cache\CacheInterface; +use Closure; class MockCache implements CacheInterface { @@ -59,6 +60,31 @@ public function get(string $key) //-------------------------------------------------------------------- + /** + * Get an item from the cache, or execute the given Closure and store the result. + * + * @param string $key Cache item name + * @param integer $ttl Time to live + * @param Closure $callback Callback return value + * + * @return mixed + */ + public function remember(string $key, int $ttl, Closure $callback) + { + $value = $this->get($key); + + if (! is_null($value)) + { + return $value; + } + + $this->save($key, $value = $callback(), $ttl); + + return $value; + } + + //-------------------------------------------------------------------- + /** * Saves an item to the cache store. * diff --git a/tests/system/Cache/Handlers/DummyHandlerTest.php b/tests/system/Cache/Handlers/DummyHandlerTest.php index 32d1188a80d8..837006ad19d2 100644 --- a/tests/system/Cache/Handlers/DummyHandlerTest.php +++ b/tests/system/Cache/Handlers/DummyHandlerTest.php @@ -20,6 +20,15 @@ public function testGet() $this->assertNull($this->dummyHandler->get('key')); } + public function testRemember() + { + $dummyHandler = $this->dummyHandler->remember('key', 2, function () { + return 'value'; + }); + + $this->assertNull($dummyHandler); + } + public function testSave() { $this->assertTrue($this->dummyHandler->save('key', 'value')); diff --git a/tests/system/Cache/Handlers/FileHandlerTest.php b/tests/system/Cache/Handlers/FileHandlerTest.php index bb77935abf10..0fb85453f2eb 100644 --- a/tests/system/Cache/Handlers/FileHandlerTest.php +++ b/tests/system/Cache/Handlers/FileHandlerTest.php @@ -94,6 +94,19 @@ public function testGet() $this->assertNull($this->fileHandler->get(self::$key1)); } + public function testRemember() + { + $this->fileHandler->remember(self::$key1, 2, function () { + return 'value'; + }); + + $this->assertSame('value', $this->fileHandler->get(self::$key1)); + $this->assertNull($this->fileHandler->get(self::$dummy)); + + \CodeIgniter\CLI\CLI::wait(3); + $this->assertNull($this->fileHandler->get(self::$key1)); + } + public function testSave() { $this->assertTrue($this->fileHandler->save(self::$key1, 'value')); diff --git a/tests/system/Cache/Handlers/MemcachedHandlerTest.php b/tests/system/Cache/Handlers/MemcachedHandlerTest.php index 063da1b0ac96..f77de3997c4c 100644 --- a/tests/system/Cache/Handlers/MemcachedHandlerTest.php +++ b/tests/system/Cache/Handlers/MemcachedHandlerTest.php @@ -57,6 +57,19 @@ public function testGet() $this->assertNull($this->memcachedHandler->get(self::$key1)); } + public function testRemember() + { + $this->memcachedHandler->remember(self::$key1, 2, function () { + return 'value'; + }); + + $this->assertSame('value', $this->memcachedHandler->get(self::$key1)); + $this->assertNull($this->memcachedHandler->get(self::$dummy)); + + \CodeIgniter\CLI\CLI::wait(3); + $this->assertNull($this->memcachedHandler->get(self::$key1)); + } + public function testSave() { $this->assertTrue($this->memcachedHandler->save(self::$key1, 'value')); diff --git a/tests/system/Cache/Handlers/PredisHandlerTest.php b/tests/system/Cache/Handlers/PredisHandlerTest.php index 83a2ab425907..2386c46f3f4a 100644 --- a/tests/system/Cache/Handlers/PredisHandlerTest.php +++ b/tests/system/Cache/Handlers/PredisHandlerTest.php @@ -65,6 +65,19 @@ public function testGet() $this->assertNull($this->PredisHandler->get(self::$key1)); } + public function testRemember() + { + $this->PredisHandler->remember(self::$key1, 2, function () { + return 'value'; + }); + + $this->assertSame('value', $this->PredisHandler->get(self::$key1)); + $this->assertNull($this->PredisHandler->get(self::$dummy)); + + \CodeIgniter\CLI\CLI::wait(3); + $this->assertNull($this->PredisHandler->get(self::$key1)); + } + public function testSave() { $this->assertTrue($this->PredisHandler->save(self::$key1, 'value')); diff --git a/tests/system/Cache/Handlers/RedisHandlerTest.php b/tests/system/Cache/Handlers/RedisHandlerTest.php index 603f2ec2eab6..c5961621dc34 100644 --- a/tests/system/Cache/Handlers/RedisHandlerTest.php +++ b/tests/system/Cache/Handlers/RedisHandlerTest.php @@ -101,6 +101,19 @@ public function testGet() $this->assertNull($this->redisHandler->get(self::$key1)); } + public function testRemember() + { + $this->fileHandler->remember(self::$key1, 2, function () { + return 'value'; + }); + + $this->assertSame('value', $this->fileHandler->get(self::$key1)); + $this->assertNull($this->fileHandler->get(self::$dummy)); + + \CodeIgniter\CLI\CLI::wait(3); + $this->assertNull($this->fileHandler->get(self::$key1)); + } + public function testSave() { $this->assertTrue($this->redisHandler->save(self::$key1, 'value')); From e8d39e012f7abb9b860300ccbff05ec4b6db11b6 Mon Sep 17 00:00:00 2001 From: Agung Sugiarto Date: Fri, 15 Jan 2021 18:16:04 +0700 Subject: [PATCH 2/6] Remove method remember from interface --- system/Cache/CacheInterface.php | 13 ------------- tests/system/Cache/Handlers/RedisHandlerTest.php | 8 ++++---- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/system/Cache/CacheInterface.php b/system/Cache/CacheInterface.php index 5dacab228791..a3fd03fbc55b 100644 --- a/system/Cache/CacheInterface.php +++ b/system/Cache/CacheInterface.php @@ -36,19 +36,6 @@ public function get(string $key); //-------------------------------------------------------------------- - /** - * Get an item from the cache, or execute the given Closure and store the result. - * - * @param string $key Cache item name - * @param integer $ttl Time to live - * @param Closure $callback Callback return value - * - * @return mixed - */ - public function remember(string $key, int $ttl, Closure $callback); - - //-------------------------------------------------------------------- - /** * Saves an item to the cache store. * diff --git a/tests/system/Cache/Handlers/RedisHandlerTest.php b/tests/system/Cache/Handlers/RedisHandlerTest.php index c5961621dc34..da4676f069ec 100644 --- a/tests/system/Cache/Handlers/RedisHandlerTest.php +++ b/tests/system/Cache/Handlers/RedisHandlerTest.php @@ -103,15 +103,15 @@ public function testGet() public function testRemember() { - $this->fileHandler->remember(self::$key1, 2, function () { + $this->redisHandler->remember(self::$key1, 2, function () { return 'value'; }); - $this->assertSame('value', $this->fileHandler->get(self::$key1)); - $this->assertNull($this->fileHandler->get(self::$dummy)); + $this->assertSame('value', $this->redisHandler->get(self::$key1)); + $this->assertNull($this->redisHandler->get(self::$dummy)); \CodeIgniter\CLI\CLI::wait(3); - $this->assertNull($this->fileHandler->get(self::$key1)); + $this->assertNull($this->redisHandler->get(self::$key1)); } public function testSave() From aa7cddfb1996a212f6e7cd990c84d9b778592bbe Mon Sep 17 00:00:00 2001 From: Agung Sugiarto Date: Sat, 16 Jan 2021 09:15:51 +0700 Subject: [PATCH 3/6] Cache move to BaseHandler --- system/Cache/CacheInterface.php | 2 - system/Cache/Handlers/BaseHandler.php | 44 ++++++++++++++++++++++ system/Cache/Handlers/DummyHandler.php | 3 +- system/Cache/Handlers/FileHandler.php | 29 +------------- system/Cache/Handlers/MemcachedHandler.php | 29 +------------- system/Cache/Handlers/PredisHandler.php | 29 +------------- system/Cache/Handlers/RedisHandler.php | 29 +------------- system/Cache/Handlers/WincacheHandler.php | 29 +------------- 8 files changed, 50 insertions(+), 144 deletions(-) create mode 100644 system/Cache/Handlers/BaseHandler.php diff --git a/system/Cache/CacheInterface.php b/system/Cache/CacheInterface.php index a3fd03fbc55b..a632b8de7687 100644 --- a/system/Cache/CacheInterface.php +++ b/system/Cache/CacheInterface.php @@ -11,8 +11,6 @@ namespace CodeIgniter\Cache; -use Closure; - /** * Cache interface */ diff --git a/system/Cache/Handlers/BaseHandler.php b/system/Cache/Handlers/BaseHandler.php new file mode 100644 index 000000000000..10c2b3e82fe0 --- /dev/null +++ b/system/Cache/Handlers/BaseHandler.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace CodeIgniter\Cache\Handlers; + +use Closure; +use CodeIgniter\Cache\CacheInterface; + +/** + * Base class for cache handling + */ +abstract class BaseHandler implements CacheInterface +{ + /** + * Get an item from the cache, or execute the given Closure and store the result. + * + * @param string $key Cache item name + * @param integer $ttl Time to live + * @param Closure $callback Callback return value + * + * @return mixed + */ + public function remember(string $key, int $ttl, Closure $callback) + { + $value = $this->get($key); + + if (! is_null($value)) + { + return $value; + } + + $this->save($key, $value = $callback(), $ttl); + + return $value; + } +} diff --git a/system/Cache/Handlers/DummyHandler.php b/system/Cache/Handlers/DummyHandler.php index d17af176539e..b888a98cec0e 100644 --- a/system/Cache/Handlers/DummyHandler.php +++ b/system/Cache/Handlers/DummyHandler.php @@ -11,13 +11,12 @@ namespace CodeIgniter\Cache\Handlers; -use CodeIgniter\Cache\CacheInterface; use Closure; /** * Dummy cache handler */ -class DummyHandler implements CacheInterface +class DummyHandler extends BaseHandler { /** * Takes care of any handler-specific setup that must be done. diff --git a/system/Cache/Handlers/FileHandler.php b/system/Cache/Handlers/FileHandler.php index 4876a85e8a62..9909b354014f 100644 --- a/system/Cache/Handlers/FileHandler.php +++ b/system/Cache/Handlers/FileHandler.php @@ -11,15 +11,13 @@ namespace CodeIgniter\Cache\Handlers; -use CodeIgniter\Cache\CacheInterface; use CodeIgniter\Cache\Exceptions\CacheException; use Config\Cache; -use Closure; /** * File system cache handler */ -class FileHandler implements CacheInterface +class FileHandler extends BaseHandler { /** * Prefixed to all cache names. @@ -85,31 +83,6 @@ public function get(string $key) //-------------------------------------------------------------------- - /** - * Get an item from the cache, or execute the given Closure and store the result. - * - * @param string $key Cache item name - * @param integer $ttl Time to live - * @param Closure $callback Callback return value - * - * @return mixed - */ - public function remember(string $key, int $ttl, Closure $callback) - { - $value = $this->get($key); - - if (! is_null($value)) - { - return $value; - } - - $this->save($key, $value = $callback(), $ttl); - - return $value; - } - - //-------------------------------------------------------------------- - /** * Saves an item to the cache store. * diff --git a/system/Cache/Handlers/MemcachedHandler.php b/system/Cache/Handlers/MemcachedHandler.php index ec1a603a99a7..fc27fba7359c 100644 --- a/system/Cache/Handlers/MemcachedHandler.php +++ b/system/Cache/Handlers/MemcachedHandler.php @@ -11,10 +11,8 @@ namespace CodeIgniter\Cache\Handlers; -use CodeIgniter\Cache\CacheInterface; use CodeIgniter\Exceptions\CriticalError; use Config\Cache; -use Closure; use Exception; use Memcache; use Memcached; @@ -22,7 +20,7 @@ /** * Mamcached cache handler */ -class MemcachedHandler implements CacheInterface +class MemcachedHandler extends BaseHandler { /** * Prefixed to all cache names. @@ -197,31 +195,6 @@ public function get(string $key) //-------------------------------------------------------------------- - /** - * Get an item from the cache, or execute the given Closure and store the result. - * - * @param string $key Cache item name - * @param integer $ttl Time to live - * @param Closure $callback Callback return value - * - * @return mixed - */ - public function remember(string $key, int $ttl, Closure $callback) - { - $value = $this->get($key); - - if (! is_null($value)) - { - return $value; - } - - $this->save($key, $value = $callback(), $ttl); - - return $value; - } - - //-------------------------------------------------------------------- - /** * Saves an item to the cache store. * diff --git a/system/Cache/Handlers/PredisHandler.php b/system/Cache/Handlers/PredisHandler.php index 615cf8977071..698d802c17f8 100644 --- a/system/Cache/Handlers/PredisHandler.php +++ b/system/Cache/Handlers/PredisHandler.php @@ -11,17 +11,15 @@ namespace CodeIgniter\Cache\Handlers; -use CodeIgniter\Cache\CacheInterface; use CodeIgniter\Exceptions\CriticalError; use Config\Cache; -use Closure; use Exception; use Predis\Client; /** * Predis cache handler */ -class PredisHandler implements CacheInterface +class PredisHandler extends BaseHandler { /** * Prefixed to all cache names. @@ -132,31 +130,6 @@ public function get(string $key) //-------------------------------------------------------------------- - /** - * Get an item from the cache, or execute the given Closure and store the result. - * - * @param string $key Cache item name - * @param integer $ttl Time to live - * @param Closure $callback Callback return value - * - * @return mixed - */ - public function remember(string $key, int $ttl, Closure $callback) - { - $value = $this->get($key); - - if (! is_null($value)) - { - return $value; - } - - $this->save($key, $value = $callback(), $ttl); - - return $value; - } - - //-------------------------------------------------------------------- - /** * Saves an item to the cache store. * diff --git a/system/Cache/Handlers/RedisHandler.php b/system/Cache/Handlers/RedisHandler.php index 985b58cc7d52..1a282cbd4bb2 100644 --- a/system/Cache/Handlers/RedisHandler.php +++ b/system/Cache/Handlers/RedisHandler.php @@ -11,17 +11,15 @@ namespace CodeIgniter\Cache\Handlers; -use CodeIgniter\Cache\CacheInterface; use CodeIgniter\Exceptions\CriticalError; use Config\Cache; -use Closure; use Redis; use RedisException; /** * Redis cache handler */ -class RedisHandler implements CacheInterface +class RedisHandler extends BaseHandler { /** * Prefixed to all cache names. @@ -164,31 +162,6 @@ public function get(string $key) //-------------------------------------------------------------------- - /** - * Get an item from the cache, or execute the given Closure and store the result. - * - * @param string $key Cache item name - * @param integer $ttl Time to live - * @param Closure $callback Callback return value - * - * @return mixed - */ - public function remember(string $key, int $ttl, Closure $callback) - { - $value = $this->get($key); - - if (! is_null($value)) - { - return $value; - } - - $this->save($key, $value = $callback(), $ttl); - - return $value; - } - - //-------------------------------------------------------------------- - /** * Saves an item to the cache store. * diff --git a/system/Cache/Handlers/WincacheHandler.php b/system/Cache/Handlers/WincacheHandler.php index d6dd61699766..de02789faed7 100644 --- a/system/Cache/Handlers/WincacheHandler.php +++ b/system/Cache/Handlers/WincacheHandler.php @@ -11,16 +11,14 @@ namespace CodeIgniter\Cache\Handlers; -use CodeIgniter\Cache\CacheInterface; use Config\Cache; -use Closure; /** * Cache handler for WinCache from Microsoft & IIS. * Windows-only, so not testable on travis-ci. * Unusable methods flagged for code coverage ignoring. */ -class WincacheHandler implements CacheInterface +class WincacheHandler extends BaseHandler { /** * Prefixed to all cache names. @@ -77,31 +75,6 @@ public function get(string $key) //-------------------------------------------------------------------- - /** - * Get an item from the cache, or execute the given Closure and store the result. - * - * @param string $key Cache item name - * @param integer $ttl Time to live - * @param Closure $callback Callback return value - * - * @return mixed - */ - public function remember(string $key, int $ttl, Closure $callback) - { - $value = $this->get($key); - - if (! is_null($value)) - { - return $value; - } - - $this->save($key, $value = $callback(), $ttl); - - return $value; - } - - //-------------------------------------------------------------------- - /** * Saves an item to the cache store. * From 6bd7eaf5efc44bf40849a5e6583080021c2912d0 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" <51850998+paulbalandan@users.noreply.github.com> Date: Thu, 28 Jan 2021 22:49:58 +0800 Subject: [PATCH 4/6] Update documentation for cache remember --- user_guide_src/source/libraries/caching.rst | 77 +++++++++++---------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/user_guide_src/source/libraries/caching.rst b/user_guide_src/source/libraries/caching.rst index 368a34498c09..330e30ac0e67 100644 --- a/user_guide_src/source/libraries/caching.rst +++ b/user_guide_src/source/libraries/caching.rst @@ -19,7 +19,7 @@ The following example shows a common usage pattern within your controllers. :: - if ( ! $foo = cache('foo')) + if (! $foo = cache('foo')) { echo 'Saving to the cache!
'; $foo = 'foobarbaz!'; @@ -75,15 +75,15 @@ The settings for the Redis server that you wish to use when using the ``Redis`` Class Reference *************** -.. php:method:: ⠀isSupported() +.. php:method:: isSupported() - :returns: TRUE if supported, FALSE if not + :returns: ``true`` if supported, ``false`` if not :rtype: bool -.. php:method:: ⠀get($key) +.. php:method:: get(string $key) - :param string $key: Cache item name - :returns: Item value or NULL if not found + :param string $key: Cache item name + :returns: Item value or ``null`` if not found :rtype: mixed This method will attempt to fetch an item from the cache store. If the @@ -93,17 +93,28 @@ Class Reference $foo = $cache->get('my_cached_item'); -.. php:method:: ⠀save($key, $data[, $ttl = 60[, $raw = FALSE]]) +.. php:method:: remember(string $key, int $ttl, Closure $callback) - :param string $key: Cache item name - :param mixed $data: the data to save - :param int $ttl: Time To Live, in seconds (default 60) - :param bool $raw: Whether to store the raw value - :returns: TRUE on success, FALSE on failure + :param string $key: Ccahe item name + :param int $ttl: Time to live in seconds + :param Closure $callback: Callback to invoke when the cache item returns null + :returns: The value of the cache item + :rtype: mixed + + Gets an item from the cache. If ``null`` was returned, this will invoke the callback + and save the result. Either way, this will return the value. + +.. php:method::⠀save(string $key, $data[, int $ttl = 60[, $raw = false]]) + + :param string $key: Cache item name + :param mixed $data: the data to save + :param int $ttl: Time To Live, in seconds (default 60) + :param bool $raw: Whether to store the raw value + :returns: ``true`` on success, ``false`` on failure :rtype: bool This method will save an item to the cache store. If saving fails, the - method will return FALSE. + method will return ``false``. Example:: @@ -112,10 +123,10 @@ Class Reference .. note:: The ``$raw`` parameter is only utilized by Memcache, in order to allow usage of ``increment()`` and ``decrement()``. -.. php:method:: ⠀delete($key) +.. php:method:: delete(string $key) - :param string $key: name of cached item - :returns: TRUE on success, FALSE on failure + :param string $key: name of cached item + :returns: ``true`` on success, ``false`` on failure :rtype: bool This method will delete a specific item from the cache store. If item @@ -125,11 +136,11 @@ Class Reference $cache->delete('cache_item_id'); -.. php:method:: ⠀increment($key[, $offset = 1]) +.. php:method:: increment(string $key[, int $offset = 1]) - :param string $key: Cache ID - :param int $offset: Step/value to add - :returns: New value on success, FALSE on failure + :param string $key: Cache ID + :param int $offset: Step/value to add + :returns: New value on success, ``false`` on failure :rtype: mixed Performs atomic incrementation of a raw stored value. @@ -137,16 +148,14 @@ Class Reference Example:: // 'iterator' has a value of 2 - $cache->increment('iterator'); // 'iterator' is now 3 - $cache->increment('iterator', 3); // 'iterator' is now 6 -.. php:method:: ⠀decrement($key[, $offset = 1]) +.. php:method:: decrement(string $key[, int $offset = 1]) - :param string $key: Cache ID - :param int $offset: Step/value to reduce by - :returns: New value on success, FALSE on failure + :param string $key: Cache ID + :param int $offset: Step/value to reduce by + :returns: New value on success, ``false`` on failure :rtype: mixed Performs atomic decrementation of a raw stored value. @@ -154,14 +163,12 @@ Class Reference Example:: // 'iterator' has a value of 6 - $cache->decrement('iterator'); // 'iterator' is now 5 - $cache->decrement('iterator', 2); // 'iterator' is now 3 -.. php:method:: ⠀clean() +.. php:method:: clean() - :returns: TRUE on success, FALSE on failure + :returns: ``true`` on success, ``false`` on failure :rtype: bool This method will 'clean' the entire cache. If the deletion of the @@ -171,9 +178,9 @@ Class Reference $cache->clean(); -.. php:method:: ⠀getCacheInfo() +.. php:method:: getCacheInfo() - :returns: Information on the entire cache database + :returns: Information on the entire cache database :rtype: mixed This method will return information on the entire cache. @@ -185,10 +192,10 @@ Class Reference .. note:: The information returned and the structure of the data is dependent on which adapter is being used. -.. php:method:: ⠀getMetadata($key) +.. php:method:: getMetadata(string $key) - :param string $key: Cache item name - :returns: Metadata for the cached item + :param string $key: Cache item name + :returns: Metadata for the cached item :rtype: mixed This method will return detailed information on a specific item in the From 1ec64855047da1e5ba1e33a22311736cf5fa4a98 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" <51850998+paulbalandan@users.noreply.github.com> Date: Thu, 28 Jan 2021 23:17:44 +0800 Subject: [PATCH 5/6] Fix duplicate object description in user guide --- user_guide_src/source/libraries/caching.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/user_guide_src/source/libraries/caching.rst b/user_guide_src/source/libraries/caching.rst index 330e30ac0e67..59685668d853 100644 --- a/user_guide_src/source/libraries/caching.rst +++ b/user_guide_src/source/libraries/caching.rst @@ -80,7 +80,7 @@ Class Reference :returns: ``true`` if supported, ``false`` if not :rtype: bool -.. php:method:: get(string $key) +.. php:method:: get($key) :param string $key: Cache item name :returns: Item value or ``null`` if not found @@ -123,7 +123,7 @@ Class Reference .. note:: The ``$raw`` parameter is only utilized by Memcache, in order to allow usage of ``increment()`` and ``decrement()``. -.. php:method:: delete(string $key) +.. php:method:: delete($key) :param string $key: name of cached item :returns: ``true`` on success, ``false`` on failure @@ -136,7 +136,7 @@ Class Reference $cache->delete('cache_item_id'); -.. php:method:: increment(string $key[, int $offset = 1]) +.. php:method:: increment($key[, $offset = 1]) :param string $key: Cache ID :param int $offset: Step/value to add @@ -151,7 +151,7 @@ Class Reference $cache->increment('iterator'); // 'iterator' is now 3 $cache->increment('iterator', 3); // 'iterator' is now 6 -.. php:method:: decrement(string $key[, int $offset = 1]) +.. php:method:: decrement($key[, $offset = 1]) :param string $key: Cache ID :param int $offset: Step/value to reduce by From c56a1ef8b191190a5f2381b8a08cafb092c6b8bb Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" <51850998+paulbalandan@users.noreply.github.com> Date: Thu, 28 Jan 2021 23:25:07 +0800 Subject: [PATCH 6/6] Update caching.rst --- user_guide_src/source/libraries/caching.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/user_guide_src/source/libraries/caching.rst b/user_guide_src/source/libraries/caching.rst index 59685668d853..b91a168512ab 100644 --- a/user_guide_src/source/libraries/caching.rst +++ b/user_guide_src/source/libraries/caching.rst @@ -80,7 +80,7 @@ Class Reference :returns: ``true`` if supported, ``false`` if not :rtype: bool -.. php:method:: get($key) +.. php:method:: get($key): mixed :param string $key: Cache item name :returns: Item value or ``null`` if not found @@ -123,7 +123,7 @@ Class Reference .. note:: The ``$raw`` parameter is only utilized by Memcache, in order to allow usage of ``increment()`` and ``decrement()``. -.. php:method:: delete($key) +.. php:method:: delete($key): bool :param string $key: name of cached item :returns: ``true`` on success, ``false`` on failure @@ -136,7 +136,7 @@ Class Reference $cache->delete('cache_item_id'); -.. php:method:: increment($key[, $offset = 1]) +.. php:method:: increment($key[, $offset = 1]): mixed :param string $key: Cache ID :param int $offset: Step/value to add @@ -151,7 +151,7 @@ Class Reference $cache->increment('iterator'); // 'iterator' is now 3 $cache->increment('iterator', 3); // 'iterator' is now 6 -.. php:method:: decrement($key[, $offset = 1]) +.. php:method:: decrement($key[, $offset = 1]): mixed :param string $key: Cache ID :param int $offset: Step/value to reduce by