PHP Operation Redis Reference manual ____php

Source: Internet
Author: User
Tags delete key redis
String type Operation

String is the most basic type of Redis, and the string type is binary safe. A string that means Redis can contain any data. such as JPG images or serialized objects.

$redis->set (' key ', ' TK ');
$redis->set (' number ', ' 1 ');
$redis->setex (' key ', 5, ' TK '); Set a key value that is valid for 5 seconds
$redis->psetex (' key ', 5000, ' TK '); Set the key value for a period of 5000 milliseconds (same 5 seconds)
$redis->setnx (' key ', ' XK '); Returns true if a key value exists that returns false does not exist
$redis->delete (' key '); Delete key values can pass in array (' Key1 ', ' Key2 ') to delete multiple keys
$redis->getset (' key ', ' XK '); Sets the value of the key keys to XK and returns the value of the original key value TK
$ret = $redis->multi ()  //Bulk transaction processing, which does not guarantee atomicity of data processing ('
        key1 ', ' val1 ')
        ->get (' Key1 ')
        -> Setnx (' key ', ' Val2 ')
        ->get (' Key2 ')
        ->exec ();
$redis->watch (' key ');   Whether the monitor key is modified by another client
                           if key is modified between calling watch () and EXEC (), Exec fails
function f ($redis, $chan, $msg) {  //channel subscription
    switch ($chan) {case
        ' chan-1 ':
            echo $msg;
            break;

        Case ' chan-2 ':
            echo $msg;
            break;

        Case ' chan-2 ':
            echo $msg;
            break;
    }
}

$redis->subscribe (Array (' chan-1 ', ' chan-2 ', ' chan-3 '), ' f '); Subscribe to 3 Chans

$redis->publish (' chan-1 ', ' Hello, world! '); 
$redis->exists (' key '); Verify that the key exists and that there is a return true
$redis->incr (' number '); Key value plus 1
$redis->incrby (' number ',-10); Key value plus minus 10
$redis->incrbyfloat (' number ', +/+ 1.5); Key value plus minus decimal
$redis->DECR (' number '); Key value minus 1
$redis->decrby (' number ', 10); Key value minus 10
$mget = $redis->mget (Array (' number ', ' key ')); Bulk gets the key value, returns an array
$redis->mset (Array (' Key0 ' => ' value0 ', ' key1 ' => ' value1 ')); Set key values in bulk
$redis->msetnx (Array (' Key0 ' => ' value0 ', ' key1 ' => ' value1 ')); 
                                        Bulk set key value, similar to bulk operation of Setnx () method
$redis->append (' key ', '-smudge '); The original key value TK, append the value to the key value after the key value is Tk-smudge
$redis->getrange (' key ', 0, 5); Key-value interception starts at 0 position to 5-bit end
$redis->getrange (' key ',-6,-1); String interception begins with-6 (6th position) to 1 (last 1th position) end
$redis->setrange (' key ', 0, ' Smudge '); 
                                    The replacement string in the key value, 0 indicates how many
                                       characters to replace the position from the 0 position, where the Chinese character occupies 2 positions
$redis->strlen (' key '); Key value length
$redis->getbit (' key ');
$redis->setbit (' key ');
list Linked list Operation
$redis->delete (' List-key '); Delete Linked list
$redis->lpush (' List-key ', ' A '); Insert list head/left, return list length
$redis->rpush (' List-key ', ' B '); Insert the tail/right side of the linked list and return the list length
$redis->lpushx (' List-key ', ' C '); 
                 Insert list head/left, List does not exist return 0, existence is insert success, return the current list length
$redis->rpushx (' List-key ', ' C '); 
                 Insert the tail/right side of the list, the list does not exist return 0, the existence is inserted successfully, return the current list length
$redis->lpop (' List-key '); Returns value at the top (left) of the list, followed by first out (stack)
$redis->rpop (' List-key '); Returns the value of the tail of the list (right), first in first out (queue)
$redis->blpop ();
$redis->brpop ();
$redis->lsize (' List-key '); 
                    If the list is linked to return the length of the list, the empty list returns 0 if the 
                       linked list or not empty, return false, to determine the non-linked list "= = False"                          
$redis->lget (' List-key ',-1); Get the list element 0 by index get the left one  -1 get the last one
$redis->lset (' List-key ', 0, ' X '); 0-bit element replaced by X
$redis->lrange (' List-key ', 0, 3); 
                    The list is truncated from 0 to 3 and the end position is-1 gets all of the start position
$redis->ltrim (' List-key ', 0, 1); Intercept list (irreversible) from 0 index start 1 index end 
$redis->lrem (' List-key ', ' C ', 2); The list deletes the element 2 C from the left start
$redis->linsert (' List-key ', Redis::before, ' C ', ' X '); 
                    Insert x before C element  , Redis::after (indicates after insertion) 
                       list does not exist then insert failure returns 0 if element does not exist return-1
$redis->rpoplpush (' List-key ', ' List-key2 '); 
                    Eject an element from the end of the source list
                      and press the element from the top (left) of the target list into the target list. 
$redis->brpoplpush ();
                    Rpoplpush blocked version, this version has a third parameter to set the blocking time that
                      is, if the source list is empty, you can block the time to listen for timeout, and if there is an element, perform the action.
Set Collection type
Set unordered set does not allow duplicate element server side can implement multiple set operations
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.