How can Redis be used?

如何使用 Redis?
There is more to Redis than just caching.

Redis 不仅仅是缓存。
Redis can be used in a variety of scenarios as shown in the diagram.

Redis 可用于多种场景,如图所示。

Session
We can use Redis to share user session data among different services.

会话
我们可以使用 Redis 在不同服务之间共享用户会话数据。

Cache
We can use Redis to cache objects or pages, especially for hotspot data.

缓存
我们可以使用 Redis 来缓存对象或页面,尤其是热点数据。

Distributed lock
We can use a Redis string to acquire locks among distributed services.

分布式锁
我们可以使用 Redis 字符串来获取分布式服务之间的锁。

Counter
We can count how many likes or how many reads for articles.
计数器
我们可以计算出文章的点赞数或阅读量。

Rate limiter
We can apply a rate limiter for certain user IPs.

速率限制器
我们可以对某些用户 IP 应用速率限制器。

Global ID generator
We can use Redis Int for global ID.

全局 ID 生成器
我们可以将 Redis Int 用于全局 ID。

Shopping cart
We can use Redis Hash to represent key-value pairs in a shopping cart.

购物车
我们可以使用 Redis Hash 来表示购物车中的键值对。

Calculate user retention
We can use Bitmap to represent the user login daily and calculate user retention.

计算用户留存率
我们可以用位图来表示用户每天的登录情况,并计算用户留存率。

Message queue
We can use List for a message queue.
消息队列
我们可以将 List 用于消息队列。

Ranking
We can use ZSet to sort the articles.

排名
我们可以使用 ZSet 对文章进行排序。
Over to you: Have you used Redis for other use cases? What are they?
交给你:你是否将 Redis 用于其他用例?它们是什么?


我在项目中用到是使用分布式锁,用来防止同一文件的多次上传。

另外一个是做 mq,在我的另外一个项目中,使用 Redis Go 的 SDK,类似于RPush 方法

比如在 redis 的 Java JDK 中redisTemplate.opsForValue().setIfAbsent() 方法

看了下Redis 的文档,非常简单明了。