In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to define redis Object structure". Xiaobian shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "how to define redis Object structure" can help you solve the problem.
A Brief Introduction to the Two-tier Data Structure of Redis
One of the reasons redis has high performance is that each data structure is specifically designed and supported by one or more data structures, relying on these flexible data structures to improve read and write performance. If you want to understand the data structure of redis, you can discuss it from two different levels:
The first level, from the user's point of view, is also the call interface that Redis exposes to the outside world, such as string, list, hash, set, sorted set.
The second level, from the perspective of internal implementation, belongs to the lower level implementation, such as: dict, sds, ziplist, quicklist, skiplist, intset.
Internal implementation of Redis data structure
From a Redis user's perspective, a Redis node contains multiple databases (16 by default in non-cluster mode, only one in cluster mode), and a database maintains a mapping relationship from key space to object space. The key of this mapping relationship is of type string, while the value can be of multiple data types, such as string, list, hash, set, sorted set, etc. We can see that the type of key is fixed to string, while the possible types of value are multiple.
From the perspective of Redis internal implementation, this mapping relationship within the database is maintained by a dict. The key of dict is expressed in a fixed data structure, which is the dynamic string sds. In order to store different types of values in the same dict, a common data structure is needed. This common data structure is robj, and the full name is redisObject.
For example:
If value is a list, then its internal storage structure is a quicklist.
If value is a string, its internal storage structure is typically an sds. But if the value of string is a number, Redis internally converts it to long for storage, reducing memory usage.
So a robj can represent either an sds, a quicklist, or even a long.
redisObject structure
The definition of redisObject is as follows:
typedef struct redisObject { unsigned type:4; unsigned encoding:4; unsigned lru:LRU_BITS; /* lru time (relative to server.lruclock) */ int refcount; void *ptr;} robj;
A robj contains the following five fields:
type: Data type of object. 4 bits. There are 5 possible values: OBJ_STRING, OBJ_LIST, OBJ_SET, OBJ_ZSET,
OBJ_HASH, corresponding to the five data structures exposed by Redis
encoding: The internal representation of the object (also known as encoding), which takes up 4 bits and has 10 possible values.
lru: Used for LRU replacement algorithm, accounting for 24 bits.
refcount: Reference count. It allows robj objects to be shared under certain circumstances.
ptr: Data pointer. Point to real data. For example, a robj for string whose ptr might point to an sds structure, or a robj for list whose ptr might point to a quicklist.
The encoding field in particular needs to be looked at carefully here. For the same type, it may also correspond to different encodings, which means that the same data type may have different internal representations. Different internal representations vary in memory footprint and lookup performance.
When type = OBJ_STRING, it means that the robj stores a string, and encoding can be one of the following three types:
OBJ_ENCODING_RAW: string is represented natively, using sds.
OBJ_ENCODING_INT: string is a numeric representation, which is actually a long.
OBJ_ENCODING_EMBSTR: string is represented by a special embedded sds.
When type = OBJ_HASH, it means that the robj stores a hash. In this case, encoding can be one of the following two types:
OBJ_ENCODING_HT: hash is represented by a dict.
OBJ_ENCODING_ZIPLIST: hash is represented by a ziplist.
The ten values of encoding are as follows:
OBJ_ENCODING_RAW: The most native representation. Only string types use this encoding value (denoted as sds).
OBJ_ENCODING_INT: Expressed as a number. It is actually expressed as long.
OBJ_ENCODING_HT: Denoted as dict.
OBJ_ENCODING_ZIPMAP: is an old notation that is no longer used. Only available in versions less than Redis 2.6.
OBJ_ENCODING_LINKEDLIST: is also an old notation that is no longer used.
OBJ_ENCODING_ZIPLIST: Represented as ziplist.
OBJ_ENCODING_INTSET: Represented as intset. Used for set data structures.
OBJ_ENCODING_SKIPLIST: Expressed as skiplist. Used for sorted set data structures.
OBJ_ENCODING_EMBSTR: Represents a special embedded sds.
OBJ_ENCODING_QUICKLIST: expressed as quicklist. Used for list data structures.
The Role of RedisObject
The redisObject functions as follows:
redisObjec is a bridge between two layers of data structures.
Provides a unified representation for multiple data types.
Allow different internal representations of the same type of data to maximize memory savings in some cases.
Support object sharing and reference counting. When objects are shared, only one copy of memory is used, further saving memory.
The content of "how to define the Object structure of redis" is introduced here. Thank you for reading it. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.