php redis Chinese manual

phpredis is an extension of php, which is quite efficient and has a linked list sorting function, which is useful for creating memory-level module business relationships

Very useful; the following are the command usage tips officially provided by redis:

The download address is as follows:

https://github.com/owlient/phpredis (support redis 2.0.4)


Redis::__construct constructor
$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, popen will not actively close the link.
Refer to the setOption above

to set the redis mode

getOption to view the mode set by redis

ping to view the connection status

get the value of a key (string value )
If the key does not exist, return false

set to write the key and value (string value)
If the write is successful, return true

setex write value with survival time
$redis->setex('key', 3600, 'value') ; // sets key → value, with 1h TTL.

setnx judges whether it is repeated, and writes the value
$redis->setnx('key', 'value');
$redis->setnx('key', 'value') ;

Delete a transaction return value multi(), return a redis object, and enter the multi-mode mode, once enter the multi-mode mode, all methods called later will return the same object, only until the exec() method is called. watch, unwatch (after the code is tested, the effect cannot be achieved)



























Monitor whether the value of a key has been changed by other programs. If the key is modified between watch and exec (method), the execution of this MULTI/EXEC transaction will fail (return false)
unwatch cancels all key parameters monitored by this program
, 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 *
publishes content to a certain channel. Note that this method may change in the future

exists
to determine whether the key exists. If there is a true value that is not in

the incr, incrBy
key, the value in the incrBy key will be auto-incremented by 1. If the second parameter is filled in, the value filled in the second parameter will be auto-incremented
$redis->incr('key1');
$redis-> incrBy('key1', 10);

decr, decrBy
to do subtraction, use the same method as incr

getMultiple
to pass
parameters An array composed of keys
Return parameters
If the key exists, return value, if it does not exist, return false
Output the first element from the left (head)/right (tail) of the list named key, delete the element blPop/brPop $redis->blPop('key1', 'key2', 10); block of the lpop command Version. That is, when the timeout is 0, if the name is key  i






















If the list does not exist or the list is empty, the command ends. If timeout>0, when encountering the above situation, wait for timeout seconds, if the problem is not solved, perform pop operation on the list starting from key i+1

lSize
$redis->lSize('key');
return the name of the key How many elements are there in the list

lIndex, lGet
$redis->lGet('key', 0);
Return the element at the index position in the list named key

lSet
$redis->lSet('key', 0, 'X');
Assign a value to the element at the index position in the list named key

lRange, lGetRange
$redis->lRange('key1', 0, -1);
return 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, keep the elements between start and end

lRem, lRemove
$redis->lRem('key ', 'A', 2);
Delete count elements whose value is value in the list named key. count is 0, delete all value elements, count>0 delete count elements with value from beginning to end, count<0 delete |count| elements with value from end to beginning lInsert in the name

of
key In the list, find the value whose value is pivot  , and determine according to the parameter Redis::BEFORE | Redis::AFTER, whether newvalue is placed before or after pivot. If the key does not exist, it will not be inserted. If the 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->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
adds element value to the set named key, if value exists, do not write, return false
$redis->sAdd(key, value);

sRem,sRemove
deletes 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
moves the value element from the collection named srckey to the collection named dstkey
$redis->sMove(seckey, dstkey, value);

sIsMember, sContains
name is Check whether there is a value element in the set of keys, true or not false
$redis->sIsMember(key, value);

sCard, sSize
returns the number of elements in the set named key

sPop
randomly returns and deletes one of the set named key The element

sRandMember
randomly returns an element in the set named key without deleting

the sInter
intersection set

sInterStore
to seek the intersection set and save the intersection set to the output set
$redis->sInterStore('output', 'key1', 'key2', 'key3' )

sUnion
seek union
$redis->sUnion('s0', 's1', 's2');
s0, s1, s2 seek union at the same time

sUnionStore
seek union and save the union to the output set
$redis->sUnionStore('output', 'key1', 'key2', 'key3');

sDiff
finds the difference set

sDiffStore
finds the difference set and saves the difference set to the output set

sMembers, sGetMembers
returns the set named key All elements

are sorted
, paginated, etc.
Parameters
'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 returns the value in
 
