Data Structures Supported by redis: strings, lists, hashes, set, sorted set. Next we will do the experiment one by one.
Strings operations include get/set/setnx, append, strlen, getrange/setrange, incr/decr/incrby/decrby, mget/mset.
Get/set is to insert data (key-value corresponds ):
- Redis 127.0.0.1: 6379 [1]>Set test chen
- OK
- Redis 127.0.0.1: 6379 [1]>Set test2 zhou
- OK
- Redis 127.0.0.1: 6379 [1]>Get test
- "Chen"
- Redis 127.0.0.1: 6379 [1]>Get test2
- "Zhou"
Setnx is used to insert data to check whether the same key value exists. If yes, 0 is returned if no value exists. If no value exists, 1 is returned:
- Redis 127.0.0.1: 6379 [1]>Setnx test 11111
- (Integer) 0
- Redis 127.0.0.1: 6379 [1]>Get test
- "Chen"
Append is even simpler, that is, append characters used at ordinary times:
- Redis 127.0.0.1: 6379 [1]>Get test
- "Chen"
- Redis 127.0.0.1: 6379 [1]>Append test @ gmail.com
- (Integer) 14
- Redis 127.0.0.1: 6379 [1]>Get test
- Chen@gmail.com"
Getrange/setrange is a simple string operation that retrieves or sets the character at the relative position. There was a problem during the experiment here, mainly because we were not familiar with the setrange operation. Let's take a look at the following operation and we will understand that setrange is used to replace the number of corresponding positions, the location of 5 is @, and QQ.com is 6 characters to replace mysina. com10 characters can only Replace the first six.
- Redis 127.0.0.1: 6379 [1]>Set test3 chen@gmail.com
- OK
- Redis 127.0.0.1: 6379 [1]>Get test3
- Chen@gmail.com"
- Redis 127.0.0.1: 6379 [1]>Setrange test3 5 mysina.com
- (Integer) 15
- Redis 127.0.0.1: 6379 [1]>Get test3
- Chen@mysina.com"
- Redis 127.0.0.1: 6379 [1]>Setrange test3 5 QQ.com
- (Integer) 15
- Redis 127.0.0.1: 6379 [1]>Get test3
- Chen@QQ.com.com"
Incr/decr/incrby/decrby means that the incrby/decrby after auto-increment and auto-subtraction are added with the step size. If there is no key value, it can also be operated:
- Redis 127.0.0.1: 6379>Set age 20
- OK
- Redis 127.0.0.1: 6379>Incr age
- (Integer) 21
- Redis 127.0.0.1: 6379>Get age
- "21"
- Redis 127.0.0.1: 6379>Incrby age 5
- (Integer) 26
- Redis 127.0.0.1: 6379>Incrby ss 3
- (Integer) 3
- Redis 127.0.0.1: 6379>Incr tt 2
- (Error) ERR wrong number of arguments for 'inc' command
- Redis 127.0.0.1: 6379>Get tt
- (Nil)
Mget/mset indicates batch insertion and removal.
The basic operations of a hash table are the same as those of a string, such as hget/hset/hsetnx, happend, hstrlen, hgetrange/hsetrange, hincr/hdecr/hincrby/hdecrby, and hmet/hmset, just add H to the front. It is especially suitable for storing object data. The basic operation is the same as above. Here the key is used as the hash name. During the operation, you need to provide the hash field you want to insert:
- Redis 127.0.0.1: 6379 [1]>Hset myhash ID 001
- (Integer) 1
- Redis 127.0.0.1: 6379 [1]>Hset myhash name chen
- (Integer) 1
- Redis 127.0.0.1: 6379 [1]>Hset myhash address ncut
- (Integer) 1
- Redis 127.0.0.1: 6379 [1]>Hset myhash telephone 12332123
- (Integer) 1
- Redis 127.0.0.1: 6379 [1]>Hget myhash id
- (Nil)
- Redis 127.0.0.1: 6379 [1]>Hget myhash ID
- "001"
- Redis 127.0.0.1: 6379 [1]>Hget myhash name
- "Chen"
- Redis 127.0.0.1: 6379 [1]>Hget myhash0000 name
- (Nil)
Other operations are the same as above:
- Redis 127.0.0.1: 6379 [1]>Hkeys myhash
- 1) "ID"
- 2) "name"
- 3) "address"
- 4) "telephone"
- Redis 127.0.0.1: 6379 [1]>Hvals myhash
- 1) "001"
- 2) "chen"
- 3) "ncut"
- 4) 12332123"
- Redis 127.0.0.1: 6379 [1]>Hgetall myhash
- 1) "ID"
- 2) "001"
- 3) "name"
- 4) "chen"
- 5) "address"
- 6) "ncut"
- 7) "telephone"
- 8) "12332123"