Giter Site home page Giter Site logo

yii2-redis's Introduction

Redis Cache, Session and ActiveRecord for Yii 2


This extension provides the redis key-value store support for the Yii framework 2.0. It includes a Cache and Session storage handler and implements the ActiveRecord pattern that allows you to store active records in redis.

For license information check the LICENSE-file.

Documentation is at docs/guide/README.md.

Latest Stable Version Total Downloads Build status

Requirements

At least redis version 2.6.12 is required for all components to work properly.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yiisoft/yii2-redis:"~2.0.0"

or add

"yiisoft/yii2-redis": "~2.0.0"

to the require section of your composer.json.

Configuration

To use this extension, you have to configure the Connection class in your application configuration:

return [
    //....
    'components' => [
        'redis' => [
            'class' => 'yii\redis\Connection',
            'hostname' => 'localhost',
            'port' => 6379,
            'database' => 0,
        ],
    ]
];

SSL configuration example:

return [
    //....
    'components' => [
        'redis' => [
            'class' => 'yii\redis\Connection',
            'hostname' => 'localhost',
            'port' => 6380,
            'database' => 0,
            'useSSL' => true,
            // Use contextOptions for more control over the connection (https://www.php.net/manual/en/context.php), not usually needed
            'contextOptions' => [
                'ssl' => [
                    'local_cert' => '/path/to/local/certificate',
                    'local_pk' => '/path/to/local/private_key',
                ],
            ],
        ],
    ],
];

Configuring The Connection Scheme

By default, Redis will use the tcp scheme when connecting to your Redis server; however, you may use TLS / SSL encryption by specifying a scheme configuration option in your application configuration:

return [
    //....
    'components' => [
        'redis' => [
            //....
            'scheme' => 'tls'
        ]
    ]
];

yii2-redis's People

Contributors

bwoester avatar cebe avatar creocoder avatar crtlib avatar egorpromo avatar ext4yii avatar gevik avatar gonimar avatar kartik-v avatar klimov-paul avatar lancecoder avatar larryullman avatar lucianobaraglia avatar mohorev avatar nineinchnick avatar pmoust avatar qiangxue avatar qiansen1386 avatar ragazzo avatar resurtm avatar rinatio avatar rob006 avatar samdark avatar schmunk42 avatar sensorario avatar slavcodev avatar softark avatar suralc avatar tarasio avatar tonydspaniard avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yii2-redis's Issues

Redis error: ERR wrong number of arguments for 'set' command

Page caching is working in redis version

=> Redis server v=2.8.4 sha=00000000:0 malloc=jemalloc-3.4.1 bits=64 build=a44a05d76f06a5d9
=> yii 2.0.7 | PHP 5.5.9-1 | ubuntu4.17

But its not working in below version

=>Redis server v=3.2.1 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=4088abd4b3d654a1
=> yii 2.0.7 | PHP 5.5.37 (cli) | CentOS release 6.8 (Final)

Showing following error

An Error occurred while handling another error:
exception 'yii\base\InvalidCallException' with message 'Unexpected yii\widgets\FragmentCache::end() call. A matching begin() is not found.' in /home/public_html/vendor/yiisoft/yii2/base/Widget.php:83
Stack trace:
#0 /home/public_html/vendor/yiisoft/yii2/base/View.php(475): yii\base\Widget::end()
#1 /home/public_html/vendor/yiisoft/yii2/filters/PageCache.php(214): yii\base\View->endCache()
#2 [internal function]: yii\filters\PageCache->cacheResponse(Object(yii\base\Event))
#3 /home/public_html/vendor/yiisoft/yii2/base/Component.php(541): call_user_func(Array, Object(yii\base\Event))
#4 /home/public_html/vendor/yiisoft/yii2/web/Response.php(316): yii\base\Component->trigger('afterSend')
#5 /home/public_html/vendor/yiisoft/yii2/web/ErrorHandler.php(126): yii\web\Response->send()
#6 /home/public_html/vendor/yiisoft/yii2/base/ErrorHandler.php(109): yii\web\ErrorHandler->renderException(Object(yii\db\Exception))
#7 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\db\Exception))
#8 {main}

