Phpredis function Use Summary "classification detail" ___ function

Source: Internet
Author: User
Tags delete key redis

have been using PHP to use Redis, but always feel insecure, simply borrow this time free coherent,

PHP all the operations to the Redis command, almost all the time, including its return value is to stare right, even if the next forget can also directly come to check it ~ Everyone can be assured to use.


Test environment: php:5.5 redis:2.4.6

Reference URL: Https://github.com/phpredis/phpredis reference URL: http://www.cnblogs.com/zcy_soft/archive/2012/09/21/2697006.html

Redis Five types of data usage scenarios: http://blog.csdn.net/gaogaoshan/article/details/41039581


Tips: For: String, set, sort set, the hash of the increase, change operation, is the same command, but when it changes the operation, timely successful return value is still 0 for: list structure, add or delete to change the free set of methods.

<?php/*1.connection*/$redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379, 1);/short link, local host, Port 6379, over 1 seconds discard link $redis->open (' 127.0.0.1 ', 6379, 1);/Short link ( IBID.) $redis->pconnect (' 127.0.0.1 ', 6379, 1);//long link, local host, Port 6379, over 1 seconds discard link $redis->popen (' 127.0.0.1 ', 6379, 1); /Long Link (ibid.) $redis->auth (' password ');//login authentication password, return "true | False "$redis->select (0);//select Redis Library, 0~15 a total of 16 libraries $redis->close ();/release Resources $redis->ping (); Check if the link is still, [+pong] $redis->ttl (' key ');//view Expiration Time [-1 | timestamps] $redis->persist (' key ');/remove Expiration time [1 | 0] $re Dis->sort (' key ', [$array]); Returns or saves the sorted elements in the given list, collection, ordered set key, $array as parameter limit, and so on. "With $array is very powerful" [Array|false]/*2. Common operation Classification/$redis->expire (' key ', 10);/set expiration time [true | false] $redis->move (' key ' , 15)//Move the key in the current library to the 15 library [0|1]//string $redis->strlen (' key ');//Get the length of the current key $redis->append (' key ', ' string '); Append string to Key's existing value [append number] $redis->incr (' key ');//1, if no key exists, assignment is 1 (valid for integers only, stored in 10 64 bits, str in redis) [New_num |  FALSE] $redis->incrby (' key ', $num);//$num, does not exist for assignment, the value needs to be an integer [New_num | false] $redis-&GT;DECR (' key ');//self-reduction 1,[new_num | FALSE] $redis->decrby (' key ', $num);//$num,[New_num | false] $redis->setex (' key ', ten, ' value ');//key=value, valid is 10 sec [true]//list $redis->llen (' key '),//returns the length of the list key, no key returns 0, [len | 0]//set $redis->scard (' key '); Cardinality (number of elements in the collection). [num | 0] $redis->smove (' Key1 ', ' key2 ', ' member ');//move to move the member element from the Key1 collection to the Key2 collection. [1 | 0]//zset $redis->zcard (' key ');//Returns the cardinality of the collection key (the number of elements in the collection). [num | 0] $redis->zcount (' key ', 0,-1);//Returns the ordered set key, where the score value is between Min and Max (the default includes score values equal to Min or max). [num | 0]//hash $redis->hexists (' Key ', ' field ');//See if the hash exists field,[1 | 0] $redis->hincrby (' Key ', ' field ', $int _ num)//For the value of the field field in the Hash table key (+|-) Num,[new_num | false] $redis->hlen (' key ');//Returns the number of fields in the hash table key. [num | 0]/*3.server*/$redis->dbsize ()//Returns the number of keys in the current library $redis->flushall ()/clear the entire redis[total true] $redis-&G T;FLUSHDB ()//empty current Redis library [total True] $redis->save ()//sync?? Store data to disk-dump.rdb[true] $redis->bgsave ()//asynchronous ...
   Store data to disk-dump.rdb[true] $redis->info ()//Query the status of the current Redis [verson:2.4.5 ...] $redis->lastsave ();//The time the last time key was stored [timestamp] $redis->watch (' key ', ' Keyn ');//monitor one (or more) key if this (or these) Key is changed by another command, the transaction will be interrupted [true] $redis->unwatch (' key ', ' Keyn ');//suppress monitoring of one (or more) key [true] $redis->multi (redis::multi
   //Open transaction, multiple commands within a transaction block are placed in a queue sequentially, and executed by the EXEC command at an atomic time.
   $redis->multi (redis::P ipeline);//Open pipe, multiple commands within a transaction block are placed in a queue sequentially, and executed by the EXEC command at an atomic time. $redis->exec (); The return value of all commands in the transaction block, in the order in which they are executed, and when the operation is interrupted, return null value false "/*4.string, key value pair, create update with operation/$redis-& Gt;setoption (Redis::opt_prefix, ' hf_ ')//Set table prefix to hf_ $redis->set (' key ', 1);/set Key=aa value=1 [true] $redis-> Mset ($arr);//Set one or more key values [true] $redis->setnx (' key ', ' value ');//key=value,key exists return false[|true] $redis->get (' Key ')//Gets the key [value] $redis->mget ($arr),//(String|arr), returns the value of the queried key $redis->del ($keY_arr);//(String|arr) Delete key, support array bulk Delete "return delete number" $redis->delete ($key _str, $key 2, $key 3);//delete Keys,[del_num] $redis- >getset (' Old_key ', ' New_value '), first get the value of the key, then assign the value, [Old_value | false]/*5.list The structure of the stack, note the end of the header, create an update separate operation/$redis-> Lpush (' key ', ' value ');/+, you can only insert a value of values into the table header of the list key, do not exist to create [the length of the list |false] $redis->rpush (' key ', ' value '); You can only insert a value of values into the footer of the list key [length |false] $redis->linsert (' key ', Redis::after, ' value ', ' new_value '); Inserts the value values into the list key, before or after value values. [New_len | false] $redis->lpushx (' key ', ' value ');/increase, you can only insert a value of values into the table header of the list key, there is no $redis to create the length of the list |false]->rpush X (' key ', ' value '); can only insert a value of values into the footer of the list key, there is no->lpop (' key ') without creating [the length |false] of the list;/delete, remove and return the header element of the list key, [deleted element | FALSE] $redis->rpop (' key ');//delete, remove and return the tail element of the list key, [deleted Element | false] $redis->lrem (' key ', ' value ', 0);//delete, according to the value of the parameter count, Removes the element count= in the list that is equal to the parameter value (0|-n the header to the tail |+n the end of the table to remove n value) [Number of removed | 0] $redis->ltrim (' key ', start,end); cut, list trim, keep ( Start,end) between the value [True|false] $redis->LSet (' key ', index, ' New_v '), or change, from the number of headers, list key subscript as the element of index is the value of New_v, [true | false] $redis->lindex (' key ', index); Returns the element in the list key that is labeled Index [Value|false] $redis->lrange (' key ', 0,-1);//check, (start,stop|0,-1) returns the elements within the specified interval in the list key. The interval is specified with the offset start and stop. [Array|false]/*6.set, no duplicate member, create update with Operation */$redis->sadd (' key ', ' value1 ', ' value2 ', ' Valuen '); Adds one or more member elements to the collection key, which already exists in the collection will be ignored. [Insert_num] $redis->srem (' key ', ' value1 ', ' value2 ', ' Valuen ');//delete, remove one or more member elements from the collection key, the nonexistent member element is ignored [DEL_ num | FALSE] $redis->smembers (' key ');/check, return all members in the collection key [array |  '] $redis->sismember (' key ', ' member ');//Determine whether the members element is an assembly key [1 | 0] $redis->spop (' key ');/delete, remove and return a random element in the collection [member | false] $redis->srandmember (' key ');/check, return a random element in the collection [member | false] $redis->sinter (' Key1 ', ' key2 ', ' K Eyn '); Returns the intersection of all given sets [array | false] $redis->sunion (' Key1 ', ' key2 ', ' Keyn ');/check, return the set of all given sets [array | false] $redis ->sdiff (' Key1 ', ' key2 ', ' Keyn ');/check, returns the difference set for all given sets [array | faLSE]/*7.zset, no duplicate member, have sort order, create update with Operation * * $redis->zadd (' key ', $score 1, $member 1, $scoreN, $memberN); Adds one or more member elements and their score values to the ordered set key. [num | 0] $redis->zrem (' key ', ' member1 ', ' membern ');//delete, remove one or more members from the ordered set key, and the nonexistent members will be ignored. [del_num | 0] $redis->zscore (' key ', ' member ');/check, Naquan by value [num | null] $redis->zrange (' key ', $start, $stop); (score from small to large) "Sort ranking Range" Take member value, return ordered set key, "specified interval" members [array | null] $redis->zrevrange (' key ', $start, $stop);/check, pass ( Score from big to small) "Sort ranking range" takes the member value, returns an ordered set key, "within the specified interval" [array | null] $redis->zrangebyscore (' key ', $min, $max [, $config]) /check, through the Scroe right range to take the member value, return ordered set key, within the specified interval (small to large row) members [array | null] $redis->zrevrangebyscore (' key ', $max, $min [, $ Config]/check, through the Scroe right range to take member value, return ordered set key, within the specified interval (from large to small row) members [array | null] $redis->zrank (' key ', ' the '); By member value check (score from small to large) ranked "member sort ranking" [order | null] $redis->zrevrank (' key ', ' member '), check, check by member value ( Score from big to small "member sort rank" in result [order | null] $redis->zinterstore ();//intersect $Redis->zunionstore ()///Difference set/*8.hash, table structure, create update with Operation */$redis->hset (' Key ', ' field ', ' value '); Sets the value of the field field in the hash table key to values, does not exist, and exists to overwrite "1 |
   0 "$redis->hget (' Key ', ' field ');/check, take value" value|false "$arr = Array (' one ' =>1,2,3); $arr 2 = Array (' One ', 0, 1); $redis->hmset (' key ', $arr);//increase, change, set multivalued $arr to (Index | associative) array, $arr [Key]=field, [True] $redis->hmget (' key ', $arr 2); Gets the specified subscript's field,[$arr | false] $redis->hgetall (' key '), or check, returns all fields and values in the hash table key. [Returns an empty table when key does not exist] $redis->hkeys (' key ');/check, return all fields in the hash table key. [Returns an empty table when key does not exist] $redis->hvals (' key ');/check, returns all values in the hash table key.


 [Returns an empty table when key does not exist] $redis->hdel (' key ', $arr 2);//Delete, remove the field that specifies the subscript, the nonexistent field will be ignored, [num | false]?>





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.