K
K
Ken Jee2016-09-16 19:06:31
Yii
Ken Jee, 2016-09-16 19:06:31

Why is the key name hashed when trying to write a value to Redis via yii\redis\Cache?

Added the yii\redis\Cache extension to the Yii2 project via Composer and set its configuration

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

I'm trying to write an entity to Redis:
//  Yii::$app->user->identity->email содержит строку: [email protected]

Yii::$app->cache->set( Yii::$app->user->identity->email, 'my-value', 60 * 60 * 24 );

echo Yii::$app->user->identity->email; // Выводит: [email protected]

In Redis, the key name is written in the form 51ade67559f849edfa94159c13bc296f, but it should be [email protected]
What could be the problem?
When I try to use the following code, an error occurs
Yii::$app->cache->setValue( Yii::$app->user->identity->email, 'my-value', 60 * 60 * 24 );

Exception 'yii\base\UnknownMethodException' with message 'Calling unknown method: yii\redis\Cache::setValue()'
in E:\OpenServer\domains\laffu\vendor\yiisoft\yii2\base\Component.php:285
Stack trace:
#0 E:\OpenServer\domains\laffu\frontend\controllers\SiteController.php(40): yii\base\Component->__call('setValue', Array)
#1 [internal function]: frontend\controllers\ SiteController->actionIndex()
#2 E:\OpenServer\domains\laffu\vendor\yiisoft\yii2\base\InlineAction.php(55): call_user_func_array(Array, Array)
#3 E:\OpenServer\domains\laffu\vendor \yiisoft\yii2\base\Controller.php(154): yii\base\InlineAction->runWithParams(Array)
#4 E:\OpenServer\domains\laffu\vendor\yiisoft\yii2\base\Module.php(454) : yii\base\Controller->runAction('', Array)
#5 E:\OpenServer\domains\laffu\vendor\yiisoft\yii2\web\Application.php(87): yii\base\Module->runAction('', Array)
#6 E:\OpenServer\domains\laffu \vendor\yiisoft\yii2\base\Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
#7 E:\OpenServer\domains\laffu\frontend\web\index .php(30): yii\base\Application->run()
#8 {main}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2016-11-20
@Machez

the key is hashed and rightly so, it should not negatively affect your code in any way. Your code doesn't really care if the key is hashed or not.
And you get an error because there is no such setValue method in the yii\redis\Cache class, there is a set() method, which you probably need. The key hash has nothing to do with it.
Here is a description of all available methods: www.yiiframework.com/doc-2.0/yii-redis-cache.html

E
Elena Stepanova, 2016-09-16
@Insolita

Because it's a cache... the keys can be long, to optimize the keys >32 bytes long, or if suddenly there are non-standard characters there, they are encoded by md5
https://github.com/yiisoft/yii2/blob/master/framew...
If it is important not to code - inherit the yii\redis\Cache class, override the buldKey function with your own and use it. [but for caching query results, it's better not to do this, or make a separate component with this behavior]
But, you might be better off using redis directly if the keys are so important (for more compelling reasons than convenient lookup in the database)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question