Previous exception:
exception 'yii\db\Exception' with message 'Redis error: ERR wrong number of arguments for 'set' command
`Redis command was: SET 106673edd0deebb81340fa78d36776db a:2:{i:0;a:2:{i:0;s:18723:"

<title>Order Services</title> ..................................... ................................. });</script> ";i:1;N;}i:1;N;} PX 86400000' in /home/public_html/vendor/yiisoft/yii2-redis/Connection.php:417`

ActiveRecord findall pool duplicating keys.

RPUSH is saved keys to findall pool with duplicated values. Because Redis LIST is not unique.
Finally find()->all() return a lot of duplicated objects.
As solution it can use sorted set and store objects with SADD and get it with SMEMBERS command instead of LRANGE.

mb_strlen() expects parameter 1 to be string, array given

What steps will reproduce the problem?

Disable 'serializer' for cache :
My code:

'pageCache' => [
                'class' => 'yii\filters\PageCache',
                'cache' => [
                    'class' => 'yii\redis\Cache',
                    'keyPrefix' => 'cache_yandex_map_',
                    'serializer' => false,
                    'redis' => [
                        'hostname' => 'localhost',
                        'port' => 6379,
                        'database' => 0,
                    ],
                ],
                'only' => ['static-map'],
                'duration' => 60 * 60 * 12,
                'enabled' => !isset($request->getAcceptableContentTypes()['text/html']),
                'variations' => [
                    $request->get('id'),
                ],
            ],

What's expected?

Get cache

What do you get instead?

PHP Warning – yii\base\ErrorException

mb_strlen() expects parameter 1 to be string, array given
1. in D:\projects\metering\yii2-app-advanced\vendor\yiisoft\yii2-redis\Connection.php at line 388
379380381382383384385386387388389390391392393394395396397     * @throws Exception for commands that return [error reply](http://redis.io/topics/protocol#error-reply).
     */
    public function executeCommand($name, $params = [])
    {
        $this->open();

        array_unshift($params, $name);
        $command = '*' . count($params) . "\r\n";
        foreach ($params as $arg) {
                $command .= '$' . mb_strlen($arg, '8bit') . "\r\n" . $arg . "\r\n";
        }

        \Yii::trace("Executing Redis Command: {$name}", __METHOD__);
        fwrite($this->_socket, $command);

        return $this->parseResponse(implode(' ', $params));
    }

    /**
2. yii\base\ErrorHandler::handleError(2, 'mb_strlen() expects parameter 1 ...', 'D:\projects\metering\yii2-app-ad...', 388, ...)
3. in D:\projects\metering\yii2-app-advanced\vendor\yiisoft\yii2-redis\Connection.php at line 388 – mb_strlen(['...', null], '8bit')
382383384385386387388389390391392393394    {
        $this->open();

        array_unshift($params, $name);
        $command = '*' . count($params) . "\r\n";
        foreach ($params as $arg) {
                $command .= '$' . mb_strlen($arg, '8bit') . "\r\n" . $arg . "\r\n";
        }

        \Yii::trace("Executing Redis Command: {$name}", __METHOD__);
        fwrite($this->_socket, $command);

        return $this->parseResponse(implode(' ', $params));
4. in D:\projects\metering\yii2-app-advanced\vendor\yiisoft\yii2-redis\Cache.php at line 130 – yii\redis\Connection::executeCommand('SET', ['SET', 'cache_yandex_map_6a86bc5cb2312e2...', ['...', null], 'PX', ...])
124125126127128129130131132133134135136    {
        if ($expire == 0) {
            return (bool) $this->redis->executeCommand('SET', [$key, $value]);
        } else {
            $expire = (int) ($expire * 1000);

            return (bool) $this->redis->executeCommand('SET', [$key, $value, 'PX', $expire]);
        }
    }

    /**
     * @inheritdoc
     */
5. in D:\projects\metering\yii2-app-advanced\vendor\yiisoft\yii2\caching\Cache.php at line 220 – yii\redis\Cache::setValue('cache_yandex_map_6a86bc5cb2312e2...', ['...', null], 43200000)
6. in D:\projects\metering\yii2-app-advanced\vendor\yiisoft\yii2\widgets\FragmentCache.php at line 113 – yii\caching\Cache::set('cache_yandex_map_6a86bc5cb2312e2...', ['...', null], 43200, null)
7. in D:\projects\metering\yii2-app-advanced\vendor\yiisoft\yii2\base\Widget.php at line 73 – yii\widgets\FragmentCache::run()
8. in D:\projects\metering\yii2-app-advanced\vendor\yiisoft\yii2\base\View.php at line 466 – yii\base\Widget::end()
9. in D:\projects\metering\yii2-app-advanced\vendor\yiisoft\yii2\filters\PageCache.php at line 220 – yii\base\View::endCache()
10. yii\filters\PageCache::cacheResponse(yii\base\Event)
11. in D:\projects\metering\yii2-app-advanced\vendor\yiisoft\yii2\base\Component.php at line 541 – call_user_func([yii\filters\PageCache, 'cacheResponse'], yii\base\Event)
12. in D:\projects\metering\yii2-app-advanced\vendor\yiisoft\yii2\web\Response.php at line 315 – yii\base\Component::trigger('afterSend')
13. in D:\projects\metering\yii2-app-advanced\vendor\yiisoft\yii2\base\Application.php at line 381 – yii\web\Response::send()
14. in D:\projects\metering\yii2-app-advanced\backend\web\index.php at line 17 – yii\base\Application::run()
11121314151617    require(__DIR__ . '/../../common/config/main-local.php'),
    require(__DIR__ . '/../config/main.php'),
    require(__DIR__ . '/../config/main-local.php')
);

