Redis PHP Operating Manual (EXT) ____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.

# Means that the redis string 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 the key value with a validity period of 5 seconds

$ redis-> psetex ('key', 5000, 'TK'); // Set the key value with a validity period of 5000 milliseconds (same as 5 seconds)

$ redis-> setnx ('key', 'XK'); // return false if the key exists

$ redis-> delete ('key'); // delete key value You can pass array array ('key1', 'key2') to delete multiple keys

$ redis-> getSet ('key', 'XK'); // Set the value of key key to XK, and return the original value of this key value TK

// Batch transactions, no guarantee of atomicity of processing data

$ ret = $ redis-> multi ()-> set ('key1', 'val1')-> get ('key1')-> setnx ('key', 'val2')-> get ('key2') -> exec ();

// Monitor if the key is modified by other clients. If KEY is modified between the call of watch () and exec (), exec fails

$ redis-> watch ('key');

// Channel subscription

function f ($ redis, $ chan, $ msg) {

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!'); // send message.

$ redis-> exists ('key'); // verify if the key exists, return true if it exists

$ redis-> incr ('number'); // key value plus 1

$ redis-> incrby ('number',-10); // key value plus or minus 10

$ redis-> incrByFloat ('number', +/- 1.5); // key value plus decrease

$ redis-> decr ('number'); // minus 1

$ redis-> decrBy ('number', 10); // minus 10

$ mget = $ redis-> mget (array ('number', 'key')); // Get key values in batches and return an array

$ redis-> mset (array ('key0' => 'value0', 'key1' => 'value1')); // Set key values in batches

// Set key values in batch, similar to setnx () method batch operation

$ redis-> msetnx (array ('key0' => 'value0', 'key1' => 'value1'));

$ redis-> append ('key', '-Smudge'); // the original key value TK, append the value to the key value, the key value is TK-Smudge

$ redis-> getRange ('key', 0, 5); // key value interception starts from position 0 and ends at position 5

$ redis-> getRange ('key', -6, -1); // string truncation starts from -6 (the 6th position from the bottom) to -1 (the 1st position from the bottom)

// Replace the string in the key value, 0 means starting from the 0 position, how many characters replace how many positions, of which Chinese characters occupy 2 positions

$ redis-> setRange ('key', 0, 'Smudge');

$ redis-> strlen ('key'); // key length

$ redis-> getBit ('key');

$ redis-> setBit ('key');



/**hash data type **/




# redis hash is a string field and value mapping table.

# Its addition and deletion operations are O (1) (average). Hash is particularly suitable for storing objects.

$ redis-> hSet ('h', 'name', 'TK'); // add the name field in the h table value is TK

// add name field in table h, value is TK, return false if the value of field name exists, otherwise return true

$ redis-> hSetNx ('h', 'name', 'TK');

$ redis-> hGet ('h', 'name'); // Get name field value in table h

$ redis-> hLen ('h'); // Get the length of h table, that is, the number of fields

$ redis-> hDel ('h', 'email'); // delete the email field in the h table

$ redis-> hKeys ('h'); // Get all fields in h table

$ redis-> hVals ('h'); // Get all field values in table h

$ redis-> hGetAll ('h'); // Get all fields and value in table h and return an associative array (fields are key values)

$ redis-> hExists ('h', 'email'); // determine if the email field exists and the table h does not exist, return false

$ redis-> hSet ('h', 'age', 28);

// Set the value of the age field in the h table plus (-2). If value is a non-numeric value, return false. Otherwise, return the value after the operation.

$ redis-> hIncrBy ('h', 'age', -2);

// Set the age field value in table h to add (-2.6). If value is a non-numeric value, false is returned. Otherwise, the value after the operation is returned (15 decimal places are reserved).

$ redis-> hIncrByFloat ('h', 'age', -0.33);

// Table h sets fields and values in batches

$ redis-> hMset ('h', array ('score' => '80', 'salary' => 2000));

$ redis-> hMGet ('h', array ('score', 'salary')); // Table h get the values of the fields in batches



/****list linked list operations ****/




$ redis-> delete ('list-key'); // delete linked list

$ redis-> lPush ('list-key', 'A'); // Insert the head / left side of the list and return the length of the list

$ redis-> rPush ('list-key', 'B'); // Insert the tail / right of the linked list and return the length of the linked list

$ redis-> lPushx ('list-key', 'C'); // Insert into the head / left side of the linked list. If the linked list does not exist, it returns 0. If it exists, the insertion is successful and the length of the current linked list is returned.

$ redis-> rPushx ('list-key', 'C'); // Insert the tail / right side of the linked list. If the linked list does not exist, it returns 0. If it exists, the insertion is successful and the current linked list length

$ redis-> lPop ('list-key'); // Returns VALUE at the top (left) of the LIST, last-in-first-out (stack)

$ redis-> rPop ('list-key'); // Returns the VALUE at the end of the LIST (right), first-in first-out (queue)

$ redis-> blPop ();

$ redis-> brPop ();

// If it is a linked list, it returns the length of the linked list. If the empty linked list is 0, it returns false. If it is not a linked list, it returns false, judging the non-linked list. "=== false"

$ redis-> lSize ('list-key');

$ redis-> lGet ('list-key',-1); // Get the linked list element by index 0 Get the left one -1 Get the last one

$ redis-> lSet ('list-key', 0, 'X'); // Replace position 0 with X

$ redis-> lRange ('list-key', 0, 3); // Linked list truncation starts at 0 and ends at 3 positions, and the end position is -1 to get everything after the start position

$ redis-> lTrim ('list-key', 0, 1); // intercept the linked list (irreversible) starts at index 0 and ends at index

$ redis-> lRem ('list-key', 'C', 2); // delete 2 C from the left

// Insert X in front of the C element, Redis :: AfTER (indicating insert later) if the linked list does not exist, the insertion fails. Return 0. If the element does not exist, return -1.

$ redis-> lInsert ('list-key', Redis :: BEFORE, 'C', 'X');

// Pop an element from the end of the source LIST and push this element from the top (left) of the target LIST into the target LIST.

$ redis-> rpoplpush ('list-key', 'list-key2');

// The blocking version of rpoplpush, this version has a third parameter for setting the blocking time, that is, if the source LIST is empty,

// Then you can block listening to the timeout time and execute the operation if there are elements.

$ redis-> brpoplpush ();

/**set collection type **/

# set unordered set Duplicate elements are not allowed The server can implement multiple set operations

$ redis-> sMembers ('key'); // Get all elements in the container key

// (insert from the left, the last inserted element is at position 0), if the TK already exists in the collection, return false, if it does not exist, add successfully, return true

$ redis-> sAdd ('key', 'TK');

$ redis-> sRem ('key', 'TK'); // remove TK from the container

$ redis-> sMove ('key', 'key1', 'TK'); // Move the element TK in the easy key to the container key1 The operation successfully returns TRUE

$ redis-> sIsMember ('key', 'TK'); // check if VALUE is a member of the SET container

$ redis-> sCard ('key'); // Returns the number of members in the SET container

$ redis-> sPop ('key'); // Randomly return an element in the container and remove the element

$ redis-> sRandMember ('key'); // Randomly return an element in the container without removing the element

// Returns the intersection of two sets. If there is no intersection, an empty array is returned. If the parameter has only one set, the complete array corresponding to the set is returned.

$ redis-> sInter ('key', 'key1');

$ redis-> sInterStore ('store', 'key', 'key1'); // Store the intersection of collection key and collection key1 in the container store and return 1 if successful

$ redis-> sUnion ('key', 'key1'); // union of collection key and collection key1 Note that even if multiple collections have the same element, only one

// The union of the collection key and collection key1 is stored in the collection store. Note that even if multiple collections have the same element, only one is retained.

$ redis-> sUnionStore ('store', 'key', 'key1');

$ redis-> sDiff ('key', 'key1', 'key2'); // returns an array whose elements exist in the key collection and not in the key1 key2




/**zset data type **/




# (stored set) is a set of strings like set, except that each element is associated with a double type score

# The list type of redis is actually a doubly linked list where each child element is a string.

// Insert into the collection tkey, A element is associated with a score, the insertion returns 1 and the collection element cannot be repeated, if the element already exists, 0 is returned

$ redis-> zAdd ('tkey', 1, 'A');

$ redis-> zRange ('tkey', 0, -1); // Get the collection element, from position 0 to position -1

$ redis-> zRange ('tkey', 0, -1, true); // Get the elements of the set, from position 0 to -1, return an associative array with a score

array ([A] => 0.01, [B] => 0.02, [D] => 0.03); // where the decimal is from the second parameter of the zAdd method

$ redis-> zDelete ('tkey', 'B'); // remove element B in the collection tkey returns 1 on success and 0 on failure

$ redis-> zRevRange ('tkey', 0, -1); // Get the collection elements, from 0 to -1, the array is processed in descending order of score

// Get the collection element, from position 0 to position -1, the array is processed in descending order of score. Returns the score associative array.

$ redis-> zRevRange ('tkey', 0, -1, true);

// Get several tkey score elements in the interval [0,0.2], sort the score from low to high,

// elements have the same score, they will be sorted in lexicographic order, withscores control returns an associative array

$ redis-> zRangeByScore ('tkey', 0, 0.2, array ('withscores' => true));

// where 0 and 1 in the limit indicate that the eligible set is taken, starting from the 0 position and scanning backwards 1 to return the associative array

$ redis-> zRangeByScore ('tkey', 0.1, 0.36, array ('withscores' => TRUE, 'limit' => array (0, 1)));

$ redis-> zCount ('tkey', 2, 10); // Get the number of elements in tkey whose score is in the interval [2, 10]

$ redis-> zRemRangeByScore ('tkey', 1, 3); // remove the elements in tkey whose score is in the interval [1, 3] (including the boundary)

// The default element score is incremented, removing elements in tkey from 0 to -1

$ redis-> zRemRangeByRank ('tkey', 0, 1);

$ redis-> zSize ('tkey'); // returns the number of elements stored in the ordered set corresponding to the key

$ redis-> zScore ('tkey', 'A'); // returns the score of element A in the collection tkey

// Returns the index value of element A in the collection tkey, and the elements in the z collection are arranged according to the score from low to high, that is, the lowest score index is 0

$ redis-> zRank ('tkey', 'A');

$ redis-> zIncrBy ('tkey', 2.5, 'A'); // add the score of element A in the set tkey to 2.5

// Merge the collection tkey and collection tkey1 elements into the collection union, and the elements in the new collection cannot be duplicated. The number of elements in the new collection is returned.

// If element A exists in both tkey and tkey1, add the scores of the combined element A

$ redis-> zUnion ('union', array ('tkey', 'tkey1'));

// Set k1 and set k2 are combined in k02, the number of elements in array (5,1) corresponds to the sub-set, and then 5 corresponds to k1

// The score of each element of k1 must be multiplied by 5. Similarly, 1 corresponds to k2, and the score of each element of k2 is multiplied by 1.

// Then the elements are sorted in ascending order. By default, the same element score (SUM) is added.

$ redis-> zUnion ('ko2', array ('k1', 'k2'), array (5, 2));

// After each subset is multiplied by a factor, the elements are sorted in ascending order. The score of the same element takes the maximum value (MAX). You can also set MIN to the minimum value.

$ redis-> zUnion ('ko2', array ('k1', 'k2'), array (10, 2), 'MAX');

// Set k1 and set k2 intersect at k01 and sort by increasing score value. If the collection elements are the same, the score values of the elements in the new collection are added.

$ redis-> zInter ('ko1', array ('k1', 'k2'));

// Set k1 and set k2 intersect at k01, the number of elements in array (5,1) corresponds to the subset, and then 5 corresponds to k1

// The score of each element of k1 must be multiplied by 5. Similarly, 1 corresponds to k2, and the score of each element of k2 is multiplied by 1.

// Then the element scores are sorted in ascending order. By default, the same element score (SUM) is added.

$ redis-> zInter ('ko1', array ('k1', 'k2'), array (5, 1));

// After each subset is multiplied by a factor, the element scores are sorted in ascending order. The same element score can be set to the maximum value (MAX). You can also set MIN to the minimum value.

$ redis-> zInter ('ko1', array ('k1', 'k2'), array (5, 1), 'MAX');

Article Source: Jackluo


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.