redis02: Redis five commonly used data structures and command line operations

For data in Redis, it is generally a key-value pair, and different data types refer to the type of value in the key-value pair .

About key

command effect
DEL key This command is used to delete the key when the key exists
EXISTS key Check if the given key exists
MOVE key db Move the key of the current database to the given database db
TYPE key Returns the type of the value stored in the key
SET key value Set up a kv key-value pair

1.String:Redis中最基本的类型,它是key对应的一个单一值。二进制安全,不必担心由于编码等问题导致二进制数据变化。所以redis的string可以包含任何数据,比如jpg图片或者序列化的对象。Redis中一个字符串值的最大容量是512M。

command effect
GET key Get the value according to the key
STRLEN key Returns the length of the string value stored in key
INCR key Increment the numeric value stored in the key by 1
INCRBY key increment Add the value stored in the key to the given increment (increment)
MSET KEY VALUE [KEY VALUE …] Set up a group of multiple key-value pairs at once
MGET KEY [KEY …] Specify multiple KEYs at once and return their corresponding values. The return value of KEYs without values ​​is (nil)

2.List:Redis 列表是简单的字符串列表,按照插入顺序排序。可以添加一个元素到列表的头部(左边)或者尾部(右边)。说明它的底层是基于链表实现的,所以它操作时头尾效率高,中间效率低

command effect
LPUSH key value [value …] Add data from the left
RPUSH key value [value …] Add data from the right
LRANGE key start stop Print the list collection according to the index (0 -1 range is all data)
FILL key the length of the list collection
LPOP key Move out and get the first element of the list
RPOP key Remove the last element of the list, and the return value is the removed element.
LINDEX key index Get a value from the collection based on the index
LSET key index value Only keep the data in the specified interval, the data on both sides are deleted
LTRIM key start stop Only keep the data in the specified interval, the data on both sides are deleted

3.Set: Redis 的 Set 是 String 类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。Redis 中集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)

command effect
SADD key member [member …] Add one or more members to the collection (not repeatable)
SCARD key Get the number of members in the collection
SMEMBERS key View all data in the set collection
SREM key member1 [member2] Remove one or more members from the collection
SUNION key1 [key2] Returns the union of all given sets

4.Hash:Redis hash 是一个 string 类型的 field(字段) 和 value(值) 的映射表,hash 特别适合用于存储对象,hash数据类型可以理解为java中的Map<String,String>

project Value
HSET key field value adding data
HGETALL key Query all data of a key
HGET key field Query the data of a key corresponding to the field
HKEYS key Get all fields in the hash table
HLEN key Get the number of fields in the hash table
WHALE key Get all the values ​​in the hash table
HEXISTS key field Determine whether the specified field exists in a key
HDEL key field [field …] Delete a field specified by key
HINCRBY key field increment Add the increment value to the value corresponding to a certain field of a key
HSET key field value Only when the field does not exist, set the value of the hash table field
  1. Zset: Redis zset 和 set 一样也是string类型元素的集合,且不允许重复的成员。不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。zset的成员是唯一的,但分数(score)却可以重复。
project Value
ZADD key score1 member1 [score2 member2] Add one or more members to an ordered set, or update the scores of existing members
ZRANGE key start stop [WITHSCORES] Return data within the specified range
ZCOUNT key min max Count the number of members with a specified interval score in an ordered set
ZRANK key member Returns the index of the specified member in the ordered set
ZCARD key Query the number of data corresponding to the key
ZREM key member [member …] Delete element
ZINCRBY key increment member Add a score to an element
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] Return data within the specified interval of the score

Guess you like

Origin blog.csdn.net/weixin_44080445/article/details/114108650