I. Introduction of basic concepts and features of Redis

1.1 Introduction to basic concepts of Redis

 

1.Redis is remote, with client and server, we generally talk about the server;

2.Redis is memory based, so it is much faster than hard disk based MySQL, but very memory hungry

3.Redis is a non-relational database. It is essentially a database, but the MySQL relational database must define a data dictionary for storage, whereas Redis does not.

1.2 Comparison between Redis and Memcached

 

Redis data types all support push/ POP, add/remove, intersection union and difference sets, and richer operations, all of which are atomic.

As with Memcached, the data is cached in memory for efficiency. The difference is that Redis periodically writes the updated data to disk or the modified operation to the appended record file, and on this basis realizes master-slave synchronization.

1.3 Redis supports master/slave synchronization.

Data can be synchronized from the primary server to any number of secondary servers, which can be the primary server associated with other secondary servers. This allows Redis to perform single-layer tree replication. Data can be written intentionally or unintentionally. The fully implemented publish/subscribe mechanism makes it possible to subscribe to a channel and receive a complete record of message releases from the master server when the tree is synchronized anywhere from the database. Synchronization helps with scalability and data redundancy for read operations.

2. Application scenarios of Redis

2.1 the cache

 

Redis is often used as a data cache.

When some system interface is slow, we can put some data in Redis cache, the next data will not be very time-consuming SQL operations, directly from the cache data, one of the most common methods to improve system performance;

2.2 the queue

Push and POP operations ensure atomicity;

2.3 Data Storage

All additions, deletions, changes and checks are carried out in Redis. Redis has a persistent mechanism of hard disks to store data periodically to ensure data integrity and security.

  • View the Redis client: which redis-cli
  • Login: Redis – CLI (default login to local port 6379)
  • The info command is used to view redis information

Redis data type

In redis, all contents can be used as the name part of the key except \n and Spaces. Name length is not required.

 

 

3.1 String Operations performed as a String

 

Function: Provides string or integer operations.

Set Key Value (string/int/float) get KeyINCr String [value]// Self-increment decrby string [value]// Self-decrementCopy the code

String is the most basic type of Redis

A Redis string can contain any data. Include JPG images (binary) or serialized objects.

The maximum value of a single value is 1 gb.

If you use string only, redis can be thought of as memcache with persistence features.

 

3.2 List Types of linked lists

 

Functionality: Stores collections of sequences that can be used for queue, push, and POP operations

Lpush left in rPOP right in llen key to see how many elements there are in listCopy the code

The List type is first-in, first-out, suitable for queues; A List does not require its elements to be unique

(1) Introduction

Select * from user order by logintime desc LIMIT 10;

The above SQL statements can achieve user requirements, but when the data is much, all the data will be affected, and the load on the database is relatively high. If necessary, you need to set indexes for key fields (ID or LoginTime). Indexes also consume system resources.

If you do this with a list, you can keep only the last 10 items in the list and delete one item for each new item. You can get the data you need directly from the linked list each time. Great savings in all aspects of resource consumption

(2) Application

The list type is essentially a bidirectional list. Add and remove elements from the top or bottom of the list via push and pop operations.

This allows lists to be used as both stacks and queues.

Upper up and out: stack

Up and down: queue

 

 

  1. Lrange key 0-1 fetch all elements.
  2. Lrem key n/-n X Deletes the specified element n times.
  3. Linsert key after/before KEY1 Key2 Inserts key2 after/before key1.
  4. Rpoplpush key1; rpoplpush key2; rpoplpush key1;

 

Function: Unordered collection, each element is different.

 

3.3 the Set type

The set type stores multiple different elements in an unordered way, which can be quickly added, searched and deleted. We only count one of the same elements.

Redis’ set is an unordered collection of type string.

The set element can contain up to (2 ^ 32 -1) elements.

In addition to basic add and delete operations on set types,

Other useful operations include union, intersection, and difference of sets. Through these operations, it is easy to realize the friend recommendation function in SNS.

Note: Individual elements in each collection cannot be repeated.

 

This type of application: RECOMMENDED by QQ friends.

Mary Jack Xiaoming Wang5 Wang6 Linken friends circle: Yuehan Daxiong Luce Wang5 Wang6Copy the code

 

3.4 the hash type

 

Function: Store key-value structure, key must be string

Often used to store complex structures, Hash only requires different keys, such as name, age, and so on

Insert/modify: hset hash1 key1 12 Obtain: hget hash1 key1 Hash length: hlen key One-time obtain: hmget hash1 key1 key2Copy the code

 

 

3.5 Sort Set Sort Set type

 

What it does: Storage is an ordered collection of mixed scores, often used for leaderboards.

Sort set is often used for ranking such as class grades

Storage, like hash, is a mapping. It stores a mapping of fractions to elements. You can see it’s a leaderboard.

Value must be globally unique.

Score is a floating point.

Sort set operation that sorts elements in byte order if they have the same score

Added or Modified: zadd zset1 10.1 Val1 Number of views: zcard zset1 Rank: zrange zset1 0 2 withscores View a value rank: zrank zset1 val2Copy the code

Just like set, a sorted set is a collection of string elements, except that each element is associated with a weight. The elements in the collection can be obtained in order by weights.

This Sort set type is suitable for:

Select * from message order by backnum desc LIMIT 5;

(The above requirements can be achieved through simple SQL statements, but SQL statements consume mysql database resources.)

Example: Use sort Set to get the top 5 most popular posts

 

Each element in a sorted set is a combination of values and weights. (In the previous set types each element was just a value.)Copy the code

 

We just do a sort set with only 5 elements in it, and the 5 elements are the ones with the highest recovery

When each post is replied, it has the opportunity to enter the collection, but only the top 5 posts with the highest reply volume will exist in the collection, and those with low reply volume will be deleted.

The last

Read this friend can forward attention, the back will update more series of articles to share reading!