PHP Operation Redis

Source: Internet
Author: User
Tags redis version

Redis is an open source API that is written in ANSI C, supports the web, can be persisted in memory, key-value databases, and provides multiple languages.

The data types supported by Redis are STIRNG (strings), list (lists), Hash (dictionary), set (set), Sorted set (ordered set);

The Redis version is the Redis 2.6.12 system in windows+apache2.4+php5.6

Connection:

1     //instantiation Redis2     $redis = new Redis (), 3     //Connection 4     $redis->connect (' 127.0.0.1 ', 6379); 5     //Detect connection successful 6     echo "Server is running:". $redis->ping (); 7     //Output result Server is running: +pong    

Strng (String):

Sets the value of a string    $redis->set (' Cat ', 111);    Gets the value of a string    echo $redis->get (' cat ');//111    //Repeat set    $redis->set (' Cat ', 222);    echo $redis->get (' cat '); 222

List (lists):

 1//List 2//store data to List 3 $redis->lpush (' list ', ' HTML '), 4 $redis->lpush (' list ', ' CSS '), 5 $re Dis->lpush (' list ', ' php '); 6 7//Get all the values in the list 8 $list = $redis->lrange (' list ', 0,-1); 9 Print_r ($list); Echo ' <br> '; 10 11//Add a $redis->rpush (' list ', ' MySQL ') from the right, $list = $redis->lrange (' list ', 0,-1); Print_r ($list); Echo ' <br> '; 15 16//Eject a $redis->lpop (' list ') from the left, $list = $redis->lrange (' list ', 0,-1 ); Print_r ($list); Echo ' <br> '; 20 21//Eject a $redis->rpop (' list ') from the right, $list = $redis->lran  GE (' list ', 0,-1); Print_r ($list); Echo ' <br> '; 25 26//Results//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) 
 1 <?php 2//instancing Redis 3 $redis = new Redis (); 4//Connection 5 $redis->connect (' 127.0.0.1 ', 6379); 6//List 7//store data into the list of 8 $redis->lpush (' list ', ' html ');     9 $redis->lpush (' list ', ' CSS '), $redis->lpush (' list ', ' php '), $redis->lpush (' list ', ' MySQL '); 12 $redis->lpush (' list ', ' JavaScript '), $redis->lpush (' list ', ' Ajax '); 14 15//Get all the values in the list $list = $red Is->lrange (' list ', 0,-1); Print_r ($list); Echo ' <br> '; 18 19//Get the length of the list $length = $redis->lsize (' list '), echo $length, echo ' <br> '; 22 23//Return list key in IND The value of the ex position is echo $redis->lget (' list ', 2), Echo ' <br> ', echo $redis->lindex (' list ', 2), echo ' <br> '; 26 27//Set the value of the index position in the list echo $redis->lset (' list ', 2, ' Linux '), Echo ' <br> ', $list = $redis->lran GE (' list ', 0,-1); Print_r ($list); Echo ' <br> '; 31 32//Returns the element from start to end position in key $list = $redis->lran GE (' list ', 0, 2);Print_r ($list), Echo ' <br> ', $list = $redis->lgetrange (' list ', 0, 2), PNs Print_r ($list); Echo ' & Lt;br> '; 38 39//intercept the elements of the list from start to end