the
original
key , and write value to 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 followed by 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 (the method does not exist)
change the characters between start and end in the key string to value
$redis->set('key', 'Hello world');
$redis->setRange('key', 6, "redis");
$redis->get('key');

strlen
gets the string of the key Length
$redis->strlen('key');

getBit/setBit
returns binary information

zset ( sorted set ) operation related
zAdd ( key, score, member ) : add element member to zset named key, score is used for Sort. If the element already exists, update the order of the element 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 ,: Return all elements with index from start to end in zset named key (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 ) : Return all elements with index from start to end in the zset named key (elements have been sorted by score from large to small).withscores : whether to output the value of socre, the default is false, do not 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 zCount $redis->zCount
in the zset named key with score >= star and score <= end (key, star, end); Return the number of all elements with score >= star and score <= end in zset named key zRemRangeByScore, zDeleteRangeByScore $redis->zRemRangeByScore('key', star, end); delete name score >= star and score < in the zset for the key= all elements of end, returns the number of deleted zSize, zCard returns the number of all elements of the zset named key












zScore
$redis->zScore(key, val2); returns the score zRank, zRevRank $redis->zRevRank(key, val)
of the element val2 in the zset named key ; returns the zset named key (elements have been scaled from small to The rank (ie index, starting from 0) of the val element in the big sort), if there is no val element, return "null". zRevRank is sorted from large to small zIncrBy $redis->zIncrBy('key', increment, 'member'); if there is already an element member in the zset named key, then the score of the element increases by increment; otherwise, add to the set Add this element, whose score value is increment zUnion/zInter parameter keyOutput arrayZSetKeys arrayWeights aggregateFunction  Either "SUM", "MIN", or "MAX": defines the behavior 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 collection, before performing the AGGREGATE operation, it must be multiplied by the corresponding WEIGHT parameter. If WEIGHT is not provided, it defaults to 1. The default AGGREGATE is SUM, that is, the score of the elements in the result set is the value of the SUM operation of all corresponding elements in the set, while MIN and MAX mean that the score of the elements in the result set is the minimum and maximum values ​​among all the corresponding elements of the set.

Hash operation
hSet
$redis->hSet('h', 'key1', 'hello');
Add element key1 to the hash named h—>hello

hGet
$redis->hGet('h', 'key1') ;
Return the value corresponding to key1 in the hash named h (hello)

hLen
$redis->hLen('h');
Return the number of elements in the hash named h

hDel
$redis->hDel('h', 'key1 ');
Delete the domain

hKeys with key1 in the hash named h
$redis->hKeys('h'); Return all keys hVals
in the hash named key $redis->hVals('h'







Return all the keys (fields) in the hash named h and their corresponding values

​​hExists
$redis->hExists('h', 'a'); whether there is a field hIncrBy $
with the key name a in the hash named h 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)); Batch add elements hMGet to the hash named key $redis->hmGet('h', array('field1', 'field2')); Return the hash named h The value corresponding to field1 and field2 in the redis operation is related to flushDB to clear the current database flushAll to clear all databases randomKey randomly returns a key in the key space $key = $redis->randomKey();select selects a database move transfers 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
to rename the key
$redis->set('x', '42');
$redis->rename(' x', 'y');
$redis->get('y'); // → 42
$redis->get('x'); // → `FALSE`

renameNx
is similar to remane, but if rename The name already exists and will not be replaced successfully.

setTimeout, expire
sets 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
returns all keys that satisfy the given pattern
$keyWithUserPrefix = $redis->keys('user*');

dbSize
to see how many keys the database has now
$count = $redis->dbSize();

auth
password authentication
$redis->auth('foobared');

bgrewriteaof
uses aof for database persistence
$redis->bgrewriteaof();

slaveof
selects slave server
$redis->slaveof ('10.0.1.7', 6379);

save
saves data to disk synchronously

bgsave
saves data to disk asynchronously

lastSave
returns the Unix timestamp of the last time data was successfully saved to disk

info
returns redis version information and other details



type
returns key Type value
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
Collect learning routes & notes

Guess you like

Origin blog.csdn.net/2301_77162959/article/details/130971329