$application = new yii\web\Application($config);
$application->run();

Additional info

Btw, When 'serializer' => false, for FileCache it works fine

 'pageCache' => [
                'class' => 'yii\filters\PageCache',
                'cache' => [
                    'class' => FileCache::class,
                    'keyPrefix' => 'cache_yandex_map_',
                    'serializer' => false,                   
                ],
                'only' => ['static-map'],
                'duration' => 60 * 60 * 12,
                'enabled' => !isset($request->getAcceptableContentTypes()['text/html']),
                'variations' => [
                    $request->get('id'),
                ],
            ],

XDebug:

image

Q A
YiiRedis vesion "yiisoft/yii2": "2.0.8", "yiisoft/yii2-redis": "2.0.5",
PHP version 7.0
Operating system Win

Yii2 - Reddis - Backend session gives null data

What steps will reproduce the problem?

I have used redis

` 'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],

    'cache' => [
        'class' => 'yii\redis\Cache',
        'redis' => [
            'hostname' => 'localhost',
            'port' => 6379,
            'database' => 0,
        ]
    ],
    'session' => [
        'class' => 'yii\redis\Session',
        'redis' => [
            'hostname' => 'localhost',
            'port' => 6379,
            'database' => 0,
        ]
    ],  `

I have used https://github.com/nenad-zivkovic/yii2-advanced-template Yii2 Template.

Sessions in frontend working good i get all the data stored in sessions.

But same session is not working in backend.

Ex. : User Logged in frontend , when i try to fetch user_id with Yii::$app()->user->id , i get correct value in frontend. But i do get null value in backend. at the same time.. if there are two different tabs in browser open. frontend gives correct user_id but backedn gives null data.

What's expected?

Backedn session nshould give same data. Like Yii::$app()->user->id should give correct value.

What do you get instead?

null value.

Additional info

Q A
Yii vesion "yiisoft/yii2": "2.*",
PHP version PHP Version 5.6.20
Operating system Windows 10
Redis-x64-3.2.100.msi

Windows 10,
Redis-x64-3.2.100.msi for windows,
PHP Version 5.6.20
XAMPP Control Panel Version: 3.2.2

Bug - STREAM_CLIENT_PERSISTENT with different databases

Hi, we have bug in yii2-redis extension.

If we are using 2 cases together:

  • Persistent connect - STREAM_CLIENT_PERSISTENT
  • More than one database

Bug is:
Session write in db - 2 - reads from db - 0

Config example:

'session' => [
    'class' => yii\redis\Session::class,
    'redis' => [
        'database' => 0,
        'socketClientFlags' => STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT
    ]
],

'redisCache' => [
    'class' => \yii\redis\Cache::class,
    'redis' => [
        'database' => 2,
        'socketClientFlags' => STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT
    ]
],

If we are using persistant socket, php will not create a new one for another database.

