02- other data types

A, hash type

  hash for storing objects, the structure of the object attribute, the value (value of type String)

Add, modify

1, a single attribute:

Key Field value HSET
 # set key user attribute name is itheima 
HSET user name itheima

 

 Redis is configured to save database snapshot, but it is currently not persisted to disk. Commands to modify the data set can not be used.

the reason:

  • Redis snapshots can not be forced to shut down leading to persistence. solution:

  • Run config set stop-writes-on- bgsave-error no  command to close the configuration items stop-writes-on-bgsave- error to solve the problem.

2, a plurality of attributes

Key field1 VALUE1 Field2 value2 hmset
 # set key attribute name is u2 itcast, age property is. 11 
hmset u2 name itcast age. 11

Obtain

1, get all of the attributes specified key

Key hkeys
 # get all the key attributes of u2 
hkeys u2

2, to obtain the value of a property

Key Field, hget
 # get key attribute name u2 value 
hget u2 name

3, obtaining a plurality of attribute values

Key field1 Field2 hmget
 # acquires the attribute name key u2, age value 
hmget u2 name age

4, get all the value of the property

Key hvals
 # get the value of u2 all key attributes 
hvals u2

delete

Delete the entire key and hash value, del command, delete the attribute corresponding to the attribute values ​​are deleted with

Key field1 Field2 HDEL
 # delete key attribute age u2 of 
hdel u2 age

Two, list the type of

Element type to a list of string, sorted in order of insertion

increase

1, insert data on the left

Key VALUE1 value2 LPUSH
 # from the keyboard to the left of a1 added to the list of data A, B, C 
LPUSH a1 ABC

2, insert data on the right

Key VALUE1 value2 RPUSH
 # right list from the key data is added to a1 0,1 
RPUSH a1 0. 1

3, inserting a new element before or after the specified element

linsert key before or after an existing element of the new element
 # in the key in the list element b a1 previously added. 3 
linsert before a1. 3 b

Obtain

Returns the list of elements within the specified range ⾥

  • start, stop elements for the index index
  • Index from the left side, to the first frame element 0
  • Indices may be negative, indicating the start count from the end, such as the last frame -1 represents elements
STOP Key Start Lrange
 # Get the key is 'a1' a list of all the elements 
lrange a1 0 -1

Setting element values ​​specified index

  • Index from the left side, to the first frame element 0
  • Indices may be negative, showing the tail counted as -1 for last frame elements

Key index value LSET
 # modifier keys to 'a1' list index value of the element 1 'Z' 
LSET Z 1 A1

delete

Delete the specified element

  • The value of the element count value of the secondary list that appears before removing the
  • count> 0: to remove the tail from the head
  • count <0: is removed from the head to the tail
  • count = 0: remove all
Key COUNT value lrem
 # to the list of 'a2' in filling with the elements 'A', 'B', 'A', 'B', 'A', 'B' 
# LPUSH A2 ABABAB 

# from 'a2' list on the right start remove two 'B' 
lrem A2 -2 B 

# view a list of 'a2' all the elements 
lrange a2 0 -1

Three, set type

  • ⽆ ordered set
  • Elements of type string
  • ⼀ element has a unique, non-repeat
  • Definitions: For the set operation is not modified

increase

1, add elements

Key the member1 member2 is Sadd 
# key added to the 'a3' of the elements in a set 'zhangsan', 'Lisi', 'wangwu'
Sadd A3 zhangsan Lisi wangwu

Obtain

1, returns all the elements

Key smembers
 # Get key 'a3' of the set of all the elements 
smembers a3

delete

1, delete the specified element

Key value Srem
 # delete key set of 'a3' of the elements 'wangwu' 
Srem a3 wangwu

Four, zset type

  • sorted set, ordered set
  • Elements of type string
  • ⼀ element has a unique, non-repeat
  • Are associated with each element of type double ⼀ Score, it indicates a weight by weight of the elements from below approximately sorting ⼩
  • Description: no modification operations

increase

1, added

Key score1 the member1 member2 is score2 Zadd 
 # is added to the set of keys 'a4' element of 'lisi', 'wangwu', 'zhaoliu', 'zhangsan', weights are 4,5,6,3 
Zadd Lisi A4. 4. 5. 6 wangwu zhaoliu 3 zhangsan

Obtain

  • Returns the specified range of elements
  • start, stop elements for the index index
  • Index from the left side, to the first frame element 0
  • Indices may be negative, indicating the start count from the end, such as the last frame -1 represents elements

STOP Key Start Z Range The
 # Get key 'a4' collection of all elements 
zrange a4 0 -1
# Returns the score value between members min and max 
zrangebyscore min max Key 

# members Get key 'a4' permissions set value between 5 and 6 
zrangebyscore a4 5 6

Returns the value of the member member of score

Member Key zscore
 # Get key set 'a4' element of 'zhangsan' weights 
zscore a4 zhangsan

delete

1, delete the specified element

Key member1 member2 zrem 
 # delete collections 'a4' elements 'zhangsan' 
zrem A4 zhangsan

2, remove the weights of the elements specified return

Key min max zremrangebyscore
 # delete a collection 'a4' permission elements between 5,6 
zremrangebyscore a4 5 6

 

Guess you like

Origin www.cnblogs.com/lishuntao/p/11703354.html