It is very necessary to make so that Yii2 using the class \ yii \ redis \ Cache does not hash the names of the keys.

// prefix:keyname1 -> 0e46d28d5fb7676bffd12253c26aec56 Yii::$app->cache->set( 'prefix:keyname1', 'key-value-1, 60 * 60 * 24 ); // keyname2 -> keyname2 Yii::$app->cache->set( 'keyname2', 'key-value-2, 60 * 60 * 24 ); 

The essence of the problem lies in the fact that if the name of the key contains the characters ':', '@', etc. - the class \ yii \ redis \ Cache will hash these key names with the md5 algorithm ... How can I force \ yii \ redis \ Cache not to do this?

  • Redefine the class and rewrite the hash function as you like - Ninazu Sept

1 answer 1

You must overload the buildKey method of the Cache class.

For example, like this (for the basic template).

  1. Create the CustomRedisCache class in the components folder:

     <?php namespace app\components; class CustomRedisCache extends \yii\redis\Cache { public function buildKey($key) { return $key; // оставляем ключ как есть } } 
  2. In the configuration file, use this class instead of \yii\redis\Cache :

     'components' => [ 'cache' => [ 'class' => 'app\components\CustomRedisCache', ],