diff --git a/.travis.yml b/.travis.yml index 89987b318611..48fb82ffacfd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,6 +20,7 @@ services: - memcached - mysql - postgresql + - redis-server script: - php vendor/bin/phpunit -v @@ -31,6 +32,7 @@ before_install: before_script: - echo 'extension = memcached.so' >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini + - echo 'extension = redis.so' >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini - composer install --prefer-source after_success: diff --git a/system/Cache/Handlers/RedisHandler.php b/system/Cache/Handlers/RedisHandler.php index 5fca7828da42..32bc318d3ca0 100644 --- a/system/Cache/Handlers/RedisHandler.php +++ b/system/Cache/Handlers/RedisHandler.php @@ -70,13 +70,13 @@ class RedisHandler implements CacheInterface //-------------------------------------------------------------------- - public function __construct($config) + public function __construct(array $config) { - $this->prefix = $config->prefix ?: ''; + $this->prefix = $config['prefix'] ?? ''; - if (isset($config->redis)) + if ( ! empty($config)) { - $this->config = array_merge($this->config, $config->redis); + $this->config = array_merge($this->config, $config); } } @@ -297,10 +297,12 @@ public function getMetaData(string $key) if ($value !== FALSE) { - return array( - 'expire' => time() + $this->redis->ttl($key), - 'data' => $value - ); + $time = time(); + return [ + 'expire' => $time + $this->redis->ttl($key), + 'mtime' => $time, + 'data' => $value + ]; } return FALSE; diff --git a/tests/system/Cache/Handlers/RedisHandlerTest.php b/tests/system/Cache/Handlers/RedisHandlerTest.php new file mode 100644 index 000000000000..91da1201322a --- /dev/null +++ b/tests/system/Cache/Handlers/RedisHandlerTest.php @@ -0,0 +1,152 @@ +config = new \Config\Cache(); + + $this->redisHandler = new RedisHandler($this->config->redis); + if (!$this->redisHandler->isSupported()) { + $this->markTestSkipped('Not support redis'); + } + + $this->redisHandler->initialize(); + } + + public function tearDown() + { + foreach (self::getKeyArray() as $key) { + $this->redisHandler->delete($key); + } + } + + public function testNew() + { + $this->assertInstanceOf(RedisHandler::class, $this->redisHandler); + } + + public function testDestruct() + { + $this->redisHandler = new RedisHandler($this->config->redis); + $this->redisHandler->initialize(); + + $this->assertInstanceOf(RedisHandler::class, $this->redisHandler); + } + + + public function testGet() + { + $this->redisHandler->save(self::$key1, 'value', 1); + + $this->assertSame('value', $this->redisHandler->get(self::$key1)); + $this->assertFalse($this->redisHandler->get(self::$dummy)); + + \CodeIgniter\CLI\CLI::wait(2); + $this->assertFalse($this->redisHandler->get(self::$key1)); + } + + public function testSave() + { + $this->assertTrue($this->redisHandler->save(self::$key1, 'value')); + } + + public function testDelete() + { + $this->redisHandler->save(self::$key1, 'value'); + + $this->assertTrue($this->redisHandler->delete(self::$key1)); + $this->assertFalse($this->redisHandler->delete(self::$dummy)); + } + + //FIXME: I don't like all Hash logic very much. It's wasting memory. + //public function testIncrement() + //{ + //} + + //public function testDecrement() + //{ + //} + + public function testClean() + { + $this->redisHandler->save(self::$key1, 1); + $this->redisHandler->save(self::$key2, 'value'); + + $this->assertTrue($this->redisHandler->clean()); + } + + public function testGetCacheInfo() + { + $this->redisHandler->save(self::$key1, 'value'); + + $this->assertInternalType('array', $this->redisHandler->getCacheInfo()); + } + + public function testGetMetaData() + { + $time = time(); + $this->redisHandler->save(self::$key1, 'value'); + + $this->assertFalse($this->redisHandler->getMetaData(self::$dummy)); + + $actual = $this->redisHandler->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->redisHandler->isSupported()); + } +}