Redis in-depth adventure study notes-basics and applications (1)

Redis application scenarios

Caching and distributed locks, etc.

For example:

  1. Record the number of post likes, comments and clicks (hash)
  2. Record the user's post ID list (sorted), which is convenient for quickly displaying the user's post list (zset)
  3. Record the title, abstract, author, and cover information of the post for display on the list page (hash)
  4. Record the related article ID of the post and recommend related posts according to the content (list)
  5. If the post ID is incremented by an integer, you can use Redis to assign the post ID (counter)
  6. and many more

redis installation

There are docker method, Github source code compilation method, direct installation method

The docker installation method is recommended here, docker-compose builds redis service

Five basic data structures of redis

  1. string
  2. list
  3. hash (dictionary)
  4. set
  5. zset (ordered set)

Redis basic type operation API

The
internal representation of string is an array of characters. Redis strings are dynamic strings, which can be modified. Use pre-allocation of redundant memory to reduce frequent memory allocation.
When the string length is less than 1MB, the expansion is to double the existing space. If the length of the string exceeds 1MB, the expansion will only add 1MB of space at a time. Note that the maximum length of the string is 512MB.
At the
beginning of the list, it can be considered that the internal implementation is a two-way linked list
. The list structure of redis is often used as an asynchronous queue. Serialize the task structure that needs to be postponed into a string,
and stuff itinto the Redis list, and another thread polls the data from this list for processing.If you go deeper, you will find that the underlying storage of Redis is not a simple linkedlist. Instead, it is called a "quicklist" structure.
First, when there are fewer list elements, a continuous memory storage is used. This structure is ziplist, which is a compressed list. It stores all the elements next to each other, and allocates a contiguous memory. It will be changed to quicklist when the amount of data is relatively large. Because ordinary linked list requires too much additional pointer space, it will waste space and increase the fragmentation of memory. For example, an ordinary linked list stores only int type data, and two additional pointers prev and next are required in structure. So redis combines the linked list and the ziplist to form a quicklist, that is, multiple ziplists are stringed together using two-way pointers. The quicklist not only satisfies the fast insert and delete performance, but does not have too much space redundancy. The following advanced part will talk about the
hash
unordered dictionary, which is composed of "array + linked list". You can refer to the implementation of simple hash table.
Unlike the hash in the language,redis中的字典的key只能是字符串
when the last element of the hash structure is removed, the data The structure is automatically deleted and the memory is recycled
set
With self-deduplication function.
When the last element is removed from the set, the data structure is automatically deleted and the memory is recycled.
zset
zset is the most distinctive data structure provided by redis. On the one hand, it is a set, which guarantees the internal value. On the other hand, it can assign a score to each value, which represents the sorting weight of this value. Its internal implementation uses a data structure called a "jump list".
When the last element is removed from zset, the data structure is automatically deleted and the memory is recycled

General rules for container data structures

The four data structures list, set, hash, and zset are container data structures, and they share the following two general rules:

  1. create if not exists
  2. drop if no elements: there are no elements in the container, then the container will be deleted immediately to release the memory

Expiration

The redis tree has a data structure that can set the expiration time. When the time is up, Redis will automatically delete the corresponding object. It should be noted that the expiration time is based on the object. For example, the expiration of a hash structure is the expiration of the entire hash object, not the expiration of one of the child keys.

If a string has an expiration time set, and then you call the set method to modify it, its expiration time will disappear

Guess you like

Origin blog.csdn.net/csdniter/article/details/112593528