To install the first extended look at the redis
composer require yiisoft/yii2-redis
Add redis configuration in the configuration file
'Components' => [ .... 'Redis' => [ 'class' => 'Yii \ Redis \ Connection', 'hostname' => yourname, 'password' => yourPassword, 'Port' => 6379, // default port configuration change where other ports 'Database' => 0, // used in the first few DB ], 'redis_1' => [ 'class' => 'Yii \ Redis \ Connection', 'hostname' => yourname, 'password' => yourPassword, 'Port' => 6379,// default port configuration change where other ports 'Database' =>. 1, // first few DB used ], .... ]
Instructions:
// instantiate redis $ redis = Yii :: $ App -> redis_1; // which redis If a cluster redis redis not configured to use other DB
String:
// set the value of a string $ Redis -> SET ( 'Key', 111 ); // get the value of a string echo $ Redis -> GET ( 'Key'); // 111 // repeat SET $ Redis -> SET ( 'Key', 222); // 222
List:
// list // list data stored in $ Redis -> LPUSH ( 'List', 'JS' ); $ Redis -> LPUSH ( 'List', 'Python' ); $ Redis -> LPUSH ( 'List', 'PHP' ); // get all the values in the list $ Redis -> Lrange ( 'list', 0, -1 ); // added to the right side from a $ Redis -> RPUSH ( 'list', 'MySQL' ) ; $ Redis -> Lrange ( 'List', 0, -1 ); // from the left pop $ Redis -> lpop ( 'List' ); $ Redis -> Lrange ( 'List', 0, -1 ) ; // from the right pop up a $ Redis -> RPOP ( 'list'); $redis->lrange('list', 0, -1);
HASH
// to hash table in a key set value // if not then set successfully, returns 1 if there will replace the original value, return 0, else return 0 $ Redis -> HSET ( 'hash', 'key' , 'key' ); // get the hash of a key value of $ Redis -> hget ( 'hash', 'key' ); // get hash all of the Keys $ Redis -> hkeys ( 'hash' ); // Get all the hash values in the order of random $ Redis -> hvals ( 'hash' ); // get all the key and hash value is a sequence of random $ Redis -> hgetall ( 'hash' ); / / Get the hash key number $ Redis -> hlen ( 'hash' ); // delete a key if the hash table key is not present or absence of false returns $ Redis -> HDEL ( 'hash', 'PHP');
Set: an unordered set
// add a value set in the key $ Redis -> Sadd ( 'key', 'value' ); // remove the key in the set value $ Redis -> Srem ( 'key', 'value');
Sorted Set: an ordered collection
// add a value set in the key $ Redis -> Zadd ( 'key', '. 1', 'value' ); // remove the key value in the set $ Redis -> zrem ( 'key', '. 1', 'value');
Note: The value of the collection will go to the weight of the list are not