public function open()
{
...
$this->_socket = @stream_socket_client(
    $this->unixSocket ? 'unix://' . $this->unixSocket : 'tcp://' . $this->hostname . ':' . $this->port,
    $errorNumber,
    $errorDescription,
    $this->connectionTimeout ? $this->connectionTimeout : ini_get('default_socket_timeout'),
    $this->socketClientFlags
);
....
$this->executeCommand('SELECT', [$this->database]);

writeSession will save data in last selected DB

public function writeSession($id, $data)
{
    return (bool) $this->redis->executeCommand('SET', [$this->calculateKey($id), $data, 'EX', $this->getTimeout()]);
}

executeCommand will take the same socket, that was created last. You are using

$this->executeCommand('SELECT', [$this->database]);

only for opening. But you need to use it in:

public function executeCommand($name, $params = [])

Bug in 2.05 version

Failed to connect server always

exception 'yii\base\ErrorException' with message 'fwrite() expects parameter 1 to be resource, null given' in /var/www/vendor/yiisoft/yii2-redis/Connection.php:392

When I revert version to 2.04 it started to work again

Use predis to support replication and Sentinel

Do we want to add a new connection class, that will use predis (as e.g. laravel does) to support such features as replication and sentinel support? Or it could be set of abstract connection class with common things and two inherited to implement basic socket connection and predis connection.
I think it's a good step to HA.
Though sentinel support is still not in the stable predis branch, but it will soon, i think.

Bug in \yii\redis\Connection (it closes connection after serialize)

This issue has originally been reported by @IStranger at yiisoft/yii2#13084.
Moved here by @cebe.


What steps will reproduce the problem?

Enable Redis cache in config:

'components' => [
    'cache' => [
        'class' => 'yii\redis\Cache',
    ],
],

Then execute:

error_reporting(E_ERROR);

Yii::$app->cache->set('someKey', 'someValue');

$print = VarDumper::export(Yii::$app->cache);        // This will close redis connection
// $print = serialize(Yii::$app->cache);                // This will close redis connection
// $print = var_export(Yii::$app->cache, true);         // This won't close redis connection

$cachedValue = Yii::$app->cache->get('someKey');        // This won't work at closed redis connection:

//   error_reporting(E_ERROR) :: >> Database Exception – yii\db\Exception::: Failed to read from socket. Redis command was: GET keyPrefix:someKey
//   error_reporting(E_ALL)   :: >> PHP Warning – yii\base\ErrorException::: fwrite() expects parameter 1 to be resource, null given

What is the expected result?

Redis connection closes connection at serializing. This is normal behavior (see \yii\redis\Connection::__sleep).
However after serializing cache component won't work. I think redis connection should be restored.

Additional info

Q A
Yii version 2.0.9
PHP version 5.5.34
Operating system Mac OS X 10.11.5

integer-string-integer pks bug

Hi, i found a bug. Example:

class Bet extends \yii\redis\ActiveRecord
{
    public static function primaryKey()
    {
        return [
                   'game_id', //int
                   'stage_id', //string
                   'user_id' //int
       ];
    }
    // some code
}

        $bet1 = new Bet();
        $bet1->game_id = 1;
        $bet1->user_id = 1;
        $bet1->stage_id = 'flop';
        $bet1->sum = 0;
        $bet1->save();

        $bet2 = Bet::find()->where([
            'game_id' => 1,
            'user_id' => 1,
            'stage_id' => 'flop',
        ])->one();

        $bet2->sum = $bet2->sum + 10;

        $bet2->save(); // not saved, but return true

session_regenerate_id(): Session object destruction failed. ID: user (path: )

This issue has originally been reported by @EvgenyOrekhov at yiisoft/yii2#12821.
Moved here by @samdark.


session_regenerate_id(): Session object destruction failed. ID: user (path: ) after upgrade to 2.0.10.

What steps will reproduce the problem?

  1. Add this component config:

    'session' => [
        'class' => 'yii\redis\Session',
        'redis' => [
            'hostname' => 'localhost',
        ],
        'timeout' => 10,
    ],
    
  2. Log in to the application.

  3. Wait 10 seconds.

  4. Refresh the application page.

What is the expected result?

No warning.

What do you get instead?

PHP Warning – yii\base\ErrorException

session_regenerate_id(): Session object destruction failed. ID: user (path: )

  1. in vendor/yiisoft/yii2/web/Session.php at line 282
     * Please refer to <http://php.net/session_regenerate_id> for more details.
     * @param boolean $deleteOldSession Whether to delete the old associated session file or not.
     */
    public function regenerateID($deleteOldSession = false)
    {
        if ($this->getIsActive()) {
            // add @ to inhibit possible warning due to race condition
            // https://github.com/yiisoft/yii2/pull/1812
            if (YII_DEBUG && !headers_sent()) {
                session_regenerate_id($deleteOldSession); // <-- line 282
            } else {
                @session_regenerate_id($deleteOldSession);
            }
        }
    }

Additional info

Q A
Yii version 2.0.10
PHP version 7.0.12
Operating system Alpine Linux 3.4

Problems with Session handling

Hello,

I have some very bizarre problems, when I enable Redis session on my app, sometimes after some ajax requests (my page loads different contents with ajax about 10 to 20 ajax requests during page life) the app logs out alone, my app also has a system to switch users (it logs in another identity). All those things are working akwardly (sometimes the user is not switched, sometimes a log out, sometimes the user is switched during the ajax loads) when I use redis session. When I go back to file session none of these problems persists.

Have you any idea ?

Regards,
David

Redis error: ERR Protocol error: invalid bulk length Redis command was: hmset Array

with the demo usage:
$result = $redis->hmset(['test_collection', 'key1', 'val1', 'key2', 'val2']);
got the errors:
Database Exception – yii\db\Exception Redis error: ERR Protocol error: invalid bulk length Redis command was: hmset Array
fix the invoke codes with :
$result = $redis->hmset(['test_collection', 'key1', 'val1', 'key2', 'val2']);
seems work. If the documentation is wrong , please fix it.thx

ActiveRecord\Query Property has the Character Escape Bug.

class Country extends \yii\redis\ActiveRecord
{
    public static function primaryKey()
    {
        return [
                   'alpha2', // string
       ];
    }
    public function attributes()
    {
        return ['alpha2', 'fullname'];
    }
}

        $country = new Country();
        $country->alpha2 = 'CN';
        $country->fullname = "the People's Republic of China";
        $country->save(); // command error occurs.

The error is:

Redis error: ERR Error compiling script (new function): user_script:10: 'then' expected near 's' 
 Redis command was: EVAL local allpks=redis.call('LRANGE','country',0,-1)
 local pks={}
 local n=0
 local v=nil
 local i=0
 local key='country'
 for k,pk in ipairs(allpks) do
 local cfullname0=redis.call('HGET','country' .. ':a:' .. pk, 'fullname')

 if cfullname0=='the People\\'s Republic of China' then
 i=i+1
 if i>0 then
 do return redis.call('HGETALL','country:a:' .. pk) end
 end
 end
 end
 return pks 0

That's bug is from line 218 of LuaScriptBuilder.php (Version 2.0.4), the origin code is:

return "'" . addcslashes(str_replace("'", "\\'", $str), "\000\n\r\\\032") . "'";

I temporarily change it to:

return "'" . addcslashes($str, "\000\n\r\\\032\047") . "'";

then it works.

Fatal error on php 7 with yii/redis/Session enable

Redis return null if the key is missing, but readSession method must return string, otherwise session would close. So readSession($id) should changed like this ?

$data = $this->redis->executeCommand('GET', [$this->calculateKey($id)]);

return $data === false || $data === null ? '' : $data;

'not' and 'between' condition bugs in lua

        $q = JobRedis::find()->where(['between', 'id','5','10'])->limit(3)->asArray()->all();
       or 
        $q = JobRedis::find()->where(['between', 'id',5,10])->limit(3)->asArray()->all();

throw

[yii\db\Exception] Redis error: ERR Error running script (call to f_1c2ab0bf45b4b90789a37bff783c091c63700408): @user_script:10: user_script:10: attempt to compare string with boolean

Redis command was: EVAL local allpks=redis.call('LRANGE','job_redis',0,-1)
local pks={}
local n=0
local v=nil
local i=0
local key='job_redis'
for k,pk in ipairs(allpks) do
    local cid0=redis.call('HGET','job_redis' .. ':a:' .. pk, 'id')

    if cid0 >= '5' and cid0 <= '10' then
      i=i+1
      if i>0 and i<=3 then
        n=n+1 pks[n]=redis.call('HGETALL','job_redis:a:' .. pk)
      end
    end
end
return pks 0  
        $q = JobRedis::find()->where(['not', ['id'=>'5']])->limit(3)->asArray()->all();
        or         $q = JobRedis::find()->where(['not', ['id'=>5]])->limit(3)->asArray()->all();
        or         $q = JobRedis::find()->where(['not', ['id'=>null]])->limit(3)->asArray()->all();

throw

 [yii\db\Exception] Redis error: ERR Error compiling script (new function): user_script:10: unexpected symbol near '!' 
Redis command was: EVAL local allpks=redis.call('LRANGE','job_redis',0,-1)
local pks={}
local n=0
local v=nil
local i=0
local key='job_redis'
for k,pk in ipairs(allpks) do
    local cid0=redis.call('HGET','job_redis' .. ':a:' .. pk, 'id')

    if !(cid0=='5') then
      i=i+1
      if i>0 and i<=3 then
        n=n+1 pks[n]=redis.call('HGETALL','job_redis:a:' .. pk)
      end
    end
end
return pks 0  

redis expire time issue

Hello, today I got a error
"Redis error: ERR invalid expire time in set

soon I found in Redis-Cache

$expire = (int) ($expire * 1000);

I set a long expire time(see a month), then I got this problem.there need a fix for this

Tests fails with yii2 master branch

What steps will reproduce the problem?

Update yii2 in the test environment to the latest master commit or let tests/bootstrap.php download files from yii2 repo (tests/ActiveRecordTestTrait.php, tests/BaseTestCase.php and tests/CacheTestCase.php).

What's expected?

All tests passes.

What do you get instead?

SS.........................E.............E....................... 65 / 73 ( 89%)
........

Time: 7.55 seconds, Memory: 12.00Mb

There were 2 errors:

1) yiiunit\extensions\redis\ActiveRecordTest::testFindNestedRelation
yii\base\InvalidParamException: yiiunit\extensions\redis\data\ar\Customer has no relation named "ordersWithItems".

