How to understand PHP operation redis
PHP operation redis how to understand, in view of this problem, this article introduces in detail the corresponding analysis and solutions, hoping to help more partners who want to solve this problem to find a more simple and easy way.
Redis is an open source API that is written in ANSI C language, supports the network, can be memory-based and persistent, and provides API in multiple languages.
The data types supported by Redis are Stirng (string), List (list), Hash (dictionary), Set (collection), Sorted Set (ordered collection).
The redis version is Redis 2.6.12. The system is in Windows+Apache2.4+php5.6.
Connection:
Click (here) to collapse or open
/ / instantiate redis
$redis = new Redis ()
/ / Connect
$redis- > connect ('127.0.0.1 dollars, 6379)
/ / check whether the connection is successful
Echo "Server is running:". $redis- > ping ()
/ / output result Server is running: + PONG
Strng (string):
Click (here) to collapse or open
/ / set the value of a string
$redis- > set ('cat', 111)
/ / get the value of a string
Echo $redis- > get ('cat'); / / 111
/ / repeat set
$redis- > set ('cat', 222)
Echo $redis- > get ('cat');
List (list):
Click (here) to collapse or open
/ / list
/ / store data in the list
$redis- > lpush ('list',' html')
$redis- > lpush ('list',' css')
$redis- > lpush ('list',' php')
/ / get all the values in the list
$list = $redis- > lrange ('list', 0,-1)
Print_r ($list); echo'
'
/ / add one from the right
$redis- > rpush ('list',' mysql')
$list = $redis- > lrange ('list', 0,-1)
Print_r ($list); echo'
'
/ / one pops up from the left
$redis- > lpop ('list')
$list = $redis- > lrange ('list', 0,-1)
Print_r ($list); echo'
'
/ / one pops up from the right
$redis- > rpop ('list')
$list = $redis- > lrange ('list', 0,-1)
Print_r ($list); echo'
'
/ / result
/ / Array ([0] = > php [1] = > css [2] = > html)
/ / Array ([0] = > php [1] = > css [2] = > html [3] = > mysql)
/ / Array ([0] = > css [1] = > html [2] = > mysql)
/ / Array ([0] = > css [1] = > html)
Click (here) to collapse or open