Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common basic objects in redis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the basic objects commonly used in redis, which are introduced in great detail and have certain reference value. Friends who are interested must finish reading them.

One: preface

There are several commonly used basic objects in redis, such as string, hash, list, set, zset and so on. Let's introduce their underlying implementation data structure and common application scenarios and characteristics.

Two: redisobject

The source code location starts at line 605 in the server.h file

Typedef struct redisObject {unsigned type:4; unsigned encoding:4; unsigned lru:LRU_BITS; int refcount; void * ptr;} robj

2.1 type

The actual object types in redis are divided into five types of 0-4 declarations. Located at line 466 in the file server.h

# define OBJ_STRING 0 / * String object. * / # define OBJ_LIST 1 / * List object. * / # define OBJ_SET 2 / * Set object. * / # define OBJ_ZSET 3 / * Sorted set object. * / # define OBJ_HASH 4 / * Hash object. , /

2.2 encoding

Redis eight encoding formats used for five objects string, list, hash, set, and zset, each of which corresponds to a data structure

# define OBJ_ENCODING_RAW 0 # define OBJ_ENCODING_INT 1 # define OBJ_ENCODING_HT 2 # define OBJ_ENCODING_ZIPLIST 5 # define OBJ_ENCODING_INTSET 6 # define OBJ_ENCODING_SKIPLIST 7#define OBJ_ENCODING_EMBSTR 8 # define OBJ_ENCODING_QUICKLIST 9

2.3 refcount

The memory in redis is reclaimed by a relatively simple reference counting method. Each object reference is refcount + 1. When this reference count is reduced to 0, the memory will be reclaimed.

Three: string

3.1 Common scenarios

Distributed lock: the implementation of distributed lock is based on the command setnx user information of string: in many cases, user information will be serialized and stored in redis, but you can consider hash here. If only part of the user data is used, after all, serialization and deserialization are also an overhead.

3.2 Encoding format

Int: int encoding is used when the string is full of digits. This is the true binary data store embstr: memory address contiguous, memory one request. The string length is less than 44raw: the bottom layer is implemented by sds. The difference from embstr is that the creation of sds and the creation of redisobject are implemented twice.

3.3 Common commands

# Storage set key value# mutually exclusive storage # existing key storing data again will not change cache setnx key value# expiration storage, unit second # set key expiration time, and automatically delete setex key seconds value# expiration storage upon expiration Unit millisecond psetex key milliseconds value# batch storage mset key value [key value...] # value get key# batch value mget key [key...] # add append key value# length strlen key# self-increment, only int-encoded string incr key# custom step size self-increasing incrby key increment# self-decreasing, only int-encoded string # this can be reduced to a negative number Second kill deduction inventory and so on, think about whether you can use this command decr key# custom step size self-subtractive decrby key increment.

Four: list

4.1 Common scenarios

