Skip to content

Commit

Permalink
Cache remember (#4107)
Browse files Browse the repository at this point in the history
* Implement cache remember

* Remove method remember from interface

* Cache move to BaseHandler

* Update documentation for cache remember

* Fix duplicate object description in user guide

* Update caching.rst

Co-authored-by: John Paul E. Balandan, CPA <[email protected]>
  • Loading branch information
agungsugiarto and paulbalandan authored Jan 28, 2021
1 parent 2d930ca commit 7a0b54d
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 47 deletions.
44 changes: 44 additions & 0 deletions system/Cache/Handlers/BaseHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* 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;
}
}
20 changes: 18 additions & 2 deletions system/Cache/Handlers/DummyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +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.
Expand All @@ -42,6 +42,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.
*
Expand Down
3 changes: 1 addition & 2 deletions system/Cache/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@

namespace CodeIgniter\Cache\Handlers;

use CodeIgniter\Cache\CacheInterface;
use CodeIgniter\Cache\Exceptions\CacheException;
use Config\Cache;

/**
* File system cache handler
*/
class FileHandler implements CacheInterface
class FileHandler extends BaseHandler
{
/**
* Prefixed to all cache names.
Expand Down
3 changes: 1 addition & 2 deletions system/Cache/Handlers/MemcachedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace CodeIgniter\Cache\Handlers;

use CodeIgniter\Cache\CacheInterface;
use CodeIgniter\Exceptions\CriticalError;
use Config\Cache;
use Exception;
Expand All @@ -21,7 +20,7 @@
/**
* Mamcached cache handler
*/
class MemcachedHandler implements CacheInterface
class MemcachedHandler extends BaseHandler
{
/**
* Prefixed to all cache names.
Expand Down
3 changes: 1 addition & 2 deletions system/Cache/Handlers/PredisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace CodeIgniter\Cache\Handlers;

use CodeIgniter\Cache\CacheInterface;
use CodeIgniter\Exceptions\CriticalError;
use Config\Cache;
use Exception;
Expand All @@ -20,7 +19,7 @@
/**
* Predis cache handler
*/
class PredisHandler implements CacheInterface
class PredisHandler extends BaseHandler
{
/**
* Prefixed to all cache names.
Expand Down
3 changes: 1 addition & 2 deletions system/Cache/Handlers/RedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace CodeIgniter\Cache\Handlers;

use CodeIgniter\Cache\CacheInterface;
use CodeIgniter\Exceptions\CriticalError;
use Config\Cache;
use Redis;
Expand All @@ -20,7 +19,7 @@
/**
* Redis cache handler
*/
class RedisHandler implements CacheInterface
class RedisHandler extends BaseHandler
{
/**
* Prefixed to all cache names.
Expand Down
3 changes: 1 addition & 2 deletions system/Cache/Handlers/WincacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@

namespace CodeIgniter\Cache\Handlers;

use CodeIgniter\Cache\CacheInterface;
use Config\Cache;

/**
* 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.
Expand Down
26 changes: 26 additions & 0 deletions system/Test/Mock/MockCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Test\Mock;

use CodeIgniter\Cache\CacheInterface;
use Closure;

class MockCache implements CacheInterface
{
Expand Down Expand Up @@ -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.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/system/Cache/Handlers/DummyHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
13 changes: 13 additions & 0 deletions tests/system/Cache/Handlers/FileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,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'));
Expand Down
13 changes: 13 additions & 0 deletions tests/system/Cache/Handlers/MemcachedHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
13 changes: 13 additions & 0 deletions tests/system/Cache/Handlers/PredisHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
13 changes: 13 additions & 0 deletions tests/system/Cache/Handlers/RedisHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ public function testGet()
$this->assertNull($this->redisHandler->get(self::$key1));
}

public function testRemember()
{
$this->redisHandler->remember(self::$key1, 2, function () {
return 'value';
});

$this->assertSame('value', $this->redisHandler->get(self::$key1));
$this->assertNull($this->redisHandler->get(self::$dummy));

\CodeIgniter\CLI\CLI::wait(3);
$this->assertNull($this->redisHandler->get(self::$key1));
}

public function testSave()
{
$this->assertTrue($this->redisHandler->save(self::$key1, 'value'));
Expand Down
Loading

0 comments on commit 7a0b54d

Please sign in to comment.