Redis02 is fully resolved using the Redis database of String type

  • 2020-05-12 06:25:15
  • OfStack

1 String type

Start the server process using:

redis-server.exe

1. Set

Set the value of Key to value of type String.

Example: insert a record with data type String into the Redis database.

Enter commands on the client side:


C:\software\redis\64bit>redis-cli.exe -h 127.0.0.1 -p 6379
redis 127.0.0.1:6379> set foo test
OK
redis 127.0.0.1:6379> get foo
"test" 

2.setnx

Set key to value of type string. If key already exists, return 0. nx means not exist. The command simply reads: set 1 key, and update the value only if the corresponding value of key does not exist.


redis 127.0.0.1:6379> setnx foo1 aaa
(integer) 1
redis 127.0.0.1:6379> get foo1
"aaa"
redis 127.0.0.1:6379> setnx foo1 bbb
(integer) 0 

3. setex

Set the value of key to value of type string, and specify the expiration date for this key value.

For example, add a key-value pair of haircolor=red and specify an expiry of 10 seconds.


redis 127.0.0.1:6379> setex haircolor 10 red
OK
redis 127.0.0.1:6379> get haircolor
"red"
redis 127.0.0.1:6379> get haircolor
(nil) 

4 setrange

Sets the substring of value that specifies key

For example, we want to replace the 126 mailbox of xpxiaowu with the gmail mailbox.


redis 127.0.0.1:6379> set name abcde@126.com
OK
redis 127.0.0.1:6379> setrange name 6 gmail.com
(integer) 15
redis 127.0.0.1:6379> get name
"abcde@gmail.com" 

5.mset

Multiple values of key are set once. If ok is returned successfully, all values are set. If ok is returned successfully, no values are set.


redis 127.0.0.1:6379> mset key1 wangwu key2 lisi
OK
redis 127.0.0.1:6379> get key1
"wangwu"
redis 127.0.0.1:6379> get key2
"lisi" 

6.msetnx

Multiple values of key are set once, and ok is returned on success, indicating that all values have been set; 0 is returned on failure, indicating that no values have been set, but the existing key will not be overwritten.

If one key is not set successfully, the other key will not be set successfully.


redis 127.0.0.1:6379> msetnx key1 aaa key2 bbb key3 ccc
(integer) 0
redis 127.0.0.1:6379> get key1
"wangwu"
redis 127.0.0.1:6379> get key2
"lisi"
redis 127.0.0.1:6379> get key3
(nil)
redis 127.0.0.1:6379> msetnx key3 aaa key4 bbb key5 ccc
(integer) 1
redis 127.0.0.1:6379> get key1
"wangwu"
redis 127.0.0.1:6379> get dkey2
(nil)
redis 127.0.0.1:6379> get key3
"aaa"
redis 127.0.0.1:6379> get key4
"bbb"
redis 127.0.0.1:6379> get key5
"ccc"

7. get can be obtained on the due string value, if key does not exist, return nil.

getset sets the value of key and returns the old value of key.

getrange

Gets the substring of the value value corresponding to key.

getrange name 0 5
mget

Get multiple values of key once, and return nil if the corresponding key does not exist.

redis 127.0.0.1:6379 > mget key1 key2 key3 key4 key5

1) "wangwu"
2) "lisi"
3) "aaa"
4) "bbb"
5) "ccc"

8. incr

Add to the value of key and return the new value.

incrby

Similar to incr, add the specified value, and when key does not exist, key will be set, and the original value will be considered to be 0.


redis 127.0.0.1:6379> set key6 0
OK
redis 127.0.0.1:6379> get key6
"0"
redis 127.0.0.1:6379> incr key6
(integer) 1
redis 127.0.0.1:6379> set key7 1
OK
redis 127.0.0.1:6379> get key7
"1"
redis 127.0.0.1:6379> incrby key7 5
(integer) 6
redis 127.0.0.1:6379> get key7
"6"

decr

Subtract the value of key.

decrby

Similar to decr, subtract the specified value.

9. append

Appends value to the specified key string, returning the length of the new string value.


redis 127.0.0.1:6379> get key6
"1"
redis 127.0.0.1:6379> append key6 @163.com
(integer) 9
redis 127.0.0.1:6379> get key6
1@163.com

The above is the site to introduce you Redis02 Redis database (String type) comprehensive analysis, hope to help you, if you want to know more content, please pay attention to this site!


Related articles: