Home  >  Article  >  Database  >  Where is redis used?

Where is redis used?

步履不停
步履不停Original
2019-06-25 11:45:282462browse

Where is redis used?

1. High concurrency cache/shared session:

UserInfo getUserInfo (long id) {}

Get:

userRedisKey = "user:info:" id;

value = redis.get(userRedisKey);

if (value != null) {

userInfo = deserialize(value);

                                                                                                                                                    return userInfo;

redis.setex(userRedisKey, 3600, serialize(userInfo));

Using string to store serialized data is not three-dimensional and intuitive enough. It can be converted to hmset and stored as a hash structure, making access more intuitive.

2. Simple distributed lock

setnx can only be set successfully if it does not exist, and the rest can only wait. Single thread

3. Counter incr, because it is single-threaded, consumes less CPU than cas, etc., and has higher performance

long incrVideoCounter ( long id) {

          key = "video:playCount:" id;

4. Implement stack/queue

Stack: lpush lpop

Queue: lpush rpop

5. Flow control/rate limit

phoneNum = "12345678999"; key = "shortMsg:limit:" phoneNum;

isExists = redis.set( key, 1, "EX 60", "NX");

if (isExists != null || redis.incr(key) <= 5) {

// Pass

##} else {

// Do not pass

}

6.

Use lpush brpop to implement a blocking queue. The producer inserts elements from the left end of the list through lpush, and multiple consumers block and obtain the tail elements of the queue from the right end of the brpop

7. Every A user has his own articles. Now he wants to display the article list in pages.

hmset article:1 title xx context XXXX

lpush user:1:articles srticle:1 articles:3

articles = lrange user:1:articles 0 9

for article in {articles} hgetall {article}

8. Follow and like

Like: zincrby user:ranking:2016_03_15 mike 1

Cancel: zrem user:ranking:2016_03_15 mike

Get like The top 10 users: zrevrangebyrank user:ranking:2016_03_15 0 9

Display user information and scores: hgetall user:info:tom / zscore user:ranking:2016_03_15 mike / zrank user:ranking:2016_03_15 mike

9. Bitmaps calculate the relationships among big data sets

10. Ranking

mike uploaded a video and received 3 likes zadd user:ranking:2016_03_15 mike 3

Another person liked it zincrby user:ranking:2016_03_15 mike 1

11. Follow together

Add a follow tag to the user sadd user:1:tags tag1 tag2

Add a user to the tag sadd tag1:uses user:1

Common attention sinter user:1:tags user:2:tags sinter/sunion/sdiff

12. Publish and subscribe

Subscribe video:changes: publish video:changeds "video1,video2"

for video in video1,video2

update (video)

Each data type corresponds to a variety of underlying data structure implementations (object encoding), which can be switched through data size, length, scenarios, etc. to achieve higher efficiency

Persistent RDB (child process creation, binary file, fast recovery, not real-time enough)/AOF (appendonly. Text files, real-time writing operations first aop_buffer, and then write to the disk by configuring the write disk interval, and merge when reaching a certain size)

Batch hmget and other operations must be converted to hscan and other progressive traversal methods, otherwise it is easy to blockBuffering: client buffering (input/output), copy backlog buffer, aof buffer

Copy: full/incremental copy offset/copy backlog buffer (write command is sent to the slave server at the same time It also maintains a first-in-first-out queue, which means that the main service also saves the most recently propagated commands)/ID

sentinal: To achieve high availability, it is a special redis node. You can configure the cluster yourself and monitor the redis data cluster through heartbeat and other mechanisms. When a node fails and becomes unavailable, it can be discovered in time and automatically migrated

cluster: Distributed cluster, fault-tolerant leader selection, etc. Mapping physical nodes to 16383 slots to achieve dynamics

For more Redis-related technical articles, please visit the Redis Tutorial column to learn!

The above is the detailed content of Where is redis used?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more