Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache remember #4107

Merged
merged 6 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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'));
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