Answer the question
In order to leave comments, you need to log in
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,
]
],
...
// 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]
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
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
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 questionAsk a Question
731 491 924 answers to any question