$redis = new Redis();
connect, open link redis service
parameter
host: string, service address
port: int, port number
timeout: float, link duration (optional, default is 0, unlimited link time)
Note: There is also time in redis.conf, the default is 300
pconnect, links that popen will not actively close
Refer to above
setOption set redis mode
getOption View the mode set by redis
ping view connection status
get get the value of a key (string value)
If the key does not exist, return false
set write key and value (string value)
If the writing is successful, return ture
setex write value with time to live
$redis->setex('key', 3600,'value');//sets key → value, with 1h TTL.
setnx judge whether it is repeated, write the value
$redis->setnx('key','value');
$redis->setnx('key','value');
delete delete the value of the specified key
Returns the number of deleted keys (long integer)
$redis->delete('key1','key2');
$redis->delete(array('key3','key4','key5'));
ttl
Get the lifetime of a key
persist
Remove the key whose lifetime has expired
True if the key expires false if not expired
mset (only available for redis version 1.1 and above)
Assign values to multiple keys at the same time
$redis->mset(array('key0' =>'value0','key1' =>'value1'));
multi, exec, discard
Enter or exit transaction mode
The parameter can be Redis::MULTI or Redis::PIPELINE. The default is Redis::MULTI
Redis::MULTI: Treat multiple operations as one transaction
Redis::PIPELINE: Make (multiple) commands simpler and faster to send to the server, but there is no guarantee of atomicity
discard: delete a transaction
return value
multi(), returns a redis object and enters multi-mode mode. Once it enters multi-mode mode, all methods called in the future will return the same object, only until the exec() method is called.
watch, unwatch (After the code is tested, the said effect cannot be achieved)
Monitor whether the value of a key is changed by other programs. If this key is modified between watch and exec (method), the execution of this MULTI/EXEC transaction will fail (return false)
unwatch cancels all keys monitored by this program
Parameters, a list of a pair of keys
$redis->watch('x');
$ret = $redis->multi() ->incr('x') ->exec();
subscribe *
Method callback. Note that this method may change in the future
publish *
Publish content to a certain channel. Note that this method may change in the future
exists
Determine whether the key exists. There is true but not false
incr, incrBy
The value in the key is incremented by 1, if the second parameter is filled in, the value filled in the second parameter will be incremented
$redis->incr('key1');
$redis->incrBy('key1', 10);
decr, decrBy
Do subtraction, use the same method as incr
getMultiple
Passage
Array of keys
Return parameter
If the key exists, return value, if it does not exist, return false
$redis->set('key1','value1'); $redis->set('key2','value2'); $redis->set('key3','value3'); $redis->getMultiple (array('key1','key2','key3'));
$redis->lRem('key1','A', 2);
$redis->lRange('key1', 0, -1);
List related operations
lPush
$redis->lPush(key, value);
Add an element with value to the left (head) of the list named key
rPush
$redis->rPush(key, value);
Add an element with value to the right (at the end) of the list named key
lPushx/rPushx
$redis->lPushx(key, value);
Add an element with value to the left (head)/right (tail) of the list named key, if value already exists, don't add it
lPop/rPop
$redis->lPop('key');
Output the first element from the left (head)/right (tail) of the list named key, and delete this element
blPop/brPop
$redis->blPop('key1','key2', 10);
The block version of the lpop command. That is, when timeout is 0, if the list named key i does not exist or the list is empty, the command ends. If timeout>0, when the above situation is encountered, wait for timeout seconds, if the problem is not solved, perform a pop operation on the list starting with keyi+1
lSize
$redis->lSize('key');
Return how many elements are in the list named key
lIndex, lGet
$redis->lGet('key', 0);
Returns the element at index position in the list named key
lSet
$redis->lSet('key', 0,'X');
Assign value to the element at index position in the list named key
lRange, lGetRange
$redis->lRange('key1', 0, -1);
Returns the elements between start and end in the list named key (end is -1, return all)
lTrim, listTrim
$redis->lTrim('key', start, end);
Intercept the list named key, and keep the elements between start and end
lRem, lRemove
$redis->lRem('key','A', 2);
Delete count elements with value in the list named key. count is 0, delete all elements with value, count>0 delete count elements with value from the beginning to the end, count<0 delete |count| elements with value from end to beginning
lInsert
In the list named key, find the value of pivot, and determine it according to the parameter Redis::BEFORE | Redis::AFTER. Newvalue is placed before or after pivot. If the key does not exist, it will not be inserted, if pivot does not exist, return -1
$redis->delete('key1'); $redis->lInsert('key1', Redis::AFTER,'A','X'); $redis->lPush('key1','A'); $redis->lPush('key1','B'); $redis->lPush('key1','C'); $redis->lInsert('key1', Redis::BEFORE,'C', ' X');
$redis->lRange('key1', 0, -1);
$redis->lInsert('key1', Redis::AFTER,'C','Y');
$redis->lRange('key1', 0, -1);
$redis->lInsert('key1', Redis::AFTER,'W','value');
rpoplpush
Return and delete the last element of the list named srckey, and add this element to the head of the list named dstkey
$redis->delete('x','y');
$redis->lPush('x','abc'); $redis->lPush('x','def'); $redis->lPush('y', '123'); $redis->lPush ('y', '456');//move the last of x to the front of y. var_dump($redis->rpoplpush('x','y'));
var_dump($redis->lRange('x', 0, -1));
var_dump($redis->lRange('y', 0, -1));
string(3) "abc"
array(1) {[0]=> string(3) "def"}
array(3) {[0]=> string(3) "abc" [1]=> string(3) "456" [2]=> string(3) "123"}
SET operation related
sAdd
Add element value to the set named key, if value exists, don’t write it, return false
$redis->sAdd(key, value);
sRem, sRemove
Delete the element value in the set named key
$redis->sAdd('key1','set1');
$redis->sAdd('key1','set2');
$redis->sAdd('key1','set3');
$redis->sRem('key1','set2');
sMove
Move the value element from the collection named srckey to the collection named dstkey
$redis->sMove(seckey, dstkey, value);
sIsMember, sContains
Find if there is a value element in the set named key, there is true but not false
$redis->sIsMember(key, value);
sCard, sSize
Returns the number of elements in the set named key
sPop
Randomly return and delete an element in the set named key
sRandMember
Randomly return an element in the set named key, without deleting
sInter
Find the intersection
sInterStore
Find the intersection and save the intersection to the output set
$redis->sInterStore('output','key1','key2','key3')
sUnion
Find the union
$redis->sUnion('s0','s1','s2');
s0, s1, s2 find the union at the same time
sUnionStore
Find the union and save the union to the set of output
$redis->sUnionStore('output','key1','key2','key3');
sDiff
Difference set
sDiffStore
Find the difference set and save the difference set to the output set
sMembers, sGetMembers
Return all elements of the set named key
sort
Sorting, paging, etc.
parameter
'by' =>'some_pattern_*',
'limit' => array(0, 1),
'get' =>'some_other_pattern_*' or an array of patterns,
'sort' =>'asc' or'desc',
'alpha' => TRUE,
'store' =>'external-key'
example
$redis->delete('s'); $redis->sadd('s', 5); $redis->sadd('s', 4); $redis->sadd('s', 2); $redis->sadd('s', 1); $redis->sadd('s', 3);
var_dump($redis->sort('s'));//1,2,3,4,5
var_dump($redis->sort('s', array('sort' =>'desc')));//5,4,3,2,1
var_dump($redis->sort('s', array('sort' =>'desc','store' =>'out')));//(int)5
string command
getSet
Return the value in the original key, and write the value to the key
$redis->set('x', '42');
$exValue = $redis->getSet('x','lol');//return '42', replaces x by'lol'
$newValue = $redis->get('x')'//return'lol'
append
string, the value of the string named key is appended with value
$redis->set('key','value1');
$redis->append('key','value2');
$redis->get('key');
getRange (method does not exist)
Returns the characters between start and end in the string named key
$redis->set('key','string value');
$redis->getRange('key', 0, 5);
$redis->getRange('key', -5, -1);
setRange (method does not exist)
Change the character between start and end in the key string to value
$redis->set('key','Hello world');
$redis->setRange('key', 6, "redis");
$redis->get('key');
strlen
Get the length of the key string
$redis->strlen('key');
getBit/setBit
Return binary information
zset (sorted set) operation related
zAdd(key, score, member): Add the element member to the zset named key, and score is used for sorting. If the element already exists, the order of the element is updated according to the score.
$redis->zAdd('key', 1,'val1');
$redis->zAdd('key', 0,'val0');
$redis->zAdd('key', 5,'val5');
$redis->zRange('key', 0, -1);//array(val0, val1, val5)
zRange(key, start, end,withscores): Returns all elements whose index is from start to end in the zset named key (the elements have been sorted by score from small to large)
$redis->zAdd('key1', 0,'val0');
$redis->zAdd('key1', 2,'val2');
$redis->zAdd('key1', 10,'val10');
$redis->zRange('key1', 0, -1);//with scores $redis->zRange('key1', 0, -1, true);
zDelete, zRem
zRem(key, member): delete the element member in the zset named key
$redis->zAdd('key', 0,'val0');
$redis->zAdd('key', 2,'val2');
$redis->zAdd('key', 10,'val10');
$redis->zDelete('key','val2');
$redis->zRange('key', 0, -1);
zRevRange(key, start, end,withscores): Returns all elements with index from start to end in the zset named key (the elements are sorted by score from large to small). Withscores: whether to output the value of socre, the default is false, No output
$redis->zAdd('key', 0,'val0');
$redis->zAdd('key', 2,'val2');
$redis->zAdd('key', 10,'val10');
$redis->zRevRange('key', 0, -1);//with scores $redis->zRevRange('key', 0, -1, true);
zRangeByScore, zRevRangeByScore
$redis->zRangeByScore(key, star, end, array(withscores, limit ));
Return all elements in the zset named key that have score >= star and score <= end
zCount
$redis->zCount(key, star, end);
Returns the number of all elements in the zset named key with score >= star and score <= end
zRemRangeByScore, zDeleteRangeByScore
$redis->zRemRangeByScore('key', star, end);
Delete all elements with score >= star and score <= end in the zset named key, and return the deleted number
zSize, zCard
Returns the number of all elements of the zset named key
zScore
$redis->zScore(key, val2);
Returns the score of element val2 in the zset named key
zRank, zRevRank
$redis->zRevRank(key, val);
Returns the rank (ie index, starting from 0) of the val element in the zset named key (the elements have been sorted by score from small to large). If there is no val element, return "null". zRevRank is sorted from largest to smallest
zIncrBy
$redis->zIncrBy('key', increment,'member');
If the element member already exists in the zset named key, the score of the element is increased by increment; otherwise, the element is added to the set, and the score value is increment
zUnion/zInter
parameter
keyOutput
arrayZSetKeys
arrayWeights
aggregateFunction Either "SUM", "MIN", or "MAX": defines the behaviour to use on duplicate entries during the zUnion.
Find the union and intersection of N zsets, and save the final set in dstkeyN. For the score of each element in the set, it must be multiplied by the WEIGHT parameter before performing the AGGREGATE operation. If WEIGHT is not provided, it defaults to 1. The default AGGREGATE is SUM, that is, the score of the element in the result set is the value of the SUM operation of all the corresponding elements in the set, and MIN and MAX mean that the score of the element in the result set is the minimum and maximum of all the corresponding elements in the set.
Hash operation
hSet
$redis->hSet('h','key1','hello');
Add the element key1—>hello to the hash named h
hGet
$redis->hGet('h','key1');
Returns the value (hello) corresponding to key1 in the hash named h
hLen
$redis->hLen('h');
Returns the number of elements in the hash named h
hDel
$redis->hDel('h','key1');
Delete the domain whose key is key1 in the hash named h
hKeys
$redis->hKeys('h');
Return all keys in the hash named key
hVals
$redis->hVals('h')
Returns the value corresponding to all keys in the hash named h
hGetAll
$redis->hGetAll('h');
Returns all the keys (fields) and their corresponding values in the hash named h
hExists
$redis->hExists('h','a');
Does the domain with the key name a exist in the hash with the name h?
hIncrBy
$redis->hIncrBy('h','x', 2);
Increase the value of x in the hash named h by 2
hMset
$redis->hMset('user:1', array('name' =>'Joe','salary' => 2000));
Add elements to the hash named key in batches
hMGet
$redis->hmGet('h', array('field1','field2'));
Returns the value corresponding to field1 and field2 in the hash named h
Redis operation related
flushDB
Clear the current database
flushAll
Clear all databases
randomKey
Randomly return a key in the key space
$key = $redis->randomKey();
select
Choose a database
move
Transfer a key to another database
$redis->select(0);//switch to DB 0
$redis->set('x', '42');//write 42 to x
$redis->move('x', 1);//move to DB 1
$redis->select(1);//switch to DB 1
$redis->get('x');//will return 42
rename, renameKey
Rename the key
$redis->set('x', '42');
$redis->rename('x','y');
$redis->get('y');//→ 42
$redis->get('x');//→ `FALSE`
renameNx
Similar to remane, but if the renamed name already exists, the replacement will not succeed
setTimeout, expire
Set the activity time of a key (s)
$redis->setTimeout('x', 3);
expireAt
Key survives to a unix timestamp time
$redis->expireAt('x', time() + 3);
keys, getKeys
Return all keys that meet the given pattern
$keyWithUserPrefix = $redis->keys('user*');
dbSize
Check how many keys the database currently has
$count = $redis->dbSize();
auth
Password authentication
$redis->auth('foobared');
bgrewriteaof
Use aof for database persistence
$redis->bgrewriteaof();
slaveof
Select slave server
$redis->slaveof('10.0.1.7', 6379);
save
Synchronously save data to disk
bgsave
Save data to disk asynchronously
lastSave
Returns the Unix timestamp of the last successful data saving to disk
info
Return redis version information and other details
type
Return the type value of the key
string: Redis::REDIS_STRING
set: Redis::REDIS_SET
list: Redis::REDIS_LIST
zset: Redis::REDIS_ZSET
hash: Redis::REDIS_HASH
other: Redis::REDIS_NOT_FOUND