/home/alex/www/yii2/yii2-redis-dev/yii2-redis/vendor/yiisoft/yii2/db/BaseActiveRecord.php:1124
/home/alex/www/yii2/yii2-redis-dev/yii2-redis/vendor/yiisoft/yii2/db/ActiveQueryTrait.php:196
/home/alex/www/yii2/yii2-redis-dev/yii2-redis/vendor/yiisoft/yii2/db/ActiveQueryTrait.php:163
/home/alex/www/yii2/yii2-redis-dev/yii2-redis/ActiveQuery.php:180
/home/alex/www/yii2/yii2-redis-dev/yii2/tests/framework/ar/ActiveRecordTestTrait.php:527
/home/alex/www/yii2/yii2-redis-dev/yii2/vendor/phpunit/phpunit/phpunit:36

Caused by
yii\base\UnknownMethodException: Calling unknown method: yiiunit\extensions\redis\data\ar\Customer::getordersWithItems()

/home/alex/www/yii2/yii2-redis-dev/yii2-redis/vendor/yiisoft/yii2/base/Component.php:285
/home/alex/www/yii2/yii2-redis-dev/yii2-redis/vendor/yiisoft/yii2/db/BaseActiveRecord.php:1121
/home/alex/www/yii2/yii2-redis-dev/yii2-redis/vendor/yiisoft/yii2/db/ActiveQueryTrait.php:196
/home/alex/www/yii2/yii2-redis-dev/yii2-redis/vendor/yiisoft/yii2/db/ActiveQueryTrait.php:163
/home/alex/www/yii2/yii2-redis-dev/yii2-redis/ActiveQuery.php:180
/home/alex/www/yii2/yii2-redis-dev/yii2/tests/framework/ar/ActiveRecordTestTrait.php:527
/home/alex/www/yii2/yii2-redis-dev/yii2/vendor/phpunit/phpunit/phpunit:36

