Data type of redis

Keywords: Redis Programming

Article directory

Data type of redis

redis Data type: http://www.redis.cn/topics/data-types.html

1: String:

String is the most common and commonly used data type in all programming languages, and it is also one of the most basic data types in redis. Moreover, all key types in redis are strings.

1.1 add a key:
127.0.0.1:6379> set k1 v1
OK
1.2 get the content of a key:
127.0.0.1:6379> get k1
"v1"
1.3 view the type of a key
127.0.0.1:6379> type k1
string
1.4 set the automatic key expiration time
127.0.0.1:6379> set k2 v2 ex 3 (3 Delete after seconds key)
OK
127.0.0.1:6379> get k2
"v2"
127.0.0.1:6379> get k2
(nil)
1.5 delete a key
127.0.0.1:6379> del k1
(integer) 1
1.6 batch setting multiple key s:
127.0.0.1:6379> MSET key1 value1 key2 value2
OK
1.7 additional data
127.0.0.1:6379> append k1 v666
(integer) 6
127.0.0.1:6379> get k1
"v1v666"
1.8 numerical increment
127.0.0.1:6379> set n 10
OK
127.0.0.1:6379> incr n
(integer) 11
127.0.0.1:6379> get n
"11"
1.9 numerical decrement
127.0.0.1:6379> set m 10
OK
127.0.0.1:6379> decr m
(integer) 9
127.0.0.1:6379> get m
"9"
1.10 return string key length
127.0.0.1:6379> strlen k1
(integer) 6

Two, list

A list is a two-way read-write pipe, with the head on the left and the tail on the right. A list can contain at most 2 ^ 32-1 elements
4294967295 elements.

2.1 generate list and insert data
127.0.0.1:6379> lpush list1   d1 d2 d3 (Insert 3 data at a time)
(integer) 3

The order of storage is d3 d2 d1

2.2 additional data
  • lpush adds elements from left to right (string elements are added in the header of the list corresponding to the key)

    127.0.0.1:6379> lpush l1 tom 
    (integer) 1
    127.0.0.1:6379> lpush l1 rose
    (integer) 2
    
  • rpush adds elements from right to left. Add string elements at the end of the list corresponding to key

    127.0.0.1:6379> rpush l1  jack
    (integer) 3
    
2.3 get list length:
127.0.0.1:6379> llen l1
(integer) 3

127.0.0.1:6379> type l1
list
2.4 remove list data
  • LPOP list1 ා first

    127.0.0.1:6379> lpop list1
    "d3"
    
  • RPOP list1 last

    127.0.0.1:6379> rpop list1
    "d1"
    

3, Unordered set

Set is an unordered set of type String. Collection members are unique, which means that duplicate data cannot appear in the collection

3.1 generate set key
127.0.0.1:6379> sadd s1 v1
(integer) 1
127.0.0.1:6379> sadd s2  v2 v4
(integer) 2
127.0.0.1:6379> type s1
set
3.2 additional value
127.0.0.1:6379> sadd s1 v2 v3 v4
(integer) 3

127.0.0.1:6379> sadd s1 v1
(integer) 0    (No success here)
3.3 viewing all data in a collection
127.0.0.1:6379> smembers s1
1) "v2"
2) "v3"
3) "v4"
4) "v1"

127.0.0.1:6379> smembers s2
1) "v2"
2) "v4"
127.0.0.1:6379> 
3.4 obtaining difference sets of sets
127.0.0.1:6379> sdiff s1 s2
1) "v3"
2) "v1"
3.5 get intersection of sets
127.0.0.1:6379> sinter s1 s2
1) "v2"
2) "v4"
3.6 get union of sets
127.0.0.1:6379> sunion s1 s2
1) "v3"
2) "v4"
3) "v1"
4) "v2"

4, Sorted set:

Redis ordered set is also a set of string type elements as well as a set, and duplicate members are not allowed. The difference is that each element will be associated with a double (double precision floating-point) type score. Redis sorts the members of the set from small to large through the score. The members of the ordered set are unique, but the score can be repeated. The set is through Hash table, so the complexity of adding, deleting and searching is O(1). The maximum number of members in the collection is 2 ^ 32 - 1 (4294967295, each collection can store more than 4 billion members).

4.1 generate an ordered set:
127.0.0.1:6379> zadd zs1 1 v1
(integer) 1
127.0.0.1:6379> zadd zs1 2 v2
(integer) 1
127.0.0.1:6379> zadd zs1 2 v3
(integer) 1
127.0.0.1:6379> zadd zs1 3 v4
(integer) 1

127.0.0.1:6379> type zs1
zset
4.2 batch add multiple values:
127.0.0.1:6379> zadd zs2 1 v1 2 v2 3 v3 5 v5
(integer) 4
4.3 get the length of the set
127.0.0.1:6379> zcard zs1
(integer) 4
4.4 return value based on Index:
127.0.0.1:6379 > zrange ZS1 1 15 (5 does not exist, so only to 3)
1) "v2"
2) "v3"
3) "v4"
4.5 return the index of a value
127.0.0.1:6379> zrank zs2 v1
(integer) 0
127.0.0.1:6379> zrank zs2 v2
(integer) 1
127.0.0.1:6379> zrank zs2 v3
(integer) 2
127.0.0.1:6379> zrank zs2 v4
(nil)
127.0.0.1:6379> zrank zs2 v5
(integer) 3

5, Hash (hash)

Hash is a mapping table of field and value of string type. Hash is especially suitable for storing objects. Each hash in Redis can store 232 - 1 key value pairs (more than 4 billion).

5.1 generate hash key
127.0.0.1:6379> hset hs1 name zhangtao age 17
(integer) 2
5.2 get hash key field value
127.0.0.1:6379> hget hs1 name
"zhangtao"
127.0.0.1:6379> hget hs1 age
"17"
5.3 get all fields in the hash table
127.0.0.1:6379> hkeys hs1
1) "name"
2) "age"
5.4 delete a hash key field:
127.0.0.1:6379> hdel hs1 age
(integer) 1
127.0.0.1:6379> hkeys hs1
1) "name"
127.0.0.1:6379> 
Published 26 original articles, won praise 4, visited 396
Private letter follow

Posted by russthebarber on Sat, 15 Feb 2020 22:22:10 -0800