In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces what are the commands for the use of redis in PHP. It is very detailed and has a certain reference value. Friends who are interested must finish reading it!
Phpredis is an extension of php, the efficiency is quite high with linked list sorting function, for creating memory-level module business relationships.
Very useful; here are the official command usage tips provided by redis:
Download address is as follows:
Https://github.com/owlient/phpredis (supports redis 2.0.4)
Redis::__construct constructor
$redis = new Redis ()
Connect, open link redis service
Parameters.
Host: string, service address
Port: int, port number
Timeout: float, link duration (optional, default is 0, unlimited link time)
Note: time is also available in redis.conf. Default is 300.
Pconnect, links that popen will not actively close
Refer to the above
SetOption sets redis mode
GetOption views the mode set by redis
Ping to view connection status
Get gets the value of a key (string value)
If the key does not exist, return false
Set writes key and value (string value)
If the write is successful, return ture
Setex write value with time to live
$redis- > setex ('key', 3600,' value'); / / sets key → value, with 1h TTL.
Setnx determines whether it is duplicated or not, write the value
$redis- > setnx ('key',' value')
$redis- > setnx ('key',' value')
Delete deletes the value of the specified key
Returns the number of deleted key (long integers)
$redis- > delete ('key1',' key2')
$redis- > delete (array ('key3',' key4', 'key5'))
Ttl
Get the survival time of a key
Persist
Remove key whose lifetime expires
If key expires true if it doesn't expire false
Mset (only available with redis version 1.1 or above)
Assign multiple key values at the same time
$redis- > mset (array ('key0' = >' value0', 'key1' = >' value1'))
Multi, exec, discard
Enter or exit transaction mode
Parameters can be Redis::MULTI or Redis::PIPELINE. Default is Redis::MULTI
Redis::MULTI: execute multiple operations as one transaction
Redis::PIPELINE: make (multiple) commands easier 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 in 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 said effect can not 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 the MULTI/EXEC transaction will fail (return false)
Unwatch cancels all key monitored by this program
Parameter, list of a pair of key
$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 key exists. Exist true is not in false
Incr, incrBy
The value in key is incremented by 1. If the second parameter is filled in, the value filled in by the second parameter will be incremented.
$redis- > incr ('key1')
$redis- > incrBy ('key1', 10)
Decr, decrBy
Do subtraction, using the same method as incr
GetMultiple
Pass parameters
An array of key
Return parameters
Return value if key exists, false does not exist
$redis- > set ('key1',' value1'); $redis- > set ('key2',' value2'); $redis- > set ('key3',' value3'); $redis- > getMultiple (array ('key1',' key2', 'key3'))
$redis- > lRem ('key1',' Aids, 2)
$redis- > lRange ('key1', 0,-1)
List related operations
LPush
$redis- > lPush (key, value)
Add an element with the value value to the left (header) of the list named key
RPush
$redis- > rPush (key, value)
Add an element with the value value to the right (tail) of the list named key
LPushx/rPushx
$redis- > lPushx (key, value)
Add an element with the value value to the left (header) / right (tail) of the list named key, but not if value already exists
LPop/rPop
$redis- > lPop ('key')
Output the first element from the left (top) / right (tail) of the list with the name key and delete it
BlPop/brPop
$redis- > blPop ('key1',' key2', 10)
Block version of the lpop command. That is, when timeout is 0, if a list named key I does not exist or the list is empty, the command ends. If timeout > 0, wait for timeout seconds when you encounter the above situation. If the problem is not solved, then pop the list started by keyi+1.
LSize
$redis- > lSize ('key')
Returns how many elements there are in a list named key
LIndex, lGet
$redis- > lGet ('key', 0)
Returns the element of index location in the list with the name key
LSet
$redis- > lSet ('key', 0,' X')
Assign the element in the index position in the list named key to value
LRange, lGetRange
$redis- > lRange ('key1', 0,-1)
Returns the element between start and end in the list named key (end is-1, returns all)
LTrim, listTrim
$redis- > lTrim ('key', start, end)
Intercept the list named key and retain the elements between start and end
LRem, lRemove
$redis- > lRem ('key',' Aids, 2)
Delete the count element with the value value in the list named key. If count is 0, delete all elements with the value of value, and count > 0 delete all elements with the value of value from beginning to end, countdelete ('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,' centering,'X')
$redis- > lRange ('key1', 0,-1)
$redis- > lInsert ('key1', Redis::AFTER,' Clearing,'Y')
$redis- > lRange ('key1', 0,-1)
$redis- > lInsert ('key1', Redis::AFTER,' worth, 'value')
Rpoplpush
Return and delete the tail element of the list named srckey and add it to the header 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,'))
Var_dump ($redis- > lRange ('x, 0,-1))
Var_dump ($redis- > lRange ('yearly, 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"}
Related to SET operation
SAdd
Add the element value to the set named key, if value exists, do not write, 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 out if there is a value element in the collection named key, with ture and no false
$redis- > sIsMember (key, value)
SCard, sSize
Returns the number of elements of the set named key
SPop
Randomly returns and deletes an element in the set named key
SRandMember
Randomly returns an element in the set with the name key without deletion
SInter
Find the intersection
SInterStore
Find the intersection and save the intersection to the collection of output
$redis- > sInterStore ('output',' key1', 'key2',' key3')
SUnion
Union set
$redis- > sUnion ('s 0,'s 1,'s 2')
The simultaneous union set of s0memery S1 and S2.
SUnionStore
Join and save the union to the collection of output
$redis- > sUnionStore ('output',' key1', 'key2',' key3')
SDiff
Difference set
SDiffStore
Find the difference set and save the difference set to the set of output
SMembers, sGetMembers
Returns all elements of a set named key
Sort
Sort, page, 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'
Examples
$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'))
Var_dump ($redis- > sort ('sort', array (' sort' = > 'desc')
Var_dump ($redis- > sort ('desc', array (' sort' = > 'desc',' store' = > 'out')); / / (int) 5
String command
GetSet
Return the value from the original key and write value to key
$redis- > set ('xmom,' 42')
$exValue = $redis- > getSet ('xboys,' 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 string with the name 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 string of key to value
$redis- > set ('key',' Hello world')
$redis- > setRange ('key', 6, "redis")
$redis- > get ('key')
Strlen
Get the length of the string of key
$redis- > strlen ('key')
GetBit/setBit
Returns binary information
Related to zset (sorted set) operation
ZAdd (key, score, member): add the element member,score to the zset named key for sorting. If the element already exists, the order of the element is updated according to 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 of index from start to end in a zset with the name key (elements sorted by score from smallest to largest)
$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): deletes 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 of index from start to end in zset named key (elements sorted by score from largest to smallest). Withscores: whether to output the value of socre, default 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))
Returns score > = star and score zCount (key, star, end) in zset named key
Returns score > = star and score zRemRangeByScore ('key', star, end) in zset named key
Delete score > = star and score zScore (key, val2) from the zset named key
Returns the score of the element val2 in the zset named key
ZRank, zRevRank
$redis- > zRevRank (key, val)
Returns the rank (that is, index, starting with 0) of the val element in the zset named key (the elements have been sorted by score from smallest to largest), or "null" if there is no val element. 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 increases increment;. Otherwise, add the element to the collection, and its score value is increment.
ZUnion/zInter
Parameters.
KeyOutput
ArrayZSetKeys
ArrayWeights
AggregateFunction Either "SUM", "MIN", or "MAX": defines the behaviour to use on duplicate entries during the zUnion.
Join and intersect N zset, and save the final set in dstkeyN. For the score of each element in the collection, the WEIGHT parameter is multiplied by the WEIGHT parameter before performing the AGGREGATE operation. If no WEIGHT is provided, the default is 1. The default AGGREGATE is SUM, that is, the score of the elements in the result set is the SUM value of all the 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 of all the corresponding elements in the set.
Hash operation
HSet
$redis- > hSet ('hype,' key1', 'hello')
Add the element key1- > hello to the hash named h
HGet
$redis- > hGet ('hype,' key1')
Returns the value (hello) corresponding to key1 in hash with the name h
HLen
$redis- > hLen ('h')
Returns the number of elements in hash with the name h
HDel
$redis- > hDel ('hype,' key1')
Delete the domain with the key key1 in the hash named h
HKeys
$redis- > hKeys ('h')
Returns all keys in the hash with the name key
HVals
$redis- > hVals ('h')
Returns the value of all keys in the hash with the name h
HGetAll
$redis- > hGetAll ('h')
Returns all keys (field) and their corresponding value in the hash named h
HExists
$redis- > hExists ('hype,' a')
Whether there is a domain with key name an in the hash with the name h
HIncrBy
$redis- > hIncrBy ('hints,' xtrees, 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 in batches to the hash named key
HMGet
$redis- > hmGet ('hashes, array (' field1', 'field2'))
Returns the value corresponding to field1,field2 in hash with the name h
Related to redis operation
FlushDB
Clear the current database
FlushAll
Clear all databases
RandomKey
A key that randomly returns a key space
$key = $redis- > randomKey ()
Select
Select a database
Move
Transfer one key to another database
$redis- > select (0); / / switch to DB 0
$redis- > set ('x, '42'); / / write 42 to x
$redis- > move ('xcow, 1); / / move to DB 1
$redis- > select (1); / / switch to DB 1
$redis- > get ('x'); / / will return 42
Rename, renameKey
Rename key
$redis- > set ('xmom,' 42')
$redis- > rename ('x','y')
$redis- > get ('y'); / / → 42
$redis- > get ('x'); / / → `FALSE`
RenameNx
Similar to remane, but will not be replaced successfully if the renamed name already exists
SetTimeout, expire
Set the active time of a key (s)
$redis- > setTimeout ('x, 3)
ExpireAt
Key survived to a unix timestamp time
$redis- > expireAt ('xmom, time () + 3)
Keys, getKeys
Returns all key that satisfy a given pattern
$keyWithUserPrefix = $redis- > keys ('user*')
DbSize
Check how many key there are in the database now
$count = $redis- > dbSize ()
Auth
Password authentication
$redis- > auth ('foobared')
Bgrewriteaof
Using aof for database persistence
$redis- > bgrewriteaof ()
Slaveof
Select from the server
$redis- > slaveof ('10.0.1.7 dollars, 6379)
Save
Save data synchronization to disk
Bgsave
Save data asynchronously to disk
LastSave
Returns the Unix timestamp of the last time the data was successfully saved to disk
Info
Return details such as version information of redis
Type
Returns the type value of 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
These are all the contents of this article entitled "what are the commands for using redis in PHP?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.