Skip to content
This repository has been archived by the owner on Aug 15, 2018. It is now read-only.

Commit

Permalink
phpredis >3 compatibility (#1)
Browse files Browse the repository at this point in the history
* add src path, fixes for phpredis 4, update php version, other minor fixes

* update picture

* fix doc, fix build
  • Loading branch information
andrew-nuwber authored Jun 26, 2019
1 parent 836f9ff commit 0891e0d
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 67 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
language: php

php:
- '5.4'
- '5.5'
- '5.6'
- '7.0'
- '7.1'
- '7.2'
- '7.3'

services:
- redis-server

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

Expand Down
40 changes: 11 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------

Expand Down Expand Up @@ -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,
],
],
];
```

Expand All @@ -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
Expand Down
22 changes: 11 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
}
Binary file modified phpredis-vs-yii-redis.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions Cache.php → src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
11 changes: 1 addition & 10 deletions Connection.php → src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -123,7 +114,7 @@ public function ping()
return parent::ping() === '+PONG';
}

public function flushdb()
public function flushdb($async = null)
{
return parent::flushDB();
}
Expand Down
2 changes: 1 addition & 1 deletion Session.php → src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
7 changes: 3 additions & 4 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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'));
Expand All @@ -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'));

Expand Down Expand Up @@ -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'));
}
Expand Down
14 changes: 8 additions & 6 deletions tests/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
14 changes: 14 additions & 0 deletions tests/performance.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";

0 comments on commit 0891e0d

Please sign in to comment.