Home > Database > Redis > body text

Do you know the five major uses of redis?

王林
Release: 2021-01-12 09:32:14
forward
9161 people have browsed it

Do you know the five major uses of redis?

First of all, let’s briefly introduce redis.

(Learning video sharing: redis video tutorial)

Redis is a high-performance key-value database. Redis and other key-value cache products have the following three characteristics:

1. Redis supports data persistence. It can save the data in the memory to the disk and load it again for use when restarting.

2. Redis not only supports simple key-value type data, but also provides storage of data structures such as list, set, zset, and hash.

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

Five uses

1. Full page cache

Full page cache. If you're using server-side rendered content, you don't need to re-render each page for each individual request. Using a cache like Redis, you can cache frequently requested content, thereby greatly reducing latency for the most requested pages, and most frameworks have hooks for Redis caching pages.

// 设置全页面缓存1分钟
SET key "<html>...</html>" EX 60
// 获取全页面
GET key
Copy after login

2. Sequential arrangement

Because Redis's Set data structure in memory can be incremented and decremented very quickly and efficiently, the performance is much higher than that of SQL queries. Compare this combined with Redis' sorted sets meaning you can grab the highest rated item in a list in milliseconds, and it's very easy to implement.

// 向排序集合中添加值
ZADD sortedSet 1 "one"
// 从排序集合中获取所有值
ZRANGE sortedSet 0 -1
// 从排序集合中获取所有值与权重
ZRANGE sortedSet 0 -1 WITHSCORES
Copy after login

3. Session Storage

The most common use of Redis I have seen is session storage. Unlike other session stores such as Memcache, Redis can persist data so that if the cache is stopped, on restart, all data is still there. Even if it's not a strictly ongoing task, this feature can still save your users a lot of trouble. No one likes to see their conversations randomly deleted for no reason.

// 设置一分钟过期的session
SET randomHash "{userId}" EX 60
// 获取用户ID
GET randomHash
Copy after login

4. Queue

A less common, but very useful thing you can do with Redis is queuing. Whether it's an email queue or data used by other applications, you can create an efficient queue in Redis. Any developer who is familiar with the stack and can push and pop projects can use this feature easily and naturally.

// 添加消息  
HSET messages <id> <message>
ZADD due <due_timestamp> <id>
// 接收消息
ZRANGEBYSCORE due -inf <current_timestamp> LIMIT 0 1  
HGET messages <message_id>
// 删除消息
ZREM due <message_id>
HDEL messages <message_id>
Copy after login

5. pub/sub

The final use of Redis in the real world is the pub/sub that I will propose in this article. This is one of the most powerful features built into Redis; the possibilities are endless. You can create a real-time chat system, trigger notifications for friend requests on social networks, and more. This feature is one of the most underrated features provided by Redis, but is extremely powerful and simple to use.

// 向通道中推送消息
PUBLISH channel message
// 从通道中获取消息
SUBSCRIBE channel
Copy after login

Conclusion:

I hope you enjoyed these real-world uses of Redis. While this article only scratches the surface of what Redis can do for you, I hope you'll gain some inspiration for how to get the most out of Redis.

Related recommendations: redis database tutorial

The above is the detailed content of Do you know the five major uses of redis?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template