Redis Series-Storage article string main operation function Summary

Source: Internet
Author: User
Tags redis redis server

Through the introduction of two articles, our Redis server basically ran. DB has the most basic crud capabilities, and we're going to start learning the Redis rich data structure journey along this thread, starting with the simplest and most common string, of course.

1, new

A) Set

Syntax: Set key value

Explanation: Assign value to key, add if key does not exist, otherwise, update

[root@xsf001 ~]# redis-cli 
redis 127.0.0.1:6379> set User.1.name Zhangsan #设置user. 1.name to Zhangsan
OK
Redis 127.0.0.1:6379> set User.Name      #设置user. 1.name is
OK
b) setnx

Syntax: Setnx key value

Explanation: Insert not update only, that is, if only key does not exist, then set the value of key and return 1, otherwise return 0. SETNX is the abbreviation for set if not exists

Redis 127.0.0.1:6379> setnx user.1.name zhangsan   #user. 1.name already exists, returns 0
(integer) 0
Redis 127.0.0.1:6379> setnx user.2.name Zhangsan  #user. 2.name does not exist, set
(integer) 1
c) Setex

Syntax: Setex key seconds value

Explanation: Sets the expiration time and value of the key. The expiration Time seconds the unit is seconds. Setting the expiration time and value is an atomic operation, which is very useful if Redis is only a cache.

Redis 127.0.0.1:6379> Setex user.2.age 2  #把user 2.age value set 14 and 2 seconds after expiration
OK
Redis 127.0.0.1:6379> get User.2.age  #失效前
"Redis"
127.0.0.1:6379> get user.2.age #失效后
(nil)
D) mset

Syntax: Mset key value [key value ...]

Explanation: Set multiple key-value at the same time

Redis 127.0.0.1:6379> mset user.4.name Lisi user.4.age  #设置user. 4.name=lisi,user.4.age=34
OK
Redis 127.0.0.1:6379> get User.4.name
"Lisi"
redis 127.0.0.1:6379> get user.4.age
"34"

e) msetnx

Syntax: msetnx key value [key value ...]

Explanation: No key exists to perform a set operation

Redis 127.0.0.1:6379> msetnx user.4.name Lisi user.4.age  #key are set and cannot be set again
(integer) 0
Redis 127.0.0.1:6379> msetnx user.4.name Lisi user.4.std 3   #key User.4.name was set up and cannot be set again
(integer) 0
Redis 127.0.0.1:6379> msetnx User.4.tech Lisi user.4.std 3   #key都没有设置过, can be set again
(integer) 1

2, inquiry

a) Get

Syntax: Get key

Explanation: Gets the value set by the key

Redis 127.0.0.1:6379> get User.4.name  #获取user. 4.name value
"Lisi"
Redis 127.0.0.1:6379> get User.4.age
"Redis"
127.0.0.1:6379> get User.4.tech
"Lisi"
Redis 127.0.0.1:6379> get USER.4.STD
"3"
b) mget

Syntax: Get key [key]

Explanation: Bulk Gets the value of the key. The program gets more than one value at a time to reduce network connection loss.

Redis 127.0.0.1:6379> mget user.4.name user.4.age user.4.std #批量获取存在key的值
1) "Lisi"   #user. 4.name value
2) " "     #user 4.age value
3)" 3 "      #user. 4.std value

redis 127.0.0.1:6379> mget user.4.name user.4.age USER.4.STD User.4.fri  #key User.4.fri does not exist can still return
1) "Lisi"
2) "
3" 3 "4"
(nil) #user. 4.fri value
c) GetRange

Syntax: GetRange key star End

Explanation: Gets the string stored in key value. The intercept of the string has star and end, the first character number of the string is 0, the second is 1, and the other is negative,-1 is the last character,-2 is the penultimate character, and then the second.

Redis 127.0.0.1:6379> get User.4.name
"Lisi" Redis 127.0.0.1:6379> getrange user.4.name
0 3  # 0 means start c3/> "Lisi"
redis 127.0.0.1:6379> getrange user.4.name 1 2
"is"
Redis 127.0.0.1:6379> GetRange User.4.name 1-2 #-2 represents the penultimate
"is"
redis 127.0.0.1:6379> getrange user.4.name-1-2  # End start
"" 
  redis 127.0.0.1:6379> getrange user.4.name 1  #end "The length of the
ISI"
3, modify

A) Getset

Syntax: Getset key value

Explanation: Sets the value of the key and returns the old value of the key.

Redis 127.0.0.1:6379> get user.4.name   #存在的key
"Lisi" Redis 127.0.0.1:6379> getset user.4.name
Wangwu  #把存在的user. 4.name is set to Wagnwu
"Lisi"
redis 127.0.0.1:6379> get user.4.name  
"Wangwu"
Redis 127.0.0.1:6379> get user.5.name  #不存在的key
(nil)
Redis 127.0.0.1:6379> User.5.name Lisi
(nil)
Redis 127.0.0.1:6379> get user.5.name
"Lisi"
b) Append