After the list of intercepts is changed, the list retains the intercepted elements, the remaining deletions are $list = $redis->ltrim (' list ', 0, 1); Print_r ($list); Echo ' <br> '; 42 $list = $redis->lrange (' list ', 0,-1), Print_r ($list), echo ' <br> '; 45//Result//Array ([0] + ajax [1] = + JavaScript [2] = MySQL [3] + PHP [4] + CSS [5] + HTML)//648//mysql49 MYSQL50//151//Array ([0] = + ajax [1] = + JavaScript [2] = = Linux [3] + + php [4] = + CSS [5 ] + HTML)//array ([0] = + ajax [1] = + JavaScript [2] = = Linux) +//Array ([0] = Ajax [1] = = JavaScript [2] = Linux) SI//155//Array ([0] = + ajax [1] = + JavaScript)
 1 <?php 2//instancing Redis 3 $redis = new Redis (); 4//Connection 5 $redis->connect (' 127.0.0.1 ', 6379); 6//List 7//store data into the list of 8 $redis->lpush (' list ', ' html ');     9 $redis->lpush (' list ', ' HTML '), $redis->lpush (' list ', ' HTML '), $redis->lpush (' list ', ' CSS '); 12 $redis->lpush (' list ', ' php '), $redis->lpush (' list ', ' MySQL '), $redis->lpush (' list ', ' JavaScript ');      $redis->lpush (' list ', ' HTML '), $redis->lpush (' list ', ' HTML '), $redis->lpush (' list ', ' HTML '); 18 $redis->lpush (' list ', ' Ajax '); 19 20//Get all the values in the list $list = $redis->lrange (' list ', 0,-1); Print_r ( $list); Echo ' <br> '; 23 24//Delete the Count value in the list element 25//left-to-right $redis->lrem (' list ', ' HTML ', 2), $list = $redis->lrange ( ' list ', 0,-1); Print_r ($list); Echo ' <br> '; 29 30//right-to-left delete $redis->lrem (' list ', ' HTML ',-2); $list = $redis->lrange (' list ', 0,-1); Nt_r ($list); Echo ' <br> '; 34 35//Delete all->lrem $redis (' list ', ' HTML ', 0), PNs $list = $redis->lrange (' list ', 0,-1); Print_r ($ Echo ' <br> '; 39 40//Results//Array ([0] = + Ajax [1] + HTML [2] + HTML [3] + HTML [4] =  > JavaScript [5] = + MySQL [6] + PHP [7] + CSS [8] + HTML [9] + HTML [ten] + HTML)//Array  ([0] = + Ajax [1] + HTML [2] = + JavaScript [3] + + MySQL [4] + PHP [5] + + CSS [6] + + HTML [7] = HTML [8] + HTML)//Array ([0] = + ajax [1] = + HTML [2] + JavaScript [3] + + MySQL [4] + + PHP [  5] + CSS [6] + HTML) +//Array ([0] = + ajax [1] = + JavaScript [2] = + MySQL [3] = + php [4] =      CSS)

Hash (dictionary):

 1 <?php 2//instancing Redis 3 $redis = new Redis (); 4//Connection 5 $redis->connect (' 127.0.0.1 ', 6379); 6//Dictionary 7//give the hash table a key Set value 8//If not set successfully, return 1, if present will replace the original value, return 0, fail to return 0 9 echo $redis->hset (' hash ', ' cat ', ' cat '); Echo ' <br> ', echo $redis->hset (' hash ', ' cat ', ' cat '), echo ' <br> ', echo $redis->hse  T (' hash ', ' cat ', ' cat1 '), Echo ' <br> ', echo $redis->hset (' hash ', ' dog ', ' dog '), echo ' <br> ', echo $redis->hset (' hash ', ' bird ', ' bird '), echo ' <br> ', Echo ' $redis->hset (' hash ', ' monkey ', ' monkey '), Echo ' & Lt;br> '; 15//Get the value of a key in the hash echo $redis->hget (' hash ', ' cat '), echo ' <br> '; 17 18//Get all the keys1 in the hash 9 $arr = $redis->hkeys (' hash '); Print_r ($arr); Echo ' <br> '; 21 22//Gets the order of all the values in the hash is randomly $arr = $ Redis->hvals (' hash '); Print_r ($arr); Echo ' <br> '; 25 26//Get a hash of all the keys and the value order is random $arr = $redi S->hgetall (' hash '); prinT_r ($arr); Echo ' <br> '; 29 30//Get the number of keys in the hash to echo $redis->hlen (' hash '); Echo ' <br> '; 32 33//Delete H One key in ash returns False34 echo $redis->hdel (' hash ', ' dog ') if the table does not exist or key does not exist; Echo ' <br> '; Var_dump ($redis->hde      L (' hash ', ' rabbit '); Echo ' <br> '; 36 37//Result 38//139//040//041//142//143//144 cat145//Array ([0] = cat [1] = dog [2] = bird [3] = monkey)//Array ([0] = Cat 1 [1] = dog [2] = = Bird [3] = monkey)//Array ([cat] = CAT1 [dog] + dog [bird] = bird [mon  Key] = monkey)//449////INT (0)
 1 <?php 2//instancing Redis 3 $redis = new Redis (); 4//Connection 5 $redis->connect (' 127.0.0.1 ', 6379); 6//Dictionary 7//batch set value of multiple keys 8 $arr = [1=>1, 2=>2, 3=>3, 4=>4, 5=>5]; 9 $redis->hmset (' hash ', $arr), Print_r ($redis->hgetall (' hash ')), echo ' <br> '; 11 12//Bulk gain multiple key     Values $arr = [1, 2, 3, 5];14 $hash = $redis->hmget (' hash ', $arr), Print_r ($hash); Echo ' <br> '; 16 17 Detect if a key in the hash is aware of the presence of Echo $redis->hexists (' hash ', ' 1 '); Echo ' <br> '; Var_dump ($redis->hexists (' hash ', ' cat '); Echo ' <br> '; Print_r ($redis->hgetall (' hash ')); Echo ' <br> '; 22 23//Add an integer value to key in the hash table $redis->hincrby (' hash ', ' 1 ', 1); Print_r ($redis->hgetall (' hash ')); Echo ' <br> '; 26 27//For Hash  A key adds a floating point value of $redis->hincrbyfloat (' hash ', 2, 1.3), Print_r ($redis->hgetall (' hash ')); Echo ' <br> '; 30 31//Results//Array ([1] = 1 [2] = 2 [3] = =3 [4] = 4 [5] = 5)//Array ([1] = 1 [2] = 2 [3] = 3 [5] + 5)//135//bool (FA  LSE)//Array ([1] = 1 [2] = 2 [3] = 3 [4] = 4 [5] + 5) PNS//Array ([1] = 2 [2] = =  2 [3] = 3 [4] = 4 [5] = 5)//Array ([1] = 2 [2] = = 3.3 [3] = 3 [4] = 4 [5] = 5)