2) yiiunit\extensions\redis\ActiveRecordTest::testAfterRefresh
TypeError: Argument 1 passed to PHPUnit_Framework_TestCase::onNotSuccessfulTest() must be an instance of Exception, instance of Error given, called in /home/alex/www/yii2/yii2-redis-dev/yii2/vendor/phpunit/phpunit/src/Framework/TestCase.php on line 851

/home/alex/www/yii2/yii2-redis-dev/yii2/vendor/phpunit/phpunit/phpunit:36

Additional info

Q A
Yii vesion 2.0.7 in vendor/ and master@edd6e71 in test env
PHP version PHP 7.0.4-7ubuntu2 (cli) ( NTS )
Operating system Ubuntu 16.04

session_decode can't override $_SESSION

public function writeSession($id, $data)
    {
        $tmp = $_SESSION;
//        unset($_SESSION);
//        session_start();
        session_decode($data);
        $newData = $_SESSION;
        $_SESSION = $tmp;
        $this->redis->executeCommand('SET', [$this->keyPrefix . $id, json_encode($newData), 'EX', $this->getTimeout()]);
        return true;
    }

i'm trying to override the session, but session_decode didn't work in this function. (i want to php and node.js share session in Redis ). Can you help me?

What happens when the cache expires?

Hi. I was wondering what happens when the cache is expired. Does Yii call the flush method? Are the values deleted from the cache.
For example, in an AWS Environment, with ElastiCache Redis, configures with Yii, does Yii delete the redis cache from aws when it has expired?