Message queue: generally not used, after all, all kinds of MQ, Kafka are very mature. Moreover, the implementation of message queuing by redis does not guarantee the safe ranking calculation of data: this is only suitable for regular calculation and update, but not for real-time update. For example, Meituan calculates the list of likes of resident merchants every day: such as likes on Wechat (I don't know how to do it, guess)

4.2 coding format

Quicklist: quick list, previous versions used linkedlist and ziplist. The quicklist currently used is a combination of the two. For more information, please see Redis (1)-- talking about the data structure in Redis

4.3 configuration of related parameters

The configuration parameters are located on lines 1083 and 1099 in the redis.con file

List-max-ziplist-size: configure a single ziplist size list-compress-depth: configure the LZF compression algorithm start node

4.4 Common commands

# create list and press in node # press in node at chain header lpush key value# press in node at chain tail rpush key value# pop-up list head node lpop key# pop-up list tail node rpop key# delete node # count > 0 search the number of value# count deleted count from the left

< 0 从右开始搜索删除|count|数量的value# count = 0 删除list中所有valuelrem key count value# 范围保留# -1 表示列表最后一个元素# -2 表示倒数第二个,以此类推ltrim key start stop# 计算长度llen key# 索引查询节点# index < 0 从右开始搜索# index >

0 search lindex key index# range query # stop =-1 means all lrange key start stop# blocking pop-up # will wait for more than # key when there is no node in the queue, it will be checked sequentially until the non-empty list is found # timeout can set the waiting time, 0 means waiting for blpop key [key...] Timeoutbrpop key [key...] Timeout

Five: Hash

5.1 Common scenarios

Commodity object, user object. This scenario requires confirmatory treatment. If you need a full amount of product object and user object information every time, you might as well save string, but only for partial use, you can consider using information such as hash structure SKU. Hash is more appropriate in this scenario. A hash structure stores all the sku of a product

5.2 coding format

Ziplist: when using ziplist to store hash structure, a data will use two adjacent ziplistEntry to store field and valuehashtable: when the data storage exceeds the parameter limit, the underlying structure will be converted from ziplist to dict for storage.

5.3 related parameter configuration

Hash-max-ziplist-entries: default 512, that is, the ziplist node is 1024. When the number of nodes exceeds this limit, the underlying data structure is converted to dicthash-max-ziplist-value: the default is 64, and the underlying data structure is converted to dict when any value whose length exceeds this limit is inserted into the hash.

5.4 Common commands

# Storage hset key field value# does not allow to change storage # if the field value exists, this storage will not overwrite the original value hsetnx key field value# batch storage hmset key field value [field value.] # query hget key field# batch query hmget key field [field.] # full query hgetall key# full query field value hkeys key# full query value value hvals key# delete fieldhdel key field [field. ] # calculate the number of key pairs hlen key# field exist to judge the hexists key field# incremental iteration # cursor indicates the start of the iteration cursor # count indicates the number of data returned by the iteration # pattern indicates regular matching result limits hscan key cursor [Count count] [Match pattern]

Six: Set

6.1 Common scenarios

Recommendation: calculate the intersection through the sinter command. For example, when Meituan recommends nearby takeout, you can calculate the intersection with nearby merchants based on your takeout records and push security tips: WeChat group members are saved in a set, and user friends are also saved in set. When users join the group chat, they can remind non-friend users to pay attention to security.

6.2 coding format

Intset: collection of integers to store data in the set collection where all value are integers hashtable:field is used to store set set values

6.3 configuration of related parameters

Set-max-inset-entries: default 512, which means that when the number of elements exceeds the limit, it is converted to hashtable encoding.

6.4 Common commands

# Store sadd member [member...] # pop-up elements and return spop key count# query all elements smembers key [count] # calculate the number of elements scard key # Delete the specified element srem key member [member...] # determine the existence of the element sismember key member# calculate the difference between the given set and the subsequent set # return the result sdiff key [key.] # calculate the difference between the given set and the subsequent set Set # stores the result to destination and returns sdiffstore destination key [key...] # calculates the intersection of the given set and subsequent sets # returns the result sinter key [key...] # calculates the difference between the given set and subsequent sets # stores the result to destination and returns sinterstore destination key [key...] # calculates the difference between the given set and subsequent sets # returns the result sunion key [key...] # Calculate the difference between the given set and the subsequent set # store the result to destination and return sunionstore destination key [key.]

Seven: Zset

7.1 Common scenarios

Ranking: if Meituan wants to make a sales ranking, he can use the store's order to do score. The result of this query is an orderly weight queue: score as the priority, so that the weight of the data taken is the delayed task with the highest priority: score is used as the start-up execution time of the task, and the value can be judged to execute.

7.2 coding format

Ziplist: similar to hash, both use two adjacent nodes to store score and memberskiplist: jump table structure, you can view Redis (1)-- talking about the data structure in Redis

7.3 configuration of related parameters

Zset-max-ziplist-entries: the default value is 128, and a maximum of 128 ziplist storage elements are specified. Exceed conversion to skiplistzset-max-ziplist-value: default value 64, maximum stored data value 64 bytes, exceed conversion to skiplist

7.4 Common commands

# Storage # xx means that the inserted member is stored only when the inserted member does not exist in the zset. # nx means that the zadd key [nx | xx] score member [score member...] # is stored only when the inserted zadd key does not exist in the zset. # withscores query results return element scores zrange key start stop [withscores] # query specified element scores zscore key member# element quantity statistics zcard key# returns score at [min] The number of elements in the max] interval zcount key min max # increases the value of the element in the specified score range zincrby key incrment member# returns the element # withscores returns the score together with # limit offset count indicates skipping the offset quantity result and then returns the count quantity result zrangebyscore key min max [withscores] [limit offset count] # returns the element # withscores in the specified score range with the score when it is returned Return # limit offset count: skip offset quantity result and return count quantity result zrevrangebyscore key max min [withrescores] [limit offset count] # Delete the specified score range [min] Max] element zremrangebyscore key min max# removes the specified element zrem key member above is all the content of the article "what are the common basic objects in redis?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report