Set (SET):

 1 <?php 2//instancing Redis 3 $redis = new Redis (); 4//Connection 5 $redis->connect (' 127.0.0.1 ', 6379); 6//Collection 7//Add an element 8 echo $redis->sadd (' Set ', ' cat '); Echo ' <br> ';     9 echo $redis->sadd (' Set ', ' cat '), echo ' <br> ', echo $redis->sadd (' Set ', ' dog '), echo ' <br> '; 11 echo $redis->sadd (' Set ', ' Rabbit '), echo ' <br> ', echo $redis->sadd (' Set ', ' Bear '), echo ' <br> '; 1 3 echo $redis->sadd (' Set ', ' horse '); Echo ' <br> '; 14 15//View all elements in the collection $set = $redis->smembers (' Set Print_r ($set); Echo ' <br> '; 18 19//delete value20 echo $redis->srem (' Set ', ' cat ') in the collection; Echo ' <br&gt ;‘; Var_dump ($redis->srem (' Set ', ' bird ')), Echo ' <br> ', $set = $redis->smembers (' Set '); _r ($set); Echo ' <br> '; 25 26//Determine if the element is a member of Set Var_dump ($redis->sismember (' Set ', ' dog ')); Echo ' <br> '; Var_dump ($redis->sismember (' Set ', ' bird ')); Echo ' <br> '; 29 30//View the number of members in the collection Echo $redis->scard (' Set '); Echo ' <br> '; 32 33//Remove and return a random element in the collection (returns the removed element) 34     echo $redis->spop (' Set '), Echo ' <br> ', Print_r ($redis->smembers (' Set ')); Echo ' <br> '; 37 38 Results/////041//142//143//144//145//Array ([0] = = Rabbit [1] = Cat [2] = Bear [3] = dog [4] = horse)//147//INT (0)//Array ([0] = dog [1] = = Rabbit [2] = horse [3] = Bear)//BOOL (TRUE)//bool (FALSE) Wuyi//452//bear53//Array ([0] =& Gt  Dog [1] = rabbit [2] = horse)
 1 <?php 2//instancing Redis 3 $redis = new Redis (); 4//Connection 5 $redis->connect (' 127.0.0.1 ', 6379); 6//Set 7 $redis->sadd (' Set ', ' horse '); 8 $redis->sadd (' Set ', ' cat '); 9 $redis->sadd (' Set ', ' dog '), $redis->sadd (' Set ', ' bird '), $redis->sadd (' Set2 ', ' fish ') $r Edis->sadd (' Set2 ', ' dog '), $redis->sadd (' Set2 ', ' bird '), Print_r ($redis->smembers (' Set ')); Echo ' &L T;br> '; Print_r ($redis->smembers (' Set2 ')); Echo ' <br> '; 17 18//Returns the intersection of the set Print_r ($redis->sinte      R (' Set ', ' Set2 '); Echo ' <br> '; 20 21//Perform the intersection operation and the result is placed in a set of $redis->sinterstore (' Output ', ' Set ', ' Set2 '); 23 Print_r ($redis->smembers (' output ')); Echo ' <br> '; 24 25//Return collection of the set of Print_r ($redis->sunion (' Set ', ' Set2 '); Echo ' <br> '; 27 28//Perform the set operation and put the result in a set of $redis->sunionstore (' Output ', ' Set ', ' Set2 '); _r ($redis->smembers (' output ')); Echo ' <br> '; 31 32//Return collectionThe difference set of Print_r ($redis->sdiff (' Set ', ' Set2 ')); Echo ' <br> '; 34 35//Perform the difference set operation and put the result in a collection $redis->sdiffs Tore (' Output ', ' Set ', ' Set2 '), PNs Print_r ($redis->smembers (' output ')); Echo ' <br> '; 38 39//Results//ARR Ay ([0] = cat [1] = dog [2] = [3] = horse) Bird//Array ([0] = = Bird [1] = dog [2] = = Fish) ([0] = bird [1] = dog)//Array ([0] = dog [1] = bird) +//Array ([0  ] = Cat [1] = dog [2] = bird [3] = horse [4] + fish)/Array ([0] = cat [1] = dog [2] = Bird [3] = horse [4] = fish)//Array ([0] = = horse [1] = cat)//Array ([0] = h  orse [1] = cat)

