PHP Operations Redis

Source: Internet
Author: User
Tags auth first string numeric value redis redis server
Verify that the server has opened the Redis service  redis port number by default is 6379     A small example code <?php header ("Content-type:text/html;charset=utf8"); Declares the Redis object instance $redis = new Redis (); Call the Redis object's Connect method, the first parameter is Redis server address, the 2nd is Redis port, the default is 6379 $redis->connect ("localhost", 6379); If Redis security authentication is enabled, then we need to use the Auth method in Redis to $redis->auth ("123456") through the security authentication password;  //$redis->set ("Liang", "Gou Li National Life and Death"); Storing the hash table, the array becomes the Field,value//$redis->hmset of the hash table ("Stars:jay", Array ("name" => ' Jay ', ' Age ' =>33, ' lover ' => ') Blue Ball ")); Gets the value of the hash table//$data = $redis->hgetall ("Stars:jay");    //using the Sadd method to add a unordered set called lessons//$redis->sadd ("Lessons", "PHP"); The first parameter is the collection name, and the second parameter is the element//$redis->sadd ("Lessons", "Java") of the collection; Use the Smembers method to get all elements//$data = $redis->smembers ("Lessons") in an unordered set; Use the Zadd method to add an ordered set named Girls//$redis->zadd ("Girls", 1, "Woo Yin"); //collection name, sort, value///$redis->zadd ("Girls", 4, "" "); $redis->zadd ("Girls", 2, "AB"); $data = $redis->zrange ("Girls", 0,-1);   //to obtain the data of the collection//$data 1= $redis->Zrevrange ("Girls", 0,-1); //Descending Gets the data of the collection  //Message Queuing//exists to determine if this key name exists if ($redis->exists (' lst ')) {//lpop for pop-up if ($str = $redis->lpop (' lst ')) {    echo $str;} else{    echo "no movie played";} die; $movies = Array (    17:00 starts playing movie < 3> ",    " 20:00 start playing movie < 4> ",    " 22:0 0 start playing movie < leaf ask 5> ",    ); foreach ($movies as $movie) {   //rpush to press the list     $redis-> rpush ("LST", $movie) in the form of queues; nbsp          -//connecting to the local  Redis  service-$redis  = new redis (); -$redis->connect (' 127.0.0.1 ',  6379); -$redis->auth (' 123456 '); --/*********************key (key) *********************/-//del key [key .] ------Remove the given one or more key-$a  =  $redis->del (Array (' XRJ ', ' ly ', ' fjf ')); -//keys pattern------Find all key-$b  =  $redis->keys (' * ') that conform to the given pattern patterns; -//randomkey------Return randomly from the current database (not deleted) a key-$c  =  $redis->randomkey (); -//ttl key------Returns the remaining lifetime of the given key (ttl, time to live)-//pttl key------in milliseconds, in seconds, Returns the remaining lifetime of the given key-$d  =  $redis->ttl (' BBS '); -//exists key------Checks whether a given key exists, returns 1, or returns 0-$e  =  $redis->exists (' email '); -//MOVE&NBSP;KEY&NBSP;DB------Move the key of the current database to the given database db-$f  =  $redis->move (' email ', 1); -$redis->select (1); -$f  =  $redis->keys (' * '); -$f  =  $redis->move (' email ', 0); -//rename key newkey------renamed key to newkey-$g  =  $redis->rename (' email ', ' e-eamil.com '); -$g  =  $redis->rename (' e-eamil.com ', ' eamil '); -//renamenx key newkey------When and only if Newkey does not exist, rename the key to newkey-$h  =  $redis->renamenx (' BBS ', ' Bbs1 '); -//type key------Returns the type of value stored by key-$i  =  $redis->type (' PageView '); -//expire key seconds------To set the lifetime of the key, when the key expires, it will be automatically deleted-//pexpire key  milliseconds------Set the lifetime of the key in milliseconds-//expireat key timestamp------Command accepts the time parameter is the UNIX timestamp, the key survives to a UNIX timestamp time-//persist key------Removes the lifetime of the given key, Convert to a key-//sort key [by pattern] [limit offset count] [get with no time to live and never expire  pattern [get pattern&nbsp ...]  [asc | desc] [alpha] [store destination]------Returns or saves the sorted elements in the given list, collection, ordered collection key-/** String (string)-related operations ***************/-//set key value------The string value value is associated with the key, overwriting-$a  =   $redis->set (' email ', ' jiang@58haha.cn '); -//setnx key value------To set the value of the key to 1 if and only if the key does not exist, successfully return 0-$b  =  $redis->setnx (' Email ', ' jiang@58haha.cn '); -//setex key seconds value------Associates value values to key and sets the lifetime of the key to seconds (in seconds)-$c  =  $redis- >setex (' name ', 10086, ' Jiang '); -//psetex key milliseconds value------command is similar to the Setex command, which sets the lifetime of the key in milliseconds-$d  =  $redis-> Psetex (' Jiang ', 90000, ' hahaha '); -//mset key value [key vaLue&nbsp ...] ------Set one or more key-value pairs-$e  =  $redis->mset (Array (' ly '  =>  ' Liyang ',  ' FJF '   =>  ' Fengjingfeng ')); -//msetnx key value [key value&nbsp ...] ------sets one or more key-value pairs at the same time, if and only if the given key does not exist-$f  =  $redis->msetnx (Array (' ly '  =>  ' Liyang '),   ' xrj ' => ' Xingrongjiang ',  ' fjf '  =>  ' Fengjingfeng '); -//append key value------If the key already exists and is a string, the APPEND command appends value to the end of the key's original value. If key does not exist, append is like executing set key value-$g  =  $redis->append (' ly ', ' love '); -//get key------Returns the string value associated with the key and returns a special value if the key does not exist Nil,get can only be used to process string values-$h  =  $redis->get (' ly '); -//mget key [key&nbsp ...] ------Returns the value of all (one or more) given key, if a key in the given key does not exist, the key returns a special value nil and the command never fails-$i  =  $redis->mget (' Email ', ' ly ', ' fjf '); -//getrange key start end------Returns a substring of the string value in the key, which is determined by the start and end two offsets (including start and end), A negative offset represents the last character from the end of the string-1 for the last, and 2 for the penultimate-$j  =  $redis->getrange (' email ', 0,-1)  //from first to last, equivalent to direct get-//getset key  Value------Sets the value of the given key to values and returns the old value of the key (Old value)-$k  =  $redis->getset (' email ', ' Jiangzunshao@163.com '); -//strlen key------Returns the length of the string value stored by key-$l  =  $redis->strlen (' email '); -//decr key------Reduce the number of values stored in the key by one, if key does not exist, then the value of the key will be initialized to 0 before performing DECR operations-$m  =  $redis->decr (' PageView '); -//incr key------Add a number value stored in the key, and if key does not exist, then the value of the key is initialized to 0 before performing INCR operation-$n  =  $redis->incr (' Pageview1 '); -//decrby key decrement------The value stored by key minus the decrement decrement (can be negative)-$o  =  $redis->decrby (' PageView ', 3); -//incrby key increment------Add the value stored by the key to the increment increment (can be negative)-$p  =  $redis->incrby (' PageView ', 6); --/****************hash (hash table) related operations ***************/-//hset key field  Value------Sets the value of the field field in the hash table key to values, new returns 1, and overrides the return 0-$a  =  $redis->hset ("User", "jiang@58haha.cn", "{" Name ': ' JiangzunsHao ', ' age ':, ' work ': ' php ', ' City ': ' BeiJing '} '; -//hsetnx key field value------Sets the value of the field field in the hash table key to values, and returns 1 if the .  setting succeeds only if the field field does not exist. If an existing and no operation is performed, return 0-$b  =  $redis->hsetnx ("User", "jiang@58haha.cn", "{' Name ': ' Jiangzunshao ', ' age ': 25, ' Work ': ' php ', ' City ': ' BeiJing '} '; -//hget key field------Returns the value of a given field field in the hash table key-$c  =  $redis->hget ("User", "jiang@58haha.cn"); -//hmset key field value [field value&nbsp ...] ------Set multiple Field-value (field-value) pairs to the hash table  key-$d  =  $redis->hmset (' user ', Array (' name ' => ') Jiangzunshao ',  ' age '  => 20)); -//hmget key field [field&nbsp ...] ------Returns the value of one or more given fields in a hash table  key-$f  =  $redis->hmget (' User ',  array (' name ',  ' age ')); -//hgetall key------Return to the hash table key, all fields and values-$g  =  $redis->hgetall (' user '); -//hdel key field [field&nbsp ...] ------Delete one or more of the specified fields in the hash table key, the nonexistent domain will be ignored-$h  =  $redis-&GT;HDEl (' User ', ' age '); -//hlen key------Returns the number of fields in the hash table key-$i  =  $redis->hlen (' user '); -//hexists key field------See if a given field field exists in the hash table key, there is a return of 1, there is no return 0-$g  =  $redis->hexists (' User ', ' jiang@58haha.cn '); -//hincrby key field increment------Add an increment increment to the Field field value in the hash table key, which can be negative-$k  =  $redis- >hincrby (' user ', ' PV ', 5); -//hkeys key------Return all fields in the hash table key-$l  =  $redis->hkeys (' user '); -//hvals key------Returns the value of all fields in the hash table key-$m  =  $redis->hvals (' user '); --/****************list (list) related operations ***************/-//lpush key value [value  ------Insert data into the head of the list-$redis->lpush (' dbs ', ' mongodb-3.2 '); -//rpush key value [value&nbsp ...] ------inserts data into the tail of the list-$redis->rpush (' dbs ', ' Redis '); -//llen key------Get the length of the list-$c  =  $redis->llen (' dbs '); -//lpop key------Remove and return the header element of the list-$d  =  $redis->lpop (' dbs '); -//rpop key------Remove and return the tail element of the list-$e  =  $rEdis->rpop (' dbs '); -//lrange key start stop------Returns the elements within the specified range in the list-$f  =  $redis->lrange (' dbs ', 0, $redis-> Llen (' dbs ')); -$f  =  $redis->lrange (' dbs ', 0,-1); -//lset key index value------Set the value of the element labeled Index to value-$h  =  $redis->lset (' dbs ', 1, ' Jiang '); -//ltrim key start stop------list retains only the elements within the specified range-$i  =  $redis->ltrim (' dbs ', 3,5); -//lindex key index------Returns the element labeled index in the list-$e  =  $redis->lindex (' dbs ', $redis->llen (' DBS ')-1); -//linsert key before| After pivot value------To insert value values into the list, before or after the value pivot-$g  =  $redis->linsert (' dbs ', Redis:: Before, ' MySQL ', ' mysqlmysql '); -$g  =  $redis->linsert (' dbs ', Redis::after, ' Redis ', ' Redisredis '); -//rpoplpush source destination------Command Rpoplpush performs the following two actions within an atomic time: 1 pops up the last element in the list source (the tail element) and returns it to the client; 2, insert the element that is ejected from source into the list destination as the head element of the destination list-$k  =  $redis->rpoplpush (' dbS1 ', ' dbs '); --/****************set (set) related operations ***************/-//sadd key member [member  ------adds one or more member elements to the collection key, the membership element that already exists in the collection is ignored-$a  =  $redis->sadd (' Set ',  ' Xingrongjiang ') ); -//srem key member [member&nbsp ...] ------removes one or more member elements from the collection key, the nonexistent member element is ignored-$b  =  $redis->srem (' Set ', ' Array '); -//smembers key------Returns all members of the collection key-$c  =  $redis->smembers (' Set '); -//sismember key member------Judge whether the member element sets the members of the key-$d  =  $redis->sismember (' Set ', ' Liyang ') ); -//scard key------Returns the cardinality of the collection key (number of elements in the collection)-$e  =  $redis->scard (' Set '); -//smove source destination member------Move the member element from the source collection to the destination collection-$f  = $ Redis->smove (' Set ', ' Set1 ', ' Xingrongjiang '); -//spop key------Remove and return a random element in the collection-$g  =  $redis->spop (' Set '); -//srandmember key [count]------Returns a random element in the collection if only the key parameter is supplied; if Count is a positive number and is less than the collection cardinality, returns a containing countAn array of elements, the elements in the array are different, and if count is greater than or equal to the set cardinality, the entire collection is returned; if count is negative, the command returns an array in which the elements of the array may be repeated multiple times, while the length of the array is the absolute value of Count-$h  =   $redis->srandmember (' Set ', 2); -//sinter key [key&nbsp ...] ------Returns all the members of a collection that are the intersection of all given sets-$i  =  $redis->sinter (' Set '); -//sinterstore destination key [key&nbsp ...] ------is similar to the Sinter command, which saves the result to the destination collection, rather than simply returning the result set-$j  =  $redis->sinterstore (' haha ', ' set '); -//sunion key [key&nbsp ...] ------Returns all members of a set that are the set of all given sets-$k  =  $redis->sunion (' Set ', ' Set1 '); -//sunionstore destination key [key&nbsp ...] ------is similar to the Sunion command, which saves the result to the destination collection, rather than simply returning the result set-$l  =  $redis->sunionstore (' haha1 ', ' haha ', ' set1 ') ); -//sdiff key [key&nbsp ...] ------Returns all the members of a set that are the difference set between all given sets-$m  =  $redis->sdiff (' Set ', ' Set1 '); --/**************** ordered set (Sorted set) related operations ***************/-//zadd key score  The member------adds an element member,score to the Zset named key to sort, if the element already exists, based on theScore update the order of the elements-$a  =  $redis->zadd (' site ', 10,  ' google.com '); -$a  =  $redis->zadd (' site ', 9,  ' baidu.com '); -$a  =  $redis->zadd (' site ', 8,  ' sina.com.cn '); -//zrem key member------Remove elements in Zset named key member-//  $b  =  $redis->zrem (' site ', ' Sina.com.cn '); -//zcard key------Returns the number of all elements of the zset named Key-$c  =  $redis->zcard (' site '); -//zcount key min max------Returns the number of members score value between Min and Max in ordered set key-$d  =  $redis->zcount (' Site ', 6,9); -//zscore key member------Returns the score value of member members in an ordered set key-$e  =  $redis->zscore (' site ', ' Baidu.com '); -//zincrby key increment member------Add an increment increment to the score value of member members of the ordered set key The return value is the result of score plus increment-$f  =  $redis->zincrby (' site ', ' baidu.com '); -//zrange key start stop [withscores]------Returns the members of a specified interval in an ordered set key in which the position of the member is incremented by the score value (from small to large)-$j  =  $redis->zrange (' SitE ', 0,-1); -$j  =  $redis->zrange (' site ', 0,-1,true); -//zrevrange key start stop [withscores]------Returns the members of a specified interval in an ordered set key, in which the position of the member is decremented by the score value (from large to small) -$h  =  $redis->zrevrange (' site ', 0,-1); -$h  =  $redis->zrevrange (' site ', 0,-1,true); --/****************connection (connection) ***************/-//auth password------Password Authentication-$a  =  $redis-> Auth (' 123456 '); -//ping------View connection Status-$b  =  $redis->ping (); -//select index------Switch to the specified database, the database index number is specified with a numeric value, 0 as the starting index value, the default use of the No. 0 database-$c  =  $redis->select (1); --/****************server (server) ***************/-//time------Returns the current server time, a list containing two strings: the first string is the current time (in UNIX timestamp format), The second string is the number of microseconds that have elapsed since the current second-$a  =  $redis->time (); -//dbsize-----Returns the number of key keys in the current database-$b  =  $redis->dbsize (); -//bgrewriteaof------Use AOF for database persistence-$c  =  $redis->bgrewriteaof (); -//save------To save data to disk-$d  =  $redis->save (); -//bgsave------to asynchronously save data to disk-$e &NBsp;=  $redis->bgsave (); -//lastsave------Returns the UNIX timestamp-$f  =  $redis->lastsave () for which the data was last successfully saved to disk; -//slaveof host port------Select from server-$redis->slaveof (' 10.0.1.7 ',  6379); -//flushall------Empty the entire Redis server's data (remove all key from all databases)-$redis->flushall (); -//flushdb------Empty All key-$redis->flushdb () in the current database; -//info [section]------Returns a variety of information and statistical values about the  Redis  server-$g  =  $redis->info ();
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.