Skip to content

Commit 0891e0d

Browse files
phpredis >3 compatibility (#1)
* add src path, fixes for phpredis 4, update php version, other minor fixes * update picture * fix doc, fix build
1 parent 836f9ff commit 0891e0d

File tree

10 files changed

+52
-67
lines changed

10 files changed

+52
-67
lines changed

.travis.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
language: php
22

33
php:
4-
- '5.4'
5-
- '5.5'
6-
- '5.6'
74
- '7.0'
85
- '7.1'
6+
- '7.2'
7+
- '7.3'
98

109
services:
1110
- redis-server
1211

1312
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
1413

1514
install:
16-
- composer global require --prefer-dist "fxp/composer-asset-plugin:^1.2.0"
1715
- composer require codeclimate/php-test-reporter --dev --prefer-dist
1816
- composer install --no-progress --no-interaction --prefer-dist
1917

README.md

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,10 @@ This extension provides the [redis](http://redis.io/) key-value store support fo
44

55
It includes a `Cache` and `Session` storage handler in redis.
66

7-
8-
[![Build Status](https://travis-ci.org/dcb9/yii2-phpredis.svg?branch=master)](https://travis-ci.org/dcb9/yii2-phpredis)
9-
[![Code Climate](https://codeclimate.com/github/dcb9/yii2-phpredis/badges/gpa.svg)](https://codeclimate.com/github/dcb9/yii2-phpredis)
10-
[![Test Coverage](https://codeclimate.com/github/dcb9/yii2-phpredis/badges/coverage.svg)](https://codeclimate.com/github/dcb9/yii2-phpredis/coverage)
11-
[![Issue Count](https://codeclimate.com/github/dcb9/yii2-phpredis/badges/issue_count.svg)](https://codeclimate.com/github/dcb9/yii2-phpredis)
12-
[![Latest Stable Version](https://poser.pugx.org/dcb9/yii2-phpredis/version)](https://packagist.org/packages/dcb9/yii2-phpredis)
13-
[![Total Downloads](https://poser.pugx.org/dcb9/yii2-phpredis/downloads)](https://packagist.org/packages/dcb9/yii2-phpredis)
14-
[![License](https://poser.pugx.org/dcb9/yii2-phpredis/license)](https://packagist.org/packages/dcb9/yii2-phpredis)
7+
[![Build Status](https://travis-ci.org/nuwber/yii2-phpredis.svg?branch=master)](https://travis-ci.org/nuwber/yii2-phpredis)
158

169
**Notice: THIS REPO DOES NOT SUPPORT ACTIVE RECORD.**
1710

18-
Requirements
19-
------------
20-
21-
- PHP >= 5.4.0
22-
- Redis >= 2.6.12
23-
- ext-redis >= 2.2.7
24-
- Yii2 ~2.0.4
25-
2611
Installation
2712
------------
2813

@@ -50,15 +35,20 @@ To use this extension, you have to configure the Connection class in your applic
5035

5136
```php
5237
return [
53-
//....
5438
'components' => [
5539
'redis' => [
56-
'class' => 'dcb9\redis\Connection',
40+
'class' => \dcb9\redis\Connection::class,
5741
'hostname' => 'localhost',
5842
'port' => 6379,
5943
'database' => 0,
6044
],
61-
]
45+
'cache' => [
46+
'class' => \dcb9\redis\Cache::class,
47+
],
48+
'session' => [
49+
'class' => \dcb9\redis\Session::class,
50+
],
51+
],
6252
];
6353
```
6454

@@ -67,22 +57,14 @@ Run unit test
6757

6858
You can specific your redis config
6959

70-
```
60+
```bash
7161
$ cp tests/config.php tests/config-local.php
72-
$ vim tests/config-local.php
7362
```
7463

7564
and Run
7665

77-
```
66+
```bash
7867
$ ./vendor/bin/phpunit
79-
PHPUnit 5.6.1 by Sebastian Bergmann and contributors.
80-
81-
............ 12 / 12 (100%)
82-
83-
Time: 600 ms, Memory: 10.00MB
84-
85-
OK (12 tests, 50 assertions)
8668
```
8769

8870
Performance test

composer.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@
1010
}
1111
],
1212
"require": {
13-
"yiisoft/yii2": "~2.0.4",
14-
"ext-redis": ">=2.2.7",
15-
"php": ">=5.4.0"
13+
"ext-json": "*",
14+
"yiisoft/yii2": "~2.0.14",
15+
"ext-redis": ">=3",
16+
"php": ">=7.0"
1617
},
1718
"autoload": {
1819
"psr-4": {
19-
"dcb9\\redis\\": ""
20+
"dcb9\\redis\\": "src/",
21+
"dcb9\\redis\\tests\\": "tests/"
2022
}
2123
},
2224
"require-dev": {
2325
"phpunit/phpunit": "<6.0",
2426
"yiisoft/yii2-redis": "^2.0"
2527
},
26-
"config": {
27-
"fxp-asset": {
28-
"vcs-driver-options": {
29-
"github-no-api": true
30-
},
31-
"pattern-skip-version": "(-build|-patch)"
28+
"repositories": [
29+
{
30+
"type": "composer",
31+
"url": "https://asset-packagist.org"
3232
}
33-
}
33+
]
3434
}

phpredis-vs-yii-redis.png

11 KB
Loading

Cache.php renamed to src/Cache.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@ class Cache extends \yii\caching\Cache
2323
public function init()
2424
{
2525
parent::init();
26-
$this->redis = Instance::ensure($this->redis, Connection::className());
26+
$this->redis = Instance::ensure($this->redis, Connection::class);
2727
$this->redis->open();
2828
}
2929

30-
3130
/**
3231
* @inheritdoc
3332
*/

Connection.php renamed to src/Connection.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,6 @@ public function __construct($config = [])
6464
}
6565
}
6666

67-
/**
68-
* Returns the fully qualified name of this class.
69-
* @return string the fully qualified name of this class.
70-
*/
71-
public static function className()
72-
{
73-
return get_called_class();
74-
}
75-
7667
/**
7768
* Establishes a DB connection.
7869
* It does nothing if a DB connection has already been established.
@@ -123,7 +114,7 @@ public function ping()
123114
return parent::ping() === '+PONG';
124115
}
125116

126-
public function flushdb()
117+
public function flushdb($async = null)
127118
{
128119
return parent::flushDB();
129120
}

Session.php renamed to src/Session.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function init()
7777
$this->redis = Yii::$app->get($this->redis);
7878
} elseif (is_array($this->redis)) {
7979
if (!isset($this->redis['class'])) {
80-
$this->redis['class'] = Connection::className();
80+
$this->redis['class'] = Connection::class;
8181
}
8282
$this->redis = Yii::createObject($this->redis);
8383
}

tests/CacheTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function testMultiSet()
9797
$this->assertEquals('v1', $cache->get('k1'));
9898
$this->assertEquals('v2', $cache->get('k2'));
9999
$this->assertEquals('v3', $cache->get('k3'));
100-
sleep(1);
100+
usleep(1100000);
101101
$this->assertFalse($cache->get('k1'));
102102
$this->assertFalse($cache->get('k2'));
103103
$this->assertFalse($cache->get('k3'));
@@ -107,7 +107,6 @@ public function testMultiSet()
107107
$this->assertFalse($cache->exists('k3'));
108108

109109
$cache->multiSet($items);
110-
sleep(2);
111110
$this->assertEquals('v1', $cache->get('k1'));
112111
$this->assertEquals('v2', $cache->get('k2'));
113112
$this->assertEquals('v3', $cache->get('k3'));
@@ -118,7 +117,7 @@ public function testSetGet()
118117
$cache = $this->getCacheInstance();
119118
$cache->set('key', 'val', 1);
120119
$this->assertEquals('val', $cache->get('key'));
121-
sleep(1);
120+
usleep(1100000);
122121
$this->assertFalse($cache->get('key'));
123122
$this->assertFalse($cache->exists('key'));
124123

@@ -150,7 +149,7 @@ public function testMultiAdd()
150149
'k1' => 'v1',
151150
], 1));
152151
$this->assertEquals('vv55', $cache->get('k5'));
153-
sleep(1);
152+
usleep(1100000);
154153
$this->assertFalse($cache->exists('k5'));
155154
$this->assertTrue($cache->exists('k1'));
156155
}

tests/SessionTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,32 @@
22

33
namespace dcb9\redis\tests;
44

5-
use dcb9\redis\Connection;
65
use Yii;
6+
use dcb9\redis\Connection;
7+
use dcb9\redis\Session;
78

89
class SessionTest extends TestCase
910
{
1011
public function testSession()
1112
{
12-
$params = self::getParam();
13-
$params['class'] = Connection::className();
1413
$this->mockApplication([
1514
'components' => [
16-
'redis' => $params,
17-
'session' => 'dcb9\\redis\\Session',
15+
'redis' => array_merge(self::getParam(), [
16+
'class' => Connection::class,
17+
]),
18+
'session' => Session::class,
1819
]
1920
]);
2021

2122
$sessionId = 'sessionId';
2223
$session = Yii::$app->session;
23-
$session->setTimeout(1);
2424
$sessionData = json_encode([
2525
'sessionId' => $sessionId,
2626
'username' => 'bob',
2727
]);
28+
2829
$session->writeSession($sessionId, $sessionData);
30+
2931
$this->assertEquals($sessionData, $session->readSession($sessionId));
3032
$this->assertTrue($session->destroySession($sessionId));
3133
$this->assertEquals('', $session->readSession($sessionId));

tests/performance.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,17 @@
5656
$yiiRedis->get('yii_redis_prefix' . $i);
5757
}
5858
echo " " . ((microtime(true) - $start) * 1000) . " micro seconds.\n";
59+
60+
echo "phpredis run DEL $count times in";
61+
$start = microtime(true);
62+
for ($i = 0; $i < $count; $i++) {
63+
$phpRedis->del('php_redis_prefix' . $i);
64+
}
65+
echo " " . ((microtime(true) - $start) * 1000) . " micro seconds.\n";
66+
67+
echo "yii redis run DEL $count times in";
68+
$start = microtime(true);
69+
for ($i = 0; $i < $count; $i++) {
70+
$yiiRedis->del('yii_redis_prefix' . $i);
71+
}
72+
echo " " . ((microtime(true) - $start) * 1000) . " micro seconds.\n";

0 commit comments

Comments
 (0)