Sorted set (ordered set):

 1 <?php 2//instancing Redis 3 $redis = new Redis (); 4//Connection 5 $redis->connect (' 127.0.0.1 ', 6379); 6//Ordered set 7//Add Element 8 echo $redis->zadd (' Set ', 1, ' cat '); Echo ' <br> '; 9 echo $redis->zadd (' Set ', 2, ' dog '), echo ' <br> ', echo $redis->zadd (' Set ', 3, ' fish '), echo ' &LT;BR&G t; '; One echo $redis->zadd (' Set ', 4, ' dog '), echo ' <br> ', echo $redis->zadd (' Set ', 4, ' bird '), Echo ' <br& gt; '; 13 14//Returns all elements in the collection Print_r ($redis->zrange (' Set ', 0, 1)); Echo ' <br> '; Print_r ($redis->zran GE (' Set ', 0,-1, true); Echo ' <br> '; 17 18//Returns the score value of the element echo $redis->zscore (' Set ', ' dog '); Echo ' <br& gt; '; 20 21//Returns the number of stores echo $redis->zcard (' Set '); Echo ' <br> '; 23 24//delete specified member $redis->zrem (' Set ', ' Cat '); Print_r ($redis->zrange (' Set ', 0,-1)); Echo ' <br> '; 27 28//Returns the number of values between the set mediation at Min and Max Print_r ($r Edis->zcount (' Set ', 3, 5); Echo ' <br> '; 30 31//Returns the value of score between Min and Max in an ordered set of Print_r ($redis->zrangebyscore (' Set ', 3, 5)); Echo ' <br> '; Print_r ($r Edis->zrangebyscore (' Set ', 3, 5, [' Withscores ' =>true]); Echo ' <br> '; 34 35//returns all values in the specified interval in the set. Print_r ($ Redis->zrevrange (' Set ', 1, 2); Echo ' <br> '; PNs print_r ($redis->zrevrange (' Set ', 1, 2, true)); Echo ' &LT;BR&G t; '; 38 39 40//Socre of the specified value in the ordered set increases the echo $redis->zscore (' Set ', ' dog '), echo ' <br> ', $redis->zincrby (' SE T ', 2, ' dog '); Echo $redis->zscore (' Set ', ' dog '); Echo ' <br> '; 44 45//Remove score value between min and Max print  _r ($redis->zrange (' Set ', 0,-1, true)), Echo ' <br> ', Print_r ($redis->zremrangebyscore (' Set ', 3, 4)), Echo ' <br> '; Print_r ($redis->zrange (' Set ', 0,-1, true)); Echo ' <br> '; 49 50//Result 51//152//0 //054//055//056//Array ([0] = cat [1] = fish [2] = = Bird [3] = dog)//Ar Ray ([Cat] = 1 [Fish] = 3 [Bird] = 4 [dog] + 4)//459//460//Array ([0] = fish [1] = bird [2] =&gt ; Dog) 362///Array ([0] = fish [1] = bird [2] = dog) ([fish] = 3 [Bird] =&G T     4 [dog] + 4)//Array ([0] = bird [1] = fish)//Array ([Bird] = 4 [fish] + 3) 66  467//668//Array ([fish] = 3 [Bird] + 4 [dog] + 6)/////Array ([dog] = 6)

PHP Operation Redis

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.