Thanks.

Restful resources pagination with [yii\redis\ActiveRecord] + [yii\data\ActiveDataProvider] problem

This issue has originally been reported by @phuczeero at yiisoft/yii2#12961.
Moved here by @samdark.


What steps will reproduce the problem?

Token.php

namespace app\models;

class Token extends \yii\redis\ActiveRecord
{
    public function attributes()
    {
        return ['id', 'token', 'user_id'];
    }
}

TokenController.php

namespace app\modules\v2\controllers;
use app\models\Token;

class TransferController extends \yii\rest\Controller
{
    public function actionIndex()
    {
        return new \yii\data\ActiveDataProvider([
            'query' => Token::find()->where([
                'user_id' => Yii::$app->user->id,
            ]),
        ]);
    }
}

What is the expected result?

X-Pagination-Current-Page →1
X-Pagination-Page-Count →2
X-Pagination-Per-Page →20
X-Pagination-Total-Count →30

What do you get instead?

X-Pagination-Current-Page →1
X-Pagination-Page-Count →0
X-Pagination-Per-Page →20
X-Pagination-Total-Count →0

Additional info

Q A
Yii version 2.0.10, master
PHP version 7.0.0
Operating system 64-bit Windows 7 Professional SP1

redis hashes as php array

I propose to explicitly define next methods in \yii\redis\Connection

public function hSet($key, array $data);
public function hMset($key, array $data);
public function hGetAll($key);

To work with redis hashes as php associative arrays
e.g.

#set
$redis->hMset('someKey', ['hello' => 'world', 'someKey' => 'someValue']);
#get
$redis->hGetAll('someKey');
#return ['hello' => 'world', 'someKey' => 'someValue'], instead of  ['hello', 'world', 'someKey', 'someValue']

Strict attribute types

This issue has originally been reported by @NaLLiFFuNT at yiisoft/yii2#11804.
Moved here by @cebe.


The problem

I work on api. I use RedisActiveRecord.
I do create request
{
stringValue : '123'
intValue : 20,
boolValue : true
}

I load AR model & save it.
So, model have integer validator for intValue & bool Validator for boolValue

when i do get request
I find model by Id from RedisDB & return $model->toArray().
the ploblem that after loading from DB intValue & boolValue become string '20' & '1'.

Suggestion

Maybe that is not a problem for PHP7. But at this momen i don't have ability to upgrade a php version. I guess, you can face the challenge with other DB & AR.
I'd suggest following solution.
Add method attributeTypes to BaseActiveRecord which return array in following format
[
'attributeName1'=> 'int'
'attributeName2'=> 'bool'
'attributeName3'=> 'string'
]

in __set method we have to verify type of value for attribute & bring it, if it's required.

Additional info

I saw similar solution at Laravel and i find it very usefull.

Q A
Yii version 2.0.5
PHP version 5.6
Operating system Ubuntu mate

pconnect support?

Does it support redis pconnect ? I read src code, it seems not support pconnect, do you have any plan to support it ?

ActiveRecord is slow when use where condition.

ActiveRecord is very flat and slow (if we have many records at redis hash) when we try get with where condition.
For example:

RedisModelAR::find()
->where(['login' => $login])
->one();

Because when go to findByPk use HGETALL.

What about rewrite logic at ActiveQuery with HMGET etc. for best performance ?
And fields(AR attributes) store as hash field redis(hmset) not json or serialize.

