Home > Database > Redis > What are the uses of redis

What are the uses of redis

藏色散人
Release: 2019-10-17 14:26:36
Original
3488 people have browsed it

What are the uses of redis

Introduction to Redis

Redis is a high-performance key-value database.

Redis has the following three features with other key-value caching products:

- Redis supports data persistence, and can save data in memory to the disk, and can be used again when restarting Load for use.

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

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

What are the uses of redis?

Five uses

1. Full page caching

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. Queuing

One of the less common, but very useful things 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 ultimate real world use of Redis is the pub/sub that I will present 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.

The above is the detailed content of What are the uses of redis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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