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 2 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
2 changes: 2 additions & 0 deletions system/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace CodeIgniter\Cache;

use Closure;

paulbalandan marked this conversation as resolved.
Show resolved Hide resolved
/**
* Cache interface
*/
Expand Down
17 changes: 17 additions & 0 deletions system/Cache/Handlers/DummyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Cache\Handlers;

use CodeIgniter\Cache\CacheInterface;
use Closure;

/**
* Dummy cache handler
Expand Down Expand Up @@ -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.
*
Expand Down
26 changes: 26 additions & 0 deletions system/Cache/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\Cache\CacheInterface;
use CodeIgniter\Cache\Exceptions\CacheException;
use Config\Cache;
use Closure;

/**
* File system cache handler
Expand Down Expand Up @@ -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;
}
paulbalandan marked this conversation as resolved.
Show resolved Hide resolved

//--------------------------------------------------------------------

/**
* Saves an item to the cache store.
*
Expand Down
26 changes: 26 additions & 0 deletions system/Cache/Handlers/MemcachedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\Cache\CacheInterface;
use CodeIgniter\Exceptions\CriticalError;
use Config\Cache;
use Closure;
use Exception;
use Memcache;
use Memcached;
Expand Down Expand Up @@ -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.
*
Expand Down
26 changes: 26 additions & 0 deletions system/Cache/Handlers/PredisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\Cache\CacheInterface;
use CodeIgniter\Exceptions\CriticalError;
use Config\Cache;
use Closure;
use Exception;
use Predis\Client;

Expand Down Expand Up @@ -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.
*
Expand Down
26 changes: 26 additions & 0 deletions system/Cache/Handlers/RedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\Cache\CacheInterface;
use CodeIgniter\Exceptions\CriticalError;
use Config\Cache;
use Closure;
use Redis;
use RedisException;

Expand Down Expand Up @@ -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.
*
Expand Down
26 changes: 26 additions & 0 deletions system/Cache/Handlers/WincacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

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

/**
* Cache handler for WinCache from Microsoft & IIS.
Expand Down Expand Up @@ -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.
*
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