Redis study notes (1) Getting to know Redis

Redis is an open source Key-Value database (NoSQL) that is written in C language, supports network interaction, and can be memory-based and persistent. The difference between Redis and Memcached:

1 Redis not only supports simple k/v types It also provides storage of data structures such as list, set, zset, and hash.

2 Redis supports data backup, that is, data backup in master-slave mode.

3 Redis supports data persistence, which can keep the data in memory in the disk, and can be loaded again for use when restarting.

Let's take a look at the five data types it supports:

strings, lists of strings, sets of strings, sorted sets, and hashes

1). String

is commonly used Commands: set, get, decr, incr, mget, etc.

Application scenario: String is the most commonly used data type. Ordinary key/value storage can be classified into this category. When incr, decr and other operations are encountered, it will be converted into a numerical type for calculation. At this time, the encoding field of redisObject is int.

2) Hash
common commands: hget/hset/hgetall, etc.

Application scenario: We want to store a user information object data, including user ID, user name, age and birthday, through the user ID we hope to get the user's name or age or birthday ;

Implementation: The Hash of Redis is actually a HashMap that stores the value internally, and provides an interface to directly access the members of this Map. As shown in the figure below, Key is the user ID, and value is a Map. The key of this Map is the attribute name of the member, and the value is the attribute value. In this way, the data can be modified and accessed directly through the key of its internal map (the key of the internal map is called field in Redis), that is, the corresponding attribute data can be manipulated through key (user ID) + field (attribute label).




3) List
common commands: lpush/rpush/lpop/rpop/lrange, etc.;

application scenarios: Redis list has many application scenarios, and it is also one of the most important data structures of Redis, such as twitter watch list, fan list, etc. can be used Redis list structure to achieve;

implementation method: Redis list is implemented as a doubly linked list, that is, it can support reverse search and traversal, which is more convenient to operate, but it brings some additional memory overhead. Many internal implementations of Redis, including sending This data structure is also used for buffer queues, etc.

4) Set
common commands: sadd/spop/smembers/sunion, etc.;

application scenario: Redis set provides external functions similar to list, which is a list function. The special feature is that set can automatically arrange weights. When you need to store a When you have list data and don't want duplicate data, set is a good choice, and set provides an important interface for judging whether a member is in a set collection, which is also not provided by list;

implementation method: set The internal implementation is a HashMap whose value is always null. In fact, it is used to quickly arrange the weights by calculating the hash. This is also the reason why set can judge whether a member is in the set.

5) Sorted Set
Common commands: zadd/zrange/zrem/zcard, etc.;

Application scenario: The usage scenario of Redis sorted set is similar to that of set, the difference is that set is not automatically ordered, and sorted set can provide an additional priority (score) parameter through the user To sort the members, and it is insertion-ordered, that is, automatic sorting. When you need an ordered and non-repeating set list, you can choose the sorted set data structure. For example, the public timeline of twitter can be stored with the publication time as the score, so that it is automatically sorted by time when it is obtained.

Implementation method: Redis sorted set internally uses HashMap and SkipList to ensure the storage and ordering of data. HashMap stores the mapping from members to scores, and the skip list stores all members. The sorting is based on For the score stored in HashMap, the structure of the jump table can be used to obtain relatively high search efficiency, and the implementation is relatively simple.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326880282&siteId=291194637