update userguide

This commit is contained in:
Instrye 2020-05-20 10:59:51 +08:00
parent eebda65055
commit 484abb86d9
No known key found for this signature in database
GPG Key ID: 1A9D73C043CE3BE5
9 changed files with 146 additions and 18 deletions

View File

@ -19,7 +19,8 @@
"fzaninotto/faker": "^1.9@dev",
"mikey179/vfsstream": "1.6.*",
"phpunit/phpunit": "^8.5",
"squizlabs/php_codesniffer": "^3.3"
"squizlabs/php_codesniffer": "^3.3",
"predis/predis": "^1.1"
},
"autoload": {
"psr-4": {

View File

@ -92,7 +92,7 @@ class DummyHandler implements CacheInterface
*
* @param string $key Cache item name
*
* @return mixed
* @return boolean
*/
public function delete(string $key)
{
@ -134,7 +134,7 @@ class DummyHandler implements CacheInterface
/**
* Will delete all items in the entire cache.
*
* @return mixed
* @return boolean
*/
public function clean()
{

View File

@ -148,7 +148,7 @@ class FileHandler implements CacheInterface
*
* @param string $key Cache item name
*
* @return mixed
* @return boolean
*/
public function delete(string $key)
{
@ -228,7 +228,7 @@ class FileHandler implements CacheInterface
/**
* Will delete all items in the entire cache.
*
* @return mixed
* @return boolean
*/
public function clean()
{

View File

@ -263,7 +263,7 @@ class MemcachedHandler implements CacheInterface
*
* @param string $key Cache item name
*
* @return mixed
* @return boolean
*/
public function delete(string $key)
{
@ -322,7 +322,7 @@ class MemcachedHandler implements CacheInterface
/**
* Will delete all items in the entire cache.
*
* @return mixed
* @return boolean
*/
public function clean()
{

View File

@ -203,7 +203,7 @@ class PredisHandler implements CacheInterface
*
* @param string $key Cache item name
*
* @return mixed
* @return boolean
*/
public function delete(string $key)
{
@ -245,11 +245,11 @@ class PredisHandler implements CacheInterface
/**
* Will delete all items in the entire cache.
*
* @return mixed
* @return boolean
*/
public function clean()
{
return $this->redis->flushdb();
return $this->redis->flushdb() === 'OK';
}
//--------------------------------------------------------------------
@ -282,13 +282,15 @@ class PredisHandler implements CacheInterface
if (isset($data['__ci_value']) && $data['__ci_value'] !== false)
{
$time = time();
return [
'expire' => time() + $this->redis->ttl($key),
'expire' => $time + $this->redis->ttl($key),
'mtime' => $time,
'data' => $data['__ci_value'],
];
}
return false;
return null;
}
//--------------------------------------------------------------------

View File

@ -241,7 +241,7 @@ class RedisHandler implements CacheInterface
*
* @param string $key Cache item name
*
* @return mixed
* @return boolean
*/
public function delete(string $key)
{
@ -289,7 +289,7 @@ class RedisHandler implements CacheInterface
/**
* Will delete all items in the entire cache.
*
* @return mixed
* @return boolean
*/
public function clean()
{

View File

@ -133,7 +133,7 @@ class WincacheHandler implements CacheInterface
*
* @param string $key Cache item name
*
* @return mixed
* @return boolean
*/
public function delete(string $key)
{
@ -192,7 +192,7 @@ class WincacheHandler implements CacheInterface
*
* @codeCoverageIgnore
*
* @return mixed
* @return boolean
*/
public function clean()
{

View File

@ -0,0 +1,113 @@
<?php namespace CodeIgniter\Cache\Handlers;
class PredisHandlerTest extends \CodeIgniter\Test\CIUnitTestCase
{
private $PredisHandler;
private static $key1 = 'key1';
private static $key2 = 'key2';
private static $key3 = 'key3';
private static function getKeyArray()
{
return [
self::$key1,
self::$key2,
self::$key3,
];
}
private static $dummy = 'dymmy';
private $config;
protected function setUp(): void
{
parent::setUp();
$this->config = new \Config\Cache();
$this->PredisHandler = new PredisHandler($this->config);
if (! $this->PredisHandler->isSupported())
{
$this->markTestSkipped('Not support Predis');
}
$this->PredisHandler->initialize();
}
public function tearDown(): void
{
foreach (self::getKeyArray() as $key)
{
$this->PredisHandler->delete($key);
}
}
public function testNew()
{
$this->assertInstanceOf(PredisHandler::class, $this->PredisHandler);
}
public function testDestruct()
{
$this->PredisHandler = new PRedisHandler($this->config);
$this->PredisHandler->initialize();
$this->assertInstanceOf(PRedisHandler::class, $this->PredisHandler);
}
public function testGet()
{
$this->PredisHandler->save(self::$key1, 'value', 1);
$this->assertSame('value', $this->PredisHandler->get(self::$key1));
$this->assertNull($this->PredisHandler->get(self::$dummy));
\CodeIgniter\CLI\CLI::wait(2);
$this->assertNull($this->PredisHandler->get(self::$key1));
}
public function testSave()
{
$this->assertTrue($this->PredisHandler->save(self::$key1, 'value'));
}
public function testDelete()
{
$this->PredisHandler->save(self::$key1, 'value');
$this->assertTrue($this->PredisHandler->delete(self::$key1));
$this->assertFalse($this->PredisHandler->delete(self::$dummy));
}
public function testClean()
{
$this->PredisHandler->save(self::$key1, 1);
$this->PredisHandler->save(self::$key2, 'value');
$this->assertTrue($this->PredisHandler->clean());
}
public function testGetCacheInfo()
{
$this->PredisHandler->save(self::$key1, 'value');
$this->assertIsArray($this->PredisHandler->getCacheInfo());
}
public function testGetMetaData()
{
$time = time();
$this->PredisHandler->save(self::$key1, 'value');
$this->assertNull($this->PredisHandler->getMetaData(self::$dummy));
$actual = $this->PredisHandler->getMetaData(self::$key1);
$this->assertLessThanOrEqual(60, $actual['expire'] - $time);
$this->assertLessThanOrEqual(0, $actual['mtime'] - $time);
$this->assertSame('value', $actual['data']);
}
public function testIsSupported()
{
$this->assertTrue($this->PredisHandler->isSupported());
}
}

View File

@ -46,7 +46,7 @@ the following items are available.
**$handler**
The is the name of the handler that should be used as the primary handler when starting up the engine.
Available names are: dummy, file, memcached, redis, wincache.
Available names are: dummy, file, memcached, redis, predis, wincache.
**$backupHandler**
@ -69,7 +69,7 @@ This is an array of servers that will be used when using the ``Memcache(d)`` han
**$redis**
The settings for the Redis server that you wish to use when using the ``Redis`` handler.
The settings for the Redis server that you wish to use when using the ``Redis`` and ``Predis`` handler.
***************
Class Reference
@ -259,6 +259,18 @@ Config options to connect to redis server stored in the cache configuration file
For more information on Redis, please see
`https://redis.io <https://redis.io>`_.
=============
Predis Caching
=============
Predis is a flexible and feature-complete PHP client library for the Redis key-value store.
To use it, from the command line inside your project root::
composer require predis/predis
For more information on Redis, please see
`https://github.com/nrk/predis <https://github.com/nrk/predis>`_.
===========
Dummy Cache
===========