Redis string Function

Source: Internet
Author: User

String Function

GET command/method/function descriptionget the value related to the specified key to obtain the value parameterskeyreturn valuestring or bool: If key didn't exist, false is returned. otherwise, the value related to this key is returned. returns the related value or bool value. If the key does not exist, false is returned. If there is a related key and value returned value. Examples $ redis-> get ('key ');
SET command/method/function descriptionset the string value in argument as value of the key. set the value to keyparameterskeyvaluetimeout (optional ). calling setex is preferred if you want a timeout. return valuebool true if the command is successful. examples $ redis-> set ('key', 'value ');
Setex command/method/function descriptionset the string value in argument as value of the key, with a time to live. psetex uses a TTL in milliseconds. set a key-value with a lifecycle. The unit of the psetex () cycle is milliseconds. Parameterskey TTL valuereturn valuebool true if the command is successful. examples $ redis-> setex ('key', 3600, 'value'); // sets key → value, with 1 h TTL.
Psetex command/method/function descriptionset the string value in argument as value of the key, with a time to live (milliseconds ). set a key-value with a lifecycle. The unit of the psetex () cycle is milliseconds. Return valuebool true if the command is successful. Examples $ redis-> psetex ('key', 100, 'value'); // sets key → value, with 0.1 sec TTL.
Setnx command/method/function descriptionset the string value in argument as value of the key if the key doesn't already exist in the database. setnx is used to set a key-value. This function first checks whether the key exists in redis. If it is not set, false is returned. Parameterskey valuereturn valuebool true in case of success, false in case of failure. examples $ redis-> setnx ('key', 'value');/* return true */$ redis-> setnx ('key', 'value '); /* return false */
DELETE command/method/function descriptionremove specified keys. remove the existing keysparametersan array of keys, or an undefined number of parameters, each a key: key1 key2 key3... keyn can be an array of keys, an undefined number parameter, or keyreturn valuelong number of keys deleted. returns the number of key-values to be deleted. Examples $ redis-> set ('key1', 'val1'); $ redis-> set ('key2', 'val2 '); $ redis-> set ('key3', 'val3'); $ redis-> set ('key4', 'val4'); $ redis-> Delete ('key1 ', 'key2');/* return 2 */$ redis-> Delete (Array ('key3', 'key4');/* return 2 */
GetSet command/method/function descriptionsets a value and returns the previous entry at that key. Set a value and return the current value of the key. Parameterskey: keystring: valuereturn valuea string, the previous value located at this key. example $ redis-> set ('x', '42'); $ exvalue = $ redis-> GetSet ('x', 'lol '); // return '42', replaces X by 'lol '$ newvalue = $ redis-> get ('x')' // return 'lol'
Multi, exec, Discard command/method/function descriptionenter and exit transactional mode. enter and exit the transaction processing mode. Parameters (optional) redis: Multi or redis: Pipeline. defaults to redis: Multi. A redis: multi block of commands runs as a single transaction; A redis: Pipeline block is simply transmitted faster to the server, but without any guarantee of atomicity. discardcancels a transaction. the Parameter options of the multi function are redis: Multi or redis: Pipeline. the default parameter is redis: Multi. Redis: multi processes multiple operations as one transaction. Redis: pipeline serves as a simple and fast processing channel for the server to process, but it does not guarantee the atomicity of data processing. The discard () function cancels a transaction processing mode. Return valuemulti () returns the redis instance and enters multi-mode. once in multi-mode, all subsequent method callreturn the same object until exec () is called. multi () returns a redis instance and enters the transaction processing mode (batch processing ). When you enter the transaction processing mode, all method calls will return the same redis instance until exec () is called to execute the transaction. Example $ ret = $ redis-> multi ()-> set ('key1', 'val1')-> get ('key1')-> set ('key2 ', 'val2')-> get ('key2')-> exec ();/* $ ret = array (0 => true, 1 => 'val1 ', 2 => true, 3 => 'val2 ');*/
Watch command/method/function descriptionwatches a key for modifications by another client. if the key is modified between watch and exec, the multi/exec transaction will fail (return false ). monitors whether a key is modified by other clients. If the key is modified between the Watch () and exec () calls, batch processing of the final exec () execution will fail. Through some experiments, the effect of this function is not so good, or it cannot be accurately monitored. Parameterskeys: A list of keysexample $ redis-> Watch ('x '); /* long code here during the execution of which other clients cocould well modify 'X' */$ ret = $ redis-> multi ()-> incr ('x ') -> exec ();/* $ ret = false if X has been modified between the call to watch and the call to Exec. */
Unwatch command/method/function descriptionunwatch cancels all the watching of all keys by this client. unwatch () Cancel monitoring of all key values for this redis instance. Through some experiments, the effect of this function is not so good, or it cannot be accurately monitored.
SUBSCRIBE command/method/function descriptionsubscribe to channels. Warning: this function will probably change in the future. Method callback function. Note: This method may be modified in the future. Parameterschannels: an array of channels to subscribe tocallback: either a string or an array ($ instance, 'Method _ name '). the callback function parameters Es 3 parameters: the redis instance, the channel name, and the message. examplefunction F ($ redis, $ Chan, $ MSG) {Switch ($ Chan) {Case 'Chan-1 ':... break; Case 'Chan-2 ':... break; Case 'Chan-2 ':... break; }}$ redis-> subscribe (Array ('Chan-1', 'Chan-2', 'Chan-3'), 'F '); // subscribe to 3 Chans
Publish command/method/function descriptionpublish messages to channels. Warning: this function will probably change in the future. Publish messages to the channel. Warning: This feature may change in the future. Parameterschannel: a channel to publish tomesssage: stringexample $ redis-> Publish ('Chan-1', 'Hello, world! '); // Send message.
Exists command/method/function descriptionverify if the specified key exists. Verify whether a specified key exists. Parameterskeyreturn valuebool: if the key exists, return true, otherwise return false. bool: if the key exists, true is returned; otherwise, false is returned. Examples $ redis-> set ('key', 'value'); $ redis-> exists ('key '); /* true */$ redis-> exists ('nonexistingkey');/* false */
Incr command/method/function descriptionincrement the number stored at key by one. if the second argument is filled, it will be used as the integer value of the increment. the value of the specified key is increased by 1. How to fill in the second parameter will automatically add the second parameter to the key value. Parameterskeyvalue: value that will be added to key (only for incrby) return valueint the new value returns the new int value examples $ redis-> incr ('key1 '); /* key1 didn't exists, set to 0 before the increment if key1 does not exist, the default value before auto increment is 0 * // * and now has the value 1 after incr is executed, it is now 1 */$ redis-> incr ('key1');/* 2 */$ redis-> incr ('key1');/* 3 */
The incrby command/method/function description adds num to the key value, for example, incrby ("AB", 10), which is equivalent: AB + 10 return valueint the new value returns the new int value examples $ redis-> incrby ('key1', 10);/* If the value of key1 is 8, the result is: 18 */
Incrbyfloat command/method/function increment the key with floating point precision. Add a floating point value. Parameterskeyvalue: (float) value that will be added to the keyreturn valuefloat the new valueexamples $ redis-> incrbyfloat ('key1', 1.5);/* key1 didn't exist, so it will now be 1.5 */$ redis-> incrbyfloat ('key1', 1.5);/* 3 */$ redis-> incrbyfloat ('key1',-1.5 ); /* 1.5 */$ redis-> incrbyfloat ('key1', 2.5);/* 3.5 */
Decr command/method/function minus 1 from the value of the specified key (if you want to subtract the specified value, you can use decrby) parameterskeyvalue: value that will be substracted to key (only for decrby) return valueint the new valueexamples $ redis-> decr ('key1');/* key1 didn't exists, set to 0 before the increment * // * and now has the value-1 */$ redis-> decr ('key1 '); /*-2 */$ redis-> decr ('key1');/*-3 */
Decrby command/method/function minus the specified value. Return valueint the new valueexamples $ redis-> set ('key1', 50); $ redis-> decrby ('key1', 10);/* result: 40 */
Mget command/method/function descriptionget the values of all the specified keys. if one or more keys dont exist, the array will contain false at the position of the key. obtain the values of all specified keys. If one or more keys do not exist, the returned array will be filled with false at the corresponding keys position. Parametersarray: array containing the list of the keys array: Return valuearray: array containing the values related to keys in argument array: return the corresponding keys value examples $ redis-> set ('key1', 'value1 '); $ redis-> set ('key2', 'value2 '); $ redis-> set ('key3', 'value3'); $ redis-> mget (Array ('key1', 'key2', 'key3 ')); /* array ('value1', 'value2', 'value3'); $ redis-> mget (Array ('key0', 'key1', 'key5 ')); /* array ('false', 'value2', 'false ');
APPEND Command/method/function descriptionappend specified string to the string stored in specified key. Add the specified string to the specified string key. Parameterskey valuereturn valueinteger: size of the value after the append returns the sizeexample $ redis-> set ('key', 'value1 ') of the added key '); $ redis-> append ('key', 'value2');/* 12 */$ redis-> get ('key');/* 'value1value2 '*/
Getrange command/method/function getrange (substr also supported but deprecated in redis) descriptionreturn a substring of a larger string returns a portion of parameterskey start endreturn valuestring of the string: the substringexample $ redis-> set ('key', 'string value'); $ redis-> getrange ('key', 0, 5 ); /* 'string' */$ redis-> getrange ('key',-5,-1);/* 'value '*/
Setrange command/method/function descriptionchanges a substring of a larger string. modify part of the string. Parameterskeyoffsetvaluereturn valuestring: the length of the string after it was modified. example $ redis-> set ('key', 'Hello World'); $ redis-> setrange ('key', 6, "redis "); /* returns 11 */$ redis-> get ('key');/* "Hello redis "*/
Strlen command/method/function descriptionget the length of a string value. Return the length of the string. Parameterskeyreturn valueintegerexample $ redis-> set ('key', 'value'); $ redis-> strlen ('key');/* 5 */
Getbit command/method/function descriptionreturn a single bit out of a larger string returns a large string parameterskeyoffsetreturn valuelong: The bit value (0 or 1) example $ redis-> set ('key', "\ x7f"); // This is 0111 $ redis-> getbit ('key', 0 ); /* 0 */$ redis-> getbit ('key', 1);/* 1 */
Setbit command/method/function descriptionchanges a single bit of a string. parameterskeyoffsetvalue: bool or int (1 or 0) return valuelong: 0 or 1, the value of the bit before it was set. example $ redis-> set ('key', "*"); // ord ("*") = 42 = 0x2f = "0010 1010" $ redis-> setbit ('key', 5, 1);/* returns 0 */$ redis-> setbit ('key ', 7, 1);/* returns 0 */$ redis-> get ('key');/* CHR (0x2f) = "/" = B ("0010 1111 ") */
Bitop command/method/function descriptionbitwise operation on multiple keys. bitwise operations are performed on one or more string keys that save binary bits, and the results are saved to the destkey. Parametersoperation: either "and", "or", "not", "XOR" ret_key: Return key1key2... return valuelong: the size of the string stored in the destination key.
Bitcount command/method/function descriptioncount bits in a string. parameterskeyreturn valuelong: The number of BITs set to 1 in the value behind the Input key. example61421888
Mset command/method/function descriptionsets multiple key-value pairs in one atomic command. msetnx only returns true if all the keys were set (see setnx). set multiple key-values in batches. If all keys are set successfully, if these key-values are set successfully, msetnx will return only one ture, and if one is an existing key, all operations are not executed. Parameterspairs: array (Key => value ,...) return valuebool true in case of success, false in case of failure. example $ redis-> mset (Array ('key0' => 'value0', 'key1' => 'value1 ')); var_dump ($ redis-> get ('key0'); var_dump ($ redis-> get ('key1'); output: string (6) "value0" string (6) "value1"

Redis string Function

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.