Basic configuration:
In Yii’s main.php:
| |
Notes:
The CRedisCache in the class field is the official Redis plugin;
The password field is required if a password has been set;
The database field specifies the corresponding database.
Usage:
$r_key = “key”;
Yii::app()->redis_cache->set($r_key, 99999);
echo Yii::app()->redis_cache->get($r_key);
However, you won’t find a key named “key” in the Redis database, because Yii’s Redis plugin applies MD5 hashing to keys by default.
By examining CCache, the parent class of CRedisCache, you’ll find that you need to declare the following two variables in CRedisCache.php:
| |
This solves the problem.
Additionally, if you encounter garbled Chinese characters, it’s because Redis defaults to ANSI encoding. When connecting, use the following command:
./redis-cli –raw -h 127.0.0.1
to connect.
For official documentation, see: http://www.yiiframework.com/doc/api/1.1/CRedisCache/.