diff --git a/.travis.yml b/.travis.yml index ba86bfb..358442d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,10 @@ language: php php: - - '5.4' - - '5.5' - - '5.6' - '7.0' - '7.1' + - '7.2' + - '7.3' services: - redis-server @@ -13,7 +12,6 @@ services: before_install: v=$(phpenv version-name); if [ ${v:0:1} -lt 7 ]; then pecl install igbinary ; yes | pecl install redis; else echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi install: - - composer global require --prefer-dist "fxp/composer-asset-plugin:^1.2.0" - composer require codeclimate/php-test-reporter --dev --prefer-dist - composer install --no-progress --no-interaction --prefer-dist diff --git a/README.md b/README.md index 2890ecf..d8356ba 100644 --- a/README.md +++ b/README.md @@ -4,25 +4,10 @@ This extension provides the [redis](http://redis.io/) key-value store support fo It includes a `Cache` and `Session` storage handler in redis. - -[![Build Status](https://travis-ci.org/dcb9/yii2-phpredis.svg?branch=master)](https://travis-ci.org/dcb9/yii2-phpredis) -[![Code Climate](https://codeclimate.com/github/dcb9/yii2-phpredis/badges/gpa.svg)](https://codeclimate.com/github/dcb9/yii2-phpredis) -[![Test Coverage](https://codeclimate.com/github/dcb9/yii2-phpredis/badges/coverage.svg)](https://codeclimate.com/github/dcb9/yii2-phpredis/coverage) -[![Issue Count](https://codeclimate.com/github/dcb9/yii2-phpredis/badges/issue_count.svg)](https://codeclimate.com/github/dcb9/yii2-phpredis) -[![Latest Stable Version](https://poser.pugx.org/dcb9/yii2-phpredis/version)](https://packagist.org/packages/dcb9/yii2-phpredis) -[![Total Downloads](https://poser.pugx.org/dcb9/yii2-phpredis/downloads)](https://packagist.org/packages/dcb9/yii2-phpredis) -[![License](https://poser.pugx.org/dcb9/yii2-phpredis/license)](https://packagist.org/packages/dcb9/yii2-phpredis) +[![Build Status](https://travis-ci.org/nuwber/yii2-phpredis.svg?branch=master)](https://travis-ci.org/nuwber/yii2-phpredis) **Notice: THIS REPO DOES NOT SUPPORT ACTIVE RECORD.** -Requirements ------------- - -- PHP >= 5.4.0 -- Redis >= 2.6.12 -- ext-redis >= 2.2.7 -- Yii2 ~2.0.4 - Installation ------------ @@ -50,15 +35,20 @@ To use this extension, you have to configure the Connection class in your applic ```php return [ - //.... 'components' => [ 'redis' => [ - 'class' => 'dcb9\redis\Connection', + 'class' => \dcb9\redis\Connection::class, 'hostname' => 'localhost', 'port' => 6379, 'database' => 0, ], - ] + 'cache' => [ + 'class' => \dcb9\redis\Cache::class, + ], + 'session' => [ + 'class' => \dcb9\redis\Session::class, + ], + ], ]; ``` @@ -67,22 +57,14 @@ Run unit test You can specific your redis config -``` +```bash $ cp tests/config.php tests/config-local.php -$ vim tests/config-local.php ``` and Run -``` +```bash $ ./vendor/bin/phpunit -PHPUnit 5.6.1 by Sebastian Bergmann and contributors. - -............ 12 / 12 (100%) - -Time: 600 ms, Memory: 10.00MB - -OK (12 tests, 50 assertions) ``` Performance test diff --git a/composer.json b/composer.json index 4895ae3..013cdda 100644 --- a/composer.json +++ b/composer.json @@ -10,25 +10,25 @@ } ], "require": { - "yiisoft/yii2": "~2.0.4", - "ext-redis": ">=2.2.7", - "php": ">=5.4.0" + "ext-json": "*", + "yiisoft/yii2": "~2.0.14", + "ext-redis": ">=3", + "php": ">=7.0" }, "autoload": { "psr-4": { - "dcb9\\redis\\": "" + "dcb9\\redis\\": "src/", + "dcb9\\redis\\tests\\": "tests/" } }, "require-dev": { "phpunit/phpunit": "<6.0", "yiisoft/yii2-redis": "^2.0" }, - "config": { - "fxp-asset": { - "vcs-driver-options": { - "github-no-api": true - }, - "pattern-skip-version": "(-build|-patch)" + "repositories": [ + { + "type": "composer", + "url": "https://asset-packagist.org" } - } + ] } diff --git a/phpredis-vs-yii-redis.png b/phpredis-vs-yii-redis.png index d977632..b1594f4 100644 Binary files a/phpredis-vs-yii-redis.png and b/phpredis-vs-yii-redis.png differ diff --git a/Cache.php b/src/Cache.php similarity index 99% rename from Cache.php rename to src/Cache.php index 725c9ee..452ff7f 100644 --- a/Cache.php +++ b/src/Cache.php @@ -23,11 +23,10 @@ class Cache extends \yii\caching\Cache public function init() { parent::init(); - $this->redis = Instance::ensure($this->redis, Connection::className()); + $this->redis = Instance::ensure($this->redis, Connection::class); $this->redis->open(); } - /** * @inheritdoc */ diff --git a/Connection.php b/src/Connection.php similarity index 93% rename from Connection.php rename to src/Connection.php index 040009b..634a686 100644 --- a/Connection.php +++ b/src/Connection.php @@ -64,15 +64,6 @@ public function __construct($config = []) } } - /** - * Returns the fully qualified name of this class. - * @return string the fully qualified name of this class. - */ - public static function className() - { - return get_called_class(); - } - /** * Establishes a DB connection. * It does nothing if a DB connection has already been established. @@ -123,7 +114,7 @@ public function ping() return parent::ping() === '+PONG'; } - public function flushdb() + public function flushdb($async = null) { return parent::flushDB(); } diff --git a/Session.php b/src/Session.php similarity index 98% rename from Session.php rename to src/Session.php index 5425b61..b82abca 100644 --- a/Session.php +++ b/src/Session.php @@ -77,7 +77,7 @@ public function init() $this->redis = Yii::$app->get($this->redis); } elseif (is_array($this->redis)) { if (!isset($this->redis['class'])) { - $this->redis['class'] = Connection::className(); + $this->redis['class'] = Connection::class; } $this->redis = Yii::createObject($this->redis); } diff --git a/tests/CacheTest.php b/tests/CacheTest.php index 1b8c156..ae981bd 100644 --- a/tests/CacheTest.php +++ b/tests/CacheTest.php @@ -97,7 +97,7 @@ public function testMultiSet() $this->assertEquals('v1', $cache->get('k1')); $this->assertEquals('v2', $cache->get('k2')); $this->assertEquals('v3', $cache->get('k3')); - sleep(1); + usleep(1100000); $this->assertFalse($cache->get('k1')); $this->assertFalse($cache->get('k2')); $this->assertFalse($cache->get('k3')); @@ -107,7 +107,6 @@ public function testMultiSet() $this->assertFalse($cache->exists('k3')); $cache->multiSet($items); - sleep(2); $this->assertEquals('v1', $cache->get('k1')); $this->assertEquals('v2', $cache->get('k2')); $this->assertEquals('v3', $cache->get('k3')); @@ -118,7 +117,7 @@ public function testSetGet() $cache = $this->getCacheInstance(); $cache->set('key', 'val', 1); $this->assertEquals('val', $cache->get('key')); - sleep(1); + usleep(1100000); $this->assertFalse($cache->get('key')); $this->assertFalse($cache->exists('key')); @@ -150,7 +149,7 @@ public function testMultiAdd() 'k1' => 'v1', ], 1)); $this->assertEquals('vv55', $cache->get('k5')); - sleep(1); + usleep(1100000); $this->assertFalse($cache->exists('k5')); $this->assertTrue($cache->exists('k1')); } diff --git a/tests/SessionTest.php b/tests/SessionTest.php index 237f641..413e4f6 100644 --- a/tests/SessionTest.php +++ b/tests/SessionTest.php @@ -2,30 +2,32 @@ namespace dcb9\redis\tests; -use dcb9\redis\Connection; use Yii; +use dcb9\redis\Connection; +use dcb9\redis\Session; class SessionTest extends TestCase { public function testSession() { - $params = self::getParam(); - $params['class'] = Connection::className(); $this->mockApplication([ 'components' => [ - 'redis' => $params, - 'session' => 'dcb9\\redis\\Session', + 'redis' => array_merge(self::getParam(), [ + 'class' => Connection::class, + ]), + 'session' => Session::class, ] ]); $sessionId = 'sessionId'; $session = Yii::$app->session; - $session->setTimeout(1); $sessionData = json_encode([ 'sessionId' => $sessionId, 'username' => 'bob', ]); + $session->writeSession($sessionId, $sessionData); + $this->assertEquals($sessionData, $session->readSession($sessionId)); $this->assertTrue($session->destroySession($sessionId)); $this->assertEquals('', $session->readSession($sessionId)); diff --git a/tests/performance.php b/tests/performance.php index 02b3c79..f8a69fd 100644 --- a/tests/performance.php +++ b/tests/performance.php @@ -56,3 +56,17 @@ $yiiRedis->get('yii_redis_prefix' . $i); } echo " " . ((microtime(true) - $start) * 1000) . " micro seconds.\n"; + +echo "phpredis run DEL $count times in"; +$start = microtime(true); +for ($i = 0; $i < $count; $i++) { + $phpRedis->del('php_redis_prefix' . $i); +} +echo " " . ((microtime(true) - $start) * 1000) . " micro seconds.\n"; + +echo "yii redis run DEL $count times in"; +$start = microtime(true); +for ($i = 0; $i < $count; $i++) { + $yiiRedis->del('yii_redis_prefix' . $i); +} +echo " " . ((microtime(true) - $start) * 1000) . " micro seconds.\n";