PHP Operation Redis Detailed explanation

Source: Internet
Author: User
Tags delete key redis
PHP Operation Redis Detailed explanation of the turn

Phpredis is an extension of Redis PHP, and the efficiency is quite high with the sort function of linked list, to create memory-level module business relationship

Very useful; The following are the Redis's official command tips:

The download address is as follows:

Https://github.com/owlient/phpredis (Support Redis 2.0.4)

Redis::__construct Constructors
$redis = new Redis ();

Connect, open link Redis service
Parameters
host:string, service address
Port:int, port number
Timeout:float, link length (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
Reference above

SetOption Set Redis mode

GetOption View the mode of Redis settings

Ping View connection Status

Key related actions

Del

Removes the given one or more key.

If the key does not exist, the command is ignored. Time complexity: O (n), n is the number of key to remove. Removes a single string type key with a time complexity of O (1). Removes a single list, set, ordered set, or hash table type key, with a time complexity of O (M) and M for the number of elements in the above data structure. Return value: The number of keys to be removed.

//del 
# condition 1: Delete a single key
$redis->set (' myname ', ' Ikodota ');
echo $redis->get (' myname '). <br> '; # return: Ikodota

$redis->del (' myname '); # return TRUE (1)
Var_dump ($redis->get (' myname ')); # return bool (FALSE)

# condition 2: Delete a nonexistent key
if (! $redis->exists (' Fake_key ')) # does not exist
Var_dump ($redis->del (' Fake_key ')); Int (0)

# 3: Simultaneous deletion of multiple key
$array _mset=array (' First_key ' => ' first_val ',
' Second_key ' => ') second_val ',
' Third_key ' => ' third_val ');
$redis->mset ($array _mset); #用MSET一次储存多个值
$array _mget=array (' First_key ', ' second_key ', ' Third_key ');
Var_dump ($redis->mget ($array _mget)); #一次返回多个值//array (3) {[0]=> string (9) "First_val" [1]=> string (a) "Second_val" [2]=> string (9) "Third_val"}

$redis->del ($array _mget); #同时删除多个key
Var_dump ($redis->mget ($array _mget)) #返回 Array (3) {[0]=> bool ( FALSE) [1]=> bool (FALSE) [2]=> bool (FALSE)}

The keys keys find the key that matches the given pattern. KEYS * hits all key in the database. KEYS H?llo hit Hello, Hallo and Hxllo. KEYS H*llo hit Hllo and Heeeeello. KEYS H[ae]llo hits Hello and Hallo, but does not hit Hillo.

Special symbols use "\" to separate time complexity: O (n), n is the number of key in the database. Return value: A key list that conforms to the given pattern.

Warning: Keys are very fast, but using it in a large database can still cause performance problems, and if you need to find a specific key from a dataset, you'd better use the set.

KEYS
# $redis->flushall ();
$array _mset_keys=array (' One ' => ' 1 ',
' Two ' => ' 2 ',
' Three ' => ' 3 ',
' Four ' => ' 4 ');
$redis->mset ($array _mset_keys); #用MSET一次储存多个值
Var_dump ($redis->keys (' *o* ')); Array (3) {[0]=> string (4) "Four" [1]=> string (3) "Two" [2]=> string (3) "One"}
Var_dump ($redis->keys (' t?? ')); Array (1) {[0]=> string (3) "Two"}
Var_dump ($redis->keys (' t[w]* ')); Array (1) {[0]=> string (3) "Two"}
Print_r ($redis->keys (' * ')); Array ([0] => four [1] => three [2] => two [3] => one)

Randomkey

Returns (without deleting) a key randomly from the current database. Time complexity: O (1) Return value: Returns a key when the database is not empty. Returns nil when the database is empty.

//randomkey 
$redis->flushall ();
# case 1: Database is not empty
$array _mset_randomkey=array (' Fruit ' => ' apple ',
' drink ' => ' beer ',
' Food ' => ' Cookis ');
$redis->mset ($array _mset_randomkey);
Echo $redis->randomkey ();
Print_r ($redis->keys (' * ')); # View all keys in the database to prove that Randomkey does not delete Key//array ([0] => food [1] => drink [2] => Frui T)

# condition 2: The database is empty
$redis->flushdb (); # Delete the current database all key
Var_dump ($redis-> randomkey ());//bool (false)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.