yii2-redis Can support the following function?

/**
     * @see keys()
     * @param   string  $pattern
     * @link    http://redis.io/commands/keys
     */
    public function getKeys( $pattern ) {}
/**
     * @see del()
     * @param   int|array   $key1
     * @param   string      $key2
     * @param   string      $key3
     * @return int Number of keys deleted.
     */
    public function delete( $key1, $key2 = null, $key3 = null ) {}

How to use for cluster

What steps will reproduce the problem?

no

What's expected?

Use for cluster

What do you get instead?

no

Additional info

Q A
Yii vesion 2.0.9
PHP version 5.6.28
Operating system windows

Connection class can't be extended

Hello recently i tried to extend Connection class and currently is not possible because the property that is used for streaming data have "private" visibility.

/**
 * @var resource redis socket connection
 */
private $_socket;

Also the method that is parsing server response is private, and cannot be overriden.

/**
 * @param string $command
 * @return mixed
 * @throws Exception on error
 */
private function parseResponse($command)
{

Im suggesting visibility of this property and method to be changed to 'protected'.

Redis pubsub support

This issue has originally been reported by @nineinchnick at yiisoft/yii2#1932.
Moved here by @cebe.


When (P)SUBSCRIBE is called in Redis a client is not supposed to execute any more commands except for (P)(UN)SUBSCRIBE.
He then should listen for incoming messages.

This can be achieved by making parseResponse() public. Also, either it has to return null on timeout instead of an exception or throw a different exception so it can be caught. Or at least one with a different code.

I require this in my queue implementation that I'm working on for yiisoft/yii2#492.

Check isActive when Redis server down

If i use Yii::$app->redis->open(); when redis server down. I want to switch to alternative DB. So i use Yii::$app->redis->open(); in try catch structure. After it, if i check on Yii::$app->redis->isActive it's true, even if redis not working(or not installed)

Redis connection can not be reopened after the remote server has closed it

What steps will reproduce the problem?

  1. Connect to remote server.
  2. Wait enough time so the remote server closes the connection on their side.
  3. Try to execute redis command.

What's expected?

The connection to be reopened.

What do you get instead?

Exception: fwrite(): send of 52 bytes failed with errno=32 Broken pipe

Additional info

Q A
Yii vesion 206
PHP version 5.6.10
Operating system Ubuntu 12.04

Setting unknown property: yii\redis\Connection::redis

Has any one seen this error before?

Setting unknown property: yii\redis\Connection::redis

I was following this tutorial which works fine if I build a new yii project from scratch ( https://www.my-yii.com/learn/view-episode/yii-2-real-time-chat-app-with-nodejs-socketio-and-redisio ). When I try and integrate it into my current project I get the above error following ajax form post.

Feel sure it's related to the application config somehow

public function actionTest() {
    if (\Yii::$app->request->post()) {

        $name = \Yii::$app->request->post('name');
        $message = \Yii::$app->request->post('message');


        return \Yii::$app->redis->executeCommand('PUBLISH', [
            'channel' => 'notification',
            'message' => yii\helpers\Json::encode(['name' => $name, 'message' => $message])
        ]);
    }

    return $this->render('test');

    // return json_encode( \Yii::$app->request->post() );

}

Config

'components' => [
    'redis' => [
        'class' => 'yii\redis\Connection',
        'redis' => [
            'hostname' => 'localhost',
            'port' => 6379,
            'database' => 0,
        ]
    ],

RedisActiveRecord expire time

ActiveRecord expire

RedisActiveRecod does not have ability to be stored with some expire time.

What's expected?

Add method to RedisActiveRecod ttl() wich return time to live value in seconds. Every time when we will save or update model, we will apply expire command with this value to AR's keys. If ttl() method return false expire command doesn't have to be applied.

yii\redis\Connection lost socket connection on timeout

This issue has originally been reported by @agorobets at yiisoft/yii2#5876.
Moved here by @cebe.


I've got this error in forked process, when process tried execute redis command after $connectionTimeout seconds:

'yii\base\ErrorException' with message 'fwrite(): send of 52 bytes failed with errno=32 Broken pipe' in ./vendor/yiisoft/yii2-redis/Connection.php:361

That's happened, because there was no check on socket resource availability, in open() method. There is only check resource!==null.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.