The data type

Strings

1.1 Type Introduction

String is the simplest Redis storage type. It stores values that can be strings, integers, or floating-point numbers. It operates on the entire string or parts of the string. Perform Increment or Decrement on integer or floating – point numbers

The Redis string is a sequence of bytes, similar to the Java ArrayList, which preallocates redundant space to reduce memory frequency. Capacity – is larger than len. If the string length is less than 1 MB, the existing space is doubled. If the string length is more than 1 MB, the existing space is expanded by 1 MB at a time. Note that the maximum length of the string is 512 MB

1.2 Application Scenarios

The string type is widely used to cache data and improve query performance. For example, store login user information, store product information in e-commerce, can do counters (want to know when to block access to an address more than a few times) and so on

1.3 Operation Instructions

Set Key Value: Adds a String data. Get Key: obtains a String data. Mset KEY1 Value1KEY2 Value2: I Adds multiple String data String data INCr Key: increments by step (+1) Incrby key step: increments by step (step) DECr key: decreases by step (-1) Derby Key step: decreases by step (step)Copy the code

Hashes

2.1 Type Introduction

A hash is the equivalent of a HashMap in Java, with an unordered dictionary inside. The implementation principle is the same as HashMap. A hash table has multiple nodes, each of which holds a key-value pair. Unlike a Java HashMap, rehash is not the same because a Java HashMap with a large dictionary is a time-consuming operation that requires a subtotal rehash. Redis uses a progressive Rehash strategy because it can’t block services for high performance. Progressive Rehash preserves both the old and new hash structures while rehashing, queries both hash structures at the same time, and then migrates the contents of the old hash to the new hash structure gradually in subsequent scheduled tasks and hash operation instructions. When the move is complete, a new hash structure is used instead.

When the hash removes the last element, the data structure is automatically deleted and the memory is reclaimed

2.2 Application Scenarios

Hash also works the same way as object storage, such as storing user information. Unlike strings, which require serialization of the object (such as JSON serialization), Hash stores each field of the user object separately, saving serialization and unsequence time. You can also keep records of your purchases, such as key for user ID,fied for item ID,vaue for number of items. It can also be used to store shopping cart data, such as key for general ID,feld for product ID, value for purchase quantity, etc.

2.3 Operation Instructions

Hset keyName fieldi valuel field2 value2 get key Get all the component values hgetall keyName subtracts hdel keyname Hlen keyName # Increment an attribute by step (the attribute must be a number) hincrby keyName Field step # increment an attribute by step (the attribute must be a number) hincrby keyName field stepCopy the code

List (lists)

3.1 Type Introduction

Lists in Redis are equivalent to LinkedList in Java. The implementation principle is a bidirectional LinkedList (its underlying is a fast list), which can support reverse lookup and traversal, making it easier to operate. Insert and delete mentions are very fast, o(1), but index location is slow, O (n)

3.2 Application Scenarios

There are so many application scenarios that you can use it to easily achieve a hot list; Work queue can be realized (lists push operation is used to store the task in lists, and then the worker thread takes the task out for execution with POP operation). You can implement up-to-date lists, such as the latest comments, etc.

3.3 Operation Instructions

Value1 value2 value3 # Left out LPOP key # Right out Rpush key valuel value2 value3 # Right out RPOP Key # Read from left to right Start and end are subscripts  lrange key start endCopy the code

Collection (sets),

4.1 Type Introduction

Sets are similar to Hash sets in Java. The internal implementation is a HashMap whose VUE is always nu. In fact, sets compute the Hash to quickly remove weights, which is why sets can be used to determine whether a member is in a Set

4.2 Application Scenarios

The set type of Redis is constructed by using hash table, so the complexity is (1). It supports adding, deleting, modifying and searching within sets, and supports intersection, union and difference set operations among multiple sets. These collection operations can be used to solve many problems between data sets in the process of program development. Such as calculating the site’s individual IP, user tags in user portraits, mutual friends and other functions

4.3 Operation Instructions

Valuel value2 sadd key value2 srem key value2 Srem key value2 Srem key value2 Sdiff keyl key2 # Reverse siffer, return intersection sinter key1 key2Copy the code

An ordered set

5.1 Type Introduction

Sorted sets are a combination of Redis similar to SortedSet and HashMap. On the one hand, it is a set, which ensures the uniqueness of the internal Vaue. On the other hand, it can assign a score to each value, representing the sorting weight of the vaue. Internally, hashMaps and SkipList are used to keep the data stored and ordered, A HashMap contains the mapping of members to Score, while a skip list stores all members. Sorting is based on the Score stored in a HashMap. The structure of a skip list can achieve relatively high search efficiency and is relatively simple to implement. After the last value of the sorted set is removed, the data structure is automatically deleted

5.2 Application Scenarios

It is mainly used in scenarios where queues are sorted according to a certain slash weight, such as game leaderboards, priority task lists, student scores, IP availability, etc.

5.3 Operation Instructions

Zrange key start end zrange key start end zrange key end Members of an ordered set whose score is between min and Max (including those equal to min or Max) are arranged in ascending order of score, with the smallest being the top zrangebyScore key start end Zcard key # Count the number of elements in a range zcount key start end # Get the current value rank zrank key value # Reverse get the rank zrevrank key valueCopy the code

Hop table query process

This article was automatically published by ArtiPub