Redis caching technology is generally used for some data that is frequently queried and does not change frequently. It can be queried from the database and stored in the redis cache (redis cache is stored in memory and can be maintained for a long time. ), the user directly reads the data in the cache when accessing, so that when the access volume and concurrency are large, the database query will not be slow. In actual projects, the data array of the database is generally converted into a json string, and set is used. The method saves and the get method obtains. Here are some ways to use redis.
$redis = new redis(); $result = $redis->connect('127.0.0.1', 6379); //$redis->auth('pt#1234@re'); //存在密码时链接 var_dump($result); //结果:bool(true) $redis->set('name',111); $redis->lpush('value',111); $redis->expire('name',10);//设置过期时间,不设置过去时间时,默认为永久保持 //$redis->expire('value',10);//不设置时,默认为-1,永久保持
1, connect
Description: The instance is connected to a Redis.
Parameters: host: string, port: int
Return value: BOOL Successful return: TRUE; Failure return: FALSE
Example:
<?php $redis = new redis(); $result = $redis->connect('127.0.0.1', 6379); var_dump($result); //结果:bool(true) ?>
2, set
Description: Set the value of key and value
Parameter: Key Value
Return value: BOOL Successful return: TRUE; Failure return: FALSE
Example:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $result = $redis->set('test',"11111111111"); var_dump($result); //结果:bool(true) ?>
3,get
Description: Get the value about the specified key
Parameter: key
Return value: string or BOOL If the key does not exist, Returns FALSE. Otherwise, return the value corresponding to the specified key.
Example:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $result = $redis->get('test'); var_dump($result); //结果:string(11) "11111111111" ?>
4,delete
Description: Delete the specified key
Parameters: a key, or an undetermined number of parameters, an array for each key: key1 key2 key3 ... keyN
Return value: the number of deleted items
Example:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->set('test',"1111111111111"); echo $redis->get('test'); //结果:1111111111111 $redis->delete('test'); var_dump($redis->get('test')); //结果:bool(false) ?>
5, setnx
Description: If the key does not exist in the database, set the key value parameter
Parameter: key value
Return value: BOOL Successful return: TRUE; Failure return: FALSE
Example:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->set('test',"1111111111111"); $redis->setnx('test',"22222222"); echo $redis->get('test'); //结果:1111111111111 $redis->delete('test'); $redis->setnx('test',"22222222"); echo $redis->get('test'); //结果:22222222 ?>
6, exists
Description: Verify whether the specified key is Existence
Parameter key
Return value: Bool Successful return: TRUE; Failure return: FALSE
Example:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->set('test',"1111111111111"); var_dump($redis->exists('test')); //结果:bool(true) ?>
7, incr
Description: Digital increment storage key value Key.
Parameters: key value: The value that will be added to the key
Return value: INT the new value
Example:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->set('test',"123"); var_dump($redis->incr("test")); //结果:int(124) var_dump($redis->incr("test")); //结果:int(125) ?>
8, decr
Description: Store key values numerically descending.
Parameters: key value: the value that will be added to the key
Return value: INT the new value
Example:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->set('test',"123"); var_dump($redis->decr("test")); //结果:int(122) var_dump($redis->decr("test")); //结果:int(121) ?>
9, getMultiple
Description: Get all Specifies the value of the key. If one or more keys do not exist, the value of that key in the array is false
Parameters: Array of lists containing the values of the keys
Return value: Returns an array containing the values of all keys
Example:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->set('test1',"1"); $redis->set('test2',"2"); $result = $redis->getMultiple(array('test1','test2')); print_r($result); //结果:Array ( [0] => 1 [1] => 2 ) ?>
10,lpush
Description: Add a string value from the head of the list. Create the list if the key does not exist. If the key exists and is not a list, return FALSE.
Parameters: key, value
Return value: Return the array length if successful, false if failed
Instance:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); var_dump($redis->lpush("test","111")); //结果:int(1) var_dump($redis->lpush("test","222")); //结果:int(2) ?>
11, rpush
Description: Add a string from the end of the list value. Create the list if the key does not exist. If the key exists and is not a list, return FALSE.
Parameters: key, value
Return value: Return the array length on success, false on failure
Example:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); var_dump($redis->lpush("test","111")); //结果:int(1) var_dump($redis->lpush("test","222")); //结果:int(2) var_dump($redis->rpush("test","333")); //结果:int(3) var_dump($redis->rpush("test","444")); //结果:int(4) ?>
12, lpop
Description: Return and remove the list The first element
Parameter: key
Return value: Return the value of the first element successfully, return false
Example:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); $redis->lpush("test","111"); $redis->lpush("test","222"); $redis->rpush("test","333"); $redis->rpush("test","444"); var_dump($redis->lpop("test")); //结果:string(3) "222" ?>
13,lsize,llen
Description: The length of the returned list. If the list does not exist or is empty, the command returns 0. If the key is not a list, this command returns FALSE.
Parameters: Key
Return value: Returns the array length on success, false on failure
Example:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); $redis->lpush("test","111"); $redis->lpush("test","222"); $redis->rpush("test","333"); $redis->rpush("test","444"); var_dump($redis->lsize("test")); //结果:int(4) ?>
14, lget
Description: Returns the specified key stored in the list specified Elements. 0 first element, 1 second... -1 last element, -2 second last... Returns FALSE if the wrong index or key does not point to the list.
Parameter: key index
Return value: Return the value of the specified element successfully, false on failure
Example:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); $redis->lpush("test","111"); $redis->lpush("test","222"); $redis->rpush("test","333"); $redis->rpush("test","444"); var_dump($redis->lget("test",3)); //结果:string(3) "444" ?>
15, lset
Description: The index specified for the list Assign a new value, if the index does not exist, return false.
Parameter: key index value
Return value: Return true if successful, false if failed
Example:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); $redis->lpush("test","111"); $redis->lpush("test","222"); var_dump($redis->lget("test",1)); //结果:string(3) "111" var_dump($redis->lset("test",1,"333")); //结果:bool(true) var_dump($redis->lget("test",1)); //结果:string(3) "333" ?>
16, lgetrange
Description:
Returns the specified element stored from start to end in the specified key list in the area, lGetRange(key, start, end). 0 the first element, 1 the second element... -1 the last element, -2 the penultimate element...
Parameter: key start end
Return value: Return the value found successfully, false on failure
Example:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); $redis->lpush("test","111"); $redis->lpush("test","222"); print_r($redis->lgetrange("test",0,-1)); //结果:Array ( [0] => 222 [1] => 111 ) ?>
17,lremove
Description: Remove count matching values from the head of the list. If count is zero, all matching elements are removed. If count is negative, the content is deleted from the end.
Parameter: key count value
Return value: Returns the number of deleted items if successful, false if failed
Example:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); $redis->lpush('test','a'); $redis->lpush('test','b'); $redis->lpush('test','c'); $redis->rpush('test','a'); print_r($redis->lgetrange('test', 0, -1)); //结果:Array ( [0] => c [1] => b [2] => a [3] => a ) var_dump($redis->lremove('test','a',2)); //结果:int(2) print_r($redis->lgetrange('test', 0, -1)); //结果:Array ( [0] => c [1] => b ) ?>
18, sadd
Description: Add a Key a value. If this value is already in this Key, return FALSE.
Parameter: key value
Return value: Return true on success, false on failure
Example:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); var_dump($redis->sadd('test','111')); //结果:bool(true) var_dump($redis->sadd('test','333')); //结果:bool(true) print_r($redis->sort('test')); //结果:Array ( [0] => 111 [1] => 333 ) ?>
19, sremove
Description: Delete the value specified in Key
Parameter: key member
Return value: true or false
Example:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); $redis->sadd('test','111'); $redis->sadd('test','333'); $redis->sremove('test','111'); print_r($redis->sort('test')); //结果:Array ( [0] => 333 ) ?>
20,smove
Description: Move the value in Key1 to Key2
Parameters: srcKey dstKey member
Return value: true or false
Example
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); $redis->delete('test1'); $redis->sadd('test','111'); $redis->sadd('test','333'); $redis->sadd('test1','222'); $redis->sadd('test1','444'); $redis->smove('test',"test1",'111'); print_r($redis->sort('test1')); //结果:Array ( [0] => 111 [1] => 222 [2] => 444 ) ?>
21,scontains
描述:检查集合中是否存在指定的值。
参数:key value
返回值:true or false
范例:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); $redis->sadd('test','111'); $redis->sadd('test','112'); $redis->sadd('test','113'); var_dump($redis->scontains('test', '111')); //结果:bool(true) ?>
22,ssize
描述:返回集合中存储值的数量
参数:key
返回值:成功返回数组个数,失败0
范例:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); $redis->sadd('test','111'); $redis->sadd('test','112'); echo $redis->ssize('test'); //结果:2 ?>
23,spop
描述:随机移除并返回key中的一个值
参数:key
返回值:成功返回删除的值,失败false
范例:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); $redis->sadd("test","111"); $redis->sadd("test","222"); $redis->sadd("test","333"); var_dump($redis->spop("test")); //结果:string(3) "333" ?>
24,sinter
描述:返回一个所有指定键的交集。如果只指定一个键,那么这个命令生成这个集合的成员。如果不存在某个键,则返回FALSE。
参数:key1, key2, keyN
返回值:成功返回数组交集,失败false
范例:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); $redis->sadd("test","111"); $redis->sadd("test","222"); $redis->sadd("test","333"); $redis->sadd("test1","111"); $redis->sadd("test1","444"); var_dump($redis->sinter("test","test1")); //结果:array(1) { [0]=> string(3) "111" } ?>
25,sinterstore
描述:执行sInter命令并把结果储存到新建的变量中。
参数:
Key: dstkey, the key to store the diff into.
Keys: key1, key2… keyN. key1..keyN are intersected as in sInter.
返回值:成功返回,交集的个数,失败false
范例:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); $redis->sadd("test","111"); $redis->sadd("test","222"); $redis->sadd("test","333"); $redis->sadd("test1","111"); $redis->sadd("test1","444"); var_dump($redis->sinterstore('new',"test","test1")); //结果:int(1) var_dump($redis->smembers('new')); //结果:array(1) { [0]=> string(3) "111" } ?>
26,sunion
描述:
返回一个所有指定键的并集
参数:
Keys: key1, key2, … , keyN
返回值:成功返回合并后的集,失败false
范例:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); $redis->sadd("test","111"); $redis->sadd("test","222"); $redis->sadd("test","333"); $redis->sadd("test1","111"); $redis->sadd("test1","444"); print_r($redis->sunion("test","test1")); //结果:Array ( [0] => 111 [1] => 222 [2] => 333 [3] => 444 ) ?>
27,sunionstore
描述:执行sunion命令并把结果储存到新建的变量中。
参数:
Key: dstkey, the key to store the diff into.
Keys: key1, key2… keyN. key1..keyN are intersected as in sInter.
返回值:成功返回,交集的个数,失败false
范例:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); $redis->sadd("test","111"); $redis->sadd("test","222"); $redis->sadd("test","333"); $redis->sadd("test1","111"); $redis->sadd("test1","444"); var_dump($redis->sinterstore('new',"test","test1")); //结果:int(4) print_r($redis->smembers('new')); //结果:Array ( [0] => 111 [1] => 222 [2] => 333 [3] => 444 ) ?>
28,sdiff
描述:返回第一个集合中存在并在其他所有集合中不存在的结果
参数:Keys: key1, key2, … , keyN: Any number of keys corresponding to sets in redis.
返回值:成功返回数组,失败false
范例:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); $redis->sadd("test","111"); $redis->sadd("test","222"); $redis->sadd("test","333"); $redis->sadd("test1","111"); $redis->sadd("test1","444"); print_r($redis->sdiff("test","test1")); //结果:Array ( [0] => 222 [1] => 333 ) ?>
29,sdiffstore
描述:执行sdiff命令并把结果储存到新建的变量中。
参数:
Key: dstkey, the key to store the diff into.
Keys: key1, key2, … , keyN: Any number of keys corresponding to sets in redis
返回值:成功返回数字,失败false
范例:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); $redis->sadd("test","111"); $redis->sadd("test","222"); $redis->sadd("test","333"); $redis->sadd("test1","111"); $redis->sadd("test1","444"); var_dump($redis->sdiffstore('new',"test","test1")); //结果:int(2) print_r($redis->smembers('new')); //结果:Array ( [0] => 222 [1] => 333 ) ?>
30,smembers, sgetmembers
描述:
返回集合的内容
参数:Key: key
返回值:An array of elements, the contents of the set.
范例:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->delete('test'); $redis->sadd("test","111"); $redis->sadd("test","222"); print_r($redis->smembers('test')); //结果:Array ( [0] => 111 [1] => 222 ) ?>
更多Redis相关知识,请访问Redis使用教程栏目!
The above is the detailed content of How to use redis cache. For more information, please follow other related articles on the PHP Chinese website!