Redis v Data type operations

Source: Internet
Author: User
Tags hash redis strlen redis cluster
String TypeSet/get is the most common and simplest operation. Key value pair; [Plain]View plain copy 192.168.159.130:7000> set num 1 OK 192.168.159.130:7000> get num "1" 192.168.159.130:7000&gt ; Get Age (Nil)
SETNX differs from set if the key value is present, does not overwrite return 0, and if the key value does not exist, set the value. and return 1 (NX not exist) [Plain]View plain copy 192.168.159.130:7000> setnx num 1 (integer) 0 192.168.159.130:7000> get num "1" 192.168.159 .130:7000> setnx Age 1 (integer) 1 192.168.159.130:7000> get Age "1"
Setex differs from set in that it can add an expiration time to the data. Set Key Time value [Plain]View plain copy 192.168.159.130:7000> Setex num 5 1 OK 192.168.159.130:7000> get num "1" 192.168.159.130:700 0> Get num "1" 192.168.159.130:7000> get num (nil)
SetRange the same set can replace the value value SetRange key The length of the substitution for the replacement of the start index substitution is determined by the length of the replacement value. Insufficient length, original value supplement [Plain]View Plain copy 192.168.159.130:7001> set name HelloWorld OK 192.168.159.130:7001> SETRANGE name 1 T (integer) 192.168.159.130:7001> Get Name "Htlloworld" 192.168.159.130:7001> setrange name 0 helloworld (integer) 1 0 192.168.159.130:7001> Get Name "HelloWorld"
Mset set multiple values at once with set (feature either all succeeds or fail all) set Key1 value1 key2 value2 ... There is no demo here (because my environment is a redis cluster, when setting multiple values, Redis is likely to report the Crossslot Keys in request Don's hash to the same slot exception. For reasons please see next article on Redis) gets Et same set will simply return the original value of key without returning nil [Plain]View Plain Copy 192.168.159.130:7000> Getset age 1 "2" 192.168.159.130:7000> Getset age 2 "1" 192.168.159.13 0:7000> Getset take 2 (nil) 192.168.159.130:7000> Getset take 1 "2"
GetRange gets a portion of the value GetRange key start index end index [Plain]View Plain copy 192.168.159.130:7001> GETRANGE name 0 (Error) ERR wrong number of arguments for ' GETRANGE ' command 192.168.159.130:7001> GETRANGE name 0 4 "Hello" 192.168.159.130:7001> GETRANGE name 0 "HelloWorld"
Mget with Get feature is a value that goes back to multiple key locks at once. Mget key1 Key2 ... There is no demo here. Reason with Mset incr increase the value of the corresponding key INCR key returns the added value [Plain]View plain copy 192.168.159.130:7001> get name "HelloWorld" 192.168.159.130:7001> INCR name (error) ERR value is a integer or out of range 192.168.159.130:7001> set name 1 OK 192.168.159.130:7001> INCR name (inte GER) 2
Incrby the same incr feature is that you can increase the value of the key corresponding to the value you have given. If the key value does not exist directly set the key value (INCR is the same) [Plain]View Plain copy 192.168.159.130:7003> get student (nil) 192.168.159.130:7001> Incrby Student (integer) 10 192.168.159.130:7001> get Student "Ten" 192.168.159.130:7001> Incrby Student 2 (integer) 12
DECR and Decrby do not introduce here. The same incr append is equivalent to the StringBuffer method in Append in Java. append strings backwards [Plain]View Plain copy 192.168.159.130:7001> set name AA ok 192.168.159.130:7001> get name "AA" 192.168.159.130:700 1> APPEND name BB (integer) 4 192.168.159.130:7001> get Name "Aabb"
Strlen gets the value of the object key length [Plain]View plain copy 192.168.159.130:7001> get name "Aabb" 192.168.159.130:7001> strlen name (integer) 4 192.168 .159.130:7001> set name 1 OK 192.168.159.130:7001> STRLEN name (integer) 2.HASH Dictionary typeHset key field value (a property of the set or new object), Hget key field (Gets the individual property value of the object), Hsetnx (the same string as the Setnx setting property value if a value is not performed) [Plain]View plain copy 192.168.159.130:7000> hset person Age (integer) 1 192.168.159.130:7000> hget person age "10 "192.168.159.130:7000> hsetnx Person age 5 (integer) 0 192.168.159.130:7000> hsetnx person sex 1 (integer) 1 192.168.159.130:7000> hget person sex "1"
Hmset key field1 value1 field2 value2 ... (Bulk set object properties), Hmget key Field1 field2. (Bulk Get object property values), Hgetall key (get all properties of the object and attribute values) [Plain]View plain copy 192.168.159.130:7001> hmset person name xiaoming age 5 Sex 1 OK 192.168.159.130:7000> hmget Pers   On name (age sex 1) "xiaoming" 2) "5" 3) "1" 192.168.159.130:7000> hgetall person 1) "name" 2) "Xiaoming" 3) "Age" 4) "5" 5 "Sex" 6) "1"
Hexists key field (determines whether a property exists), Hlen key (returns the number of object property bars), Hdel key field (removes the specified attribute), Hkeys key (returns all property names of the object), Hvals key (returns all property values of the object) [Plain]View plain copy 192.168.159.130:7000> hincrby person sex 1 (integer) 2 192.168.159.130:7000> hexists person Hobb Y (integer) 0 192.168.159.130:7000> hlen person (integer) 3 192.168.159.130:7000> hkeys (error) ERR wrong Number of arguments for ' hkeys ' command 192.168.159.130:7000> hkeys person 1) "name" 2) "age" 3) "Sex" 192.16    8.159.130:7000> hvals person 1) "Xiaoming" 2) "5" 3) "2" 192.168.159.130:7000> hdel person Sex (integer) 1 192.168.159.130:7000> hget person Sex (nil) 3.LIST queue data typeLpush Key value1 value2 ... (insert data from the head sequentially), Rpush key value1,value2. (insert data sequentially from tail), Lrange key start index end index (query element 0-1 in the specified index range indicates all) [Plain]View plain copy 192.168.159.130:7000> Lpush mylist A-redirected to slots [5282] located at 192.168.159.130:7001  (integer) 1 192.168.159.130:7001> lpush mylist B (integer) 2 192.168.159.130:7001> lrange 0-1 (Error) ERR Wrong number of arguments for ' lrange ' command 192.168.159.130:7001> lrange mylist 0-1 1) "B" 2) "a" 192.168. 159.130:7001> Rpush mylist C (integer) 3 192.168.159.130:7001> lrange mylist 0-1 1) "B" 2) "a" 3) "C" l Insert key value Before/after the value to be inserted (insert value before and after the specified value) [Plain]View plain copy 192.168.159.130:7001> Lrange mylist 0-1 1) "B" 2) "a" 3) "C" 192.168.159.130:7001> Linsert MyList before a D (integer) 4 192.168.159.130:7001> lrange mylist 0-1 1) "B" 2) "D" 3) "a" 4) "C" 192.16   8.159.130:7001> Linsert MyList after a E (integer) 5 192.168.159.130:7001> lrange mylist 0-1 1) "B" 2) "D" 3) "a" 4) "E" 5) "C" LSet key position value (replace the value of the custom seat) [Plain]View plain Copy 192.168.159.130:7000> LSet mylist 1 F OK 192.168.159.130:7001> lrange mylist 0-1 1) "B" 2) "F" 3) "a" 4) "E" 5) "C"
Lrem Key Number value (delete the element with value in the queue, the number is positive from the beginning to delete, the number is negative from the beginning of the deletion, the number is 0 delete all [Plain] View plain copy 192.168.159.130:7001> lrange mylist 0 -1    1)   "B"     2)   "a"     3)   "B"     4)   "a"     5)   "a"     6)   "B"     7)   "F"     8)   "a"     9)   "E"      "C"    192.168.159.130:7001> lrem mylist 1  a   (integer)  1   192.168.159.130:7001> lrange mylist 0 -1    1)   "B"    2)   "B"    3)   "a"    4)   "a"    5)   "B"    6)   "F"    7)   "a"    8)   "E"    9)   "C"     192.168.159.130:7001> lrem mylist -1 a   (integer)  1   192.168.159.130:7001> lrange mylist 0 -1   1)   "B"    2)   "B" & NBsp;  3)   "a"    4)   "a"    5)   "B"    6)   "F"    7)   "E "   8)  " C "   192.168.159.130:7001> lrem mylist 0 b   ( Integer)  3   192.168.159.130:7001> lrange mylist 0 -1   1)   "A"    2)   "a"    3)   "F"    4)   "E"    5)   "C"    LTrim keep data within specified range   successful return OK

[plain] view plain copy 192.168.159.130:7001> lrange mylist 0 -1    1)   "a"    2)   "a"    3)   "F"    4)   "E"    5)   "C"    192.168.159.130:7001> ltim mylist 1 2   (Error)  ERR  unknown command  ' Ltim '    192.168.159.130:7001> ltrim mylist 1 2    ok   192.168.159.130:7001> lrange mylist 0 -1   1)   " A "   2)  " F "  
Llen returns the list number

[plain] view plain copy 192.168.159.130:7001> ltrim mylist 3 7    ok   192.168.159.130:7001> lrange mylist 0 -1   (empty  List or set)    192.168.159.130:7001> llen mylist   (integer)  0    Lpop and Rpop   The former is a data popup from the head   The latter is a data popup from the tail [plain] view plain copy 192.168.159.130:7001> lpop mylist   (nil)    192.168.159.130:7001> lpush  mylist a   (integer)  1   192.168.159.130:7001> lpush mylist  b   (integer)  2   192.168.159.130:7001> lrange mylist 0 -1    1)   "B"    2)   "a"    192.168.159.130:7001> rpop mylist    "A"    lindex (returns the data for the specified index) Rpoplpush (the previous list trailing data is pressed into a list header, this method if the cluster is not suitable)

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.