Syntax: Append key value

Explanation: Key exists, append Value;key does not exist after old value, direct set

Redis 127.0.0.1:6379> get user.4.name #存在的key
"Wangwu" Redis 127.0.0.1:6379> append user.4.name
( Integer) 8
redis 127.0.0.1:6379> get user.4.name
"wangwu01"
Redis 127.0.0.1:6379> Get user.6.name #不能存在的key
(nil) redis 127.0.0.1:6379> append user.6.name
Jim
( Integer) 3
redis 127.0.0.1:6379> get User.6.name
"Jim"
c) SetRange

Syntax: SetRange key Offset value

Explanation: Overrides part of key value with value, offset specified by offset

Redis 127.0.0.1:6379> get user.4.name  #key存在
"wangwu01" Redis 127.0.0.1:6379> setrange user.4.name
0 Lisi
(integer) 8
redis 127.0.0.1:6379> get user.4.name
"lisiwu01"
Redis 127.0.0.1:6379> SetRange User.4.name 9 Lisi  # Offset string length
(integer)
redis 127.0.0.1:6379> get User.4.name
" Lisiwu01\x00lisi "
redis 127.0.0.1:6379> setrange user.4.name 8 Lisi
(integer)
Redis 127.0.0.1:6379> get user.4.name
' lisiwu01lisii '
redis 127.0.0.1:6379> get user.6.std #key does not exist
( Nil)
Redis 127.0.0.1:6379> setrange user.6.std 0 3
(integer) 1
redis 127.0.0.1:6379> get USER.6.STD
"3"
D) incr

Syntax: INCR key

Explanation: If you store a number in key, you can return the incremented value by incr the value of the key. If key cannot exist, consider the initial value to be 0

Redis 127.0.0.1:6379> Get User.7.age #key不存在, the initial value is treated as 0,
(nil) Redis, 127.0.0.1:6379> incr user.7.age 
( Integer) 1
redis 127.0.0.1:6379> get user.7.age
"1"
redis 127.0.0.1:6379> incr user.7.age
( Integer) 2

e) Incrby

Syntax: Incrby key increment

Explanation: Increases the number of key stores with the specified step size. If the step increment is a negative number, subtract

Redis 127.0.0.1:6379> Get User.7.age
"3"
redis 127.0.0.1:6379> incrby user.7.age  #增加15
( Integer)
redis 127.0.0.1:6379> get user.7.age
"a"
redis 127.0.0.1:6379> incrby User.7.fri-18< c14/> #key不能存在, the original value is treated as 0
(integer)
redis 127.0.0.1:6379> get User.7.fri
"
Redis" 127.0.0.1:6379> Incrby  user.7.age-1  #步长为负
(integer)
redis 127.0.0.1:6379> get User.7.age
"17"

f) DECR

Syntax: DECR key

Explanation: Decrements key saved number, if key does not exist, the initial value is treated as 0

Redis 127.0.0.1:6379> get user.7.age
redis 127.0.0.1:6379> decr user.7.age
(integer)
Redis 127.0.0.1:6379> decr user.7.num #key does not exist, the initial value as 0
(integer)-1 redis 127.0.0.1:6379> DECR
c6/> (integer)-2

g) Decrby

Syntax: Decrby key Decrement

Explanation: Decrements the value of the key with the specified step size, or increments if the step decrment is negative

Redis 127.0.0.1:6379> Decrby user.7.num 4  #递减
(integer)-6
redis 127.0.0.1:6379> Decrby user.7.num-9 #负值, incrementing
(integer) 3

Note: The function that increments the descending series can only be a key operation that holds numbers, not a string

4) Delete

Syntax: Del key [key]

Explanation: Deletes the specified key and returns the number of key deletions

Redis 127.0.0.1:6379> del user.7.num
(integer) 1
redis 127.0.0.1:6379> get user.7.num
(nil)
Redis 127.0.0.1:6379> del user.7.age user.7.fri #删除多个key
(integer) 2
redis 127.0.0.1:6379> mget User.7.age User.7.fri
1) (nil)
2) (nil)

5) Other

Syntax: Strlen key

Explanation: Gets the length of the value stored in the key

Redis 127.0.0.1:6379> Get User.1.name
"a"
redis 127.0.0.1:6379> strlen user.1.name  #user 1.name Length
(integer) 2
redis 127.0.0.1:6379> strlen user.8.name #key不存在
(integer) 0

With a large number of demos above, the operation of string is basically summed up. Let's get here today.

For more information, please refer to: http://redis.io/commands#string

If the feeling is helpful to you, please powder Sina Weibo : Http://weibo.com/lovecoder


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.