search
HomeDatabaseRedisWhere can Redis be used? 16 common usage scenarios shared
Where can Redis be used? 16 common usage scenarios sharedSep 18, 2021 pm 08:01 PM
redisscenes to be used

Where can Redis be applied? This article will share with you 16 common usage scenarios of Redis in one go. I hope it will be helpful to everyone!

Where can Redis be used? 16 common usage scenarios shared

[Related recommendations: Redis video tutorial]

1. Cache

String type

For example: hot data cache (such as reports, celebrity cheating), object cache, full page cache, access data that can improve hot data.

Where can Redis be used? 16 common usage scenarios shared

2. Distributed data sharing

String type, because Redis is a distributed independent service and can be shared between multiple applications

For example: Distributed Session

<dependency> 
 <groupId>org.springframework.session</groupId> 
 <artifactId>spring-session-data-redis</artifactId> 
</dependency>

3, Distributed lock

String type setnx method can only be added successfully if it does not exist, and returns true

public static boolean getLock(String key) {
    Long flag = jedis.setnx(key, "1");
    if (flag == 1) {
        jedis.expire(key, 10);
    }
    return flag == 1;
}

public static void releaseLock(String key) {
    jedis.del(key);
}

4, Global ID

int type, incrby, using atomicity

incrby userid 1000

In the scenario of sub-database and sub-table, get a section at one time

5, Counter

int type, incr method

For example: the number of articles read, the number of Weibo likes, allow a certain delay, first write to Redis and then synchronize to the database regularly

6. Current limit

int type, incr method

uses the visitor's IP and other information as the key. Each visit increases the count. If the number exceeds the number, false is returned

7. Bit statistics

Bitcount of String type (bitmap data structure introduction in 1.6.6)

characters are stored in 8-bit binary

set k1 a
setbit k1 6 1
setbit k1 7 0
get k1 
/* 6 7 代表的a的二进制位的修改
a 对应的ASCII码是97,转换为二进制数据是01100001
b 对应的ASCII码是98,转换为二进制数据是01100010

因为bit非常节省空间(1 MB=8388608 bit),可以用来做大数据量的统计。
*/

For example: online user statistics , retain user statistics

setbit onlineusers 01 
setbit onlineusers 11 
setbit onlineusers 20

Support bitwise AND, bitwise OR, etc. operations

BITOPANDdestkeykey[key...] ,对一个或多个 key 求逻辑并,并将结果保存到 destkey 。       
BITOPORdestkeykey[key...] ,对一个或多个 key 求逻辑或,并将结果保存到 destkey 。 
BITOPXORdestkeykey[key...] ,对一个或多个 key 求逻辑异或,并将结果保存到 destkey 。 
BITOPNOTdestkeykey ,对给定 key 求逻辑非,并将结果保存到 destkey 。

Calculate users who have been online for 7 days

BITOP "AND" "7_days_both_online_users" "day_1_online_users" "day_2_online_users" ...  "day_7_online_users"

8. Shopping cart

String or hash. All hashes that can be done with String can be done

Where can Redis be used? 16 common usage scenarios shared

  • key: user id; field: product id; value: product quantity.
  • 1: hincr. -1:hdecr. Delete:hdel. Select all: hgetall. Number of items: hlen.

9. User message timeline timeline

list, a doubly linked list, can be used directly as timeline. Insertion order

10. Message queue

List provides two blocking pop-up operations: blpop/brpop, and the timeout can be set

  • blpop: blpop key1 timeout removes and gets the first element of the list. If there is no element in the list, the list will be blocked until the wait times out or a pop-up element is found.
  • brpop: brpop key1 timeout removes and gets the last element of the list. If there are no elements in the list, the list will be blocked until the wait times out or a pop-up element is found.

The above operation. In fact, it is Java's blocking queue. The more things you learn. The lower the learning cost

  • Queue: first in, last out: rpush blpop, left head and right tail, right side enters the queue, left side exits the queue
  • Stack: first in, last out: rpush brpop

11. Draw

Comes with a random value

spop myset

12. Like, sign in, clock in

Where can Redis be used? 16 common usage scenarios shared

Suppose the above Weibo ID is t1001 and the user ID is u3001

Use like:t1001 to maintain t1001. All users who liked this Weibo

  • liked this Weibo: sadd like:t1001 u3001
  • Cancel like: srem like:t1001 u3001
  • Like or not: sismember like:t1001 u3001
  • All users who like it :smembers like:t1001
  • Likes: scard like:t1001

Isn’t it much simpler than the database?

13. Product tags

Where can Redis be used? 16 common usage scenarios shared

The old rule is to use tags:i5001 to maintain all tags of the product.

  • sadd tags:i5001 The picture is clear and delicate
  • sadd tags:i5001 True color clear display
  • sadd tags:i5001 The process is superb

14. Product selection

// 获取差集
sdiff set1 set2
// 获取交集(intersection )
sinter set1 set2
// 获取并集
sunion set1 set2

Where can Redis be used? 16 common usage scenarios shared

If: iPhone11 is on the market

sadd brand:apple iPhone11

sadd brand:ios iPhone11

sad screensize:6.0-6.24 iPhone11

sad screentype:lcd iPhone 11

Select products, Apple, ios, screen between 6.0-6.24 Sometimes, the screen material is LCD screen

sinter brand:apple brand:ios screensize:6.0-6.24 screentype:lcd

follow Follow fans Fans

Follow each other:

  • sadd 1:follow 2
  • sadd 2:fans 1
  • sadd 1:fans 2
  • sadd 2:follow 1

I follow People also followed him (take intersection):

  • sinter 1:follow 2:fans

People you may know:

  • 用户1可能认识的人(差集):sdiff 2:follow 1:follow
  • 用户2可能认识的人:sdiff 1:follow 2:follow

16、排行榜

id 为6001 的新闻点击数加1:

zincrby hotNews:20190926 1 n6001

获取今天点击最多的15条:

zrevrange hotNews:20190926 0 15 withscores

Where can Redis be used? 16 common usage scenarios shared

Redis 用的好,加薪少不了

原文地址:https://juejin.cn/post/6994229128534687781

作者:码猿技术专栏

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of Where can Redis be used? 16 common usage scenarios shared. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金--码猿技术专栏. If there is any infringement, please contact admin@php.cn delete
es和redis区别es和redis区别Jul 06, 2019 pm 01:45 PM

Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

一起来聊聊Redis有什么优势和特点一起来聊聊Redis有什么优势和特点May 16, 2022 pm 06:04 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

实例详解Redis Cluster集群收缩主从节点实例详解Redis Cluster集群收缩主从节点Apr 21, 2022 pm 06:23 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

Redis实现排行榜及相同积分按时间排序功能的实现Redis实现排行榜及相同积分按时间排序功能的实现Aug 22, 2022 pm 05:51 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

详细解析Redis中命令的原子性详细解析Redis中命令的原子性Jun 01, 2022 am 11:58 AM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

一文搞懂redis的bitmap一文搞懂redis的bitmapApr 27, 2022 pm 07:48 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

实例详解Redis实现排行榜及相同积分按时间排序功能的实现实例详解Redis实现排行榜及相同积分按时间排序功能的实现Aug 26, 2022 pm 02:09 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

redis error什么意思redis error什么意思Jun 17, 2019 am 11:07 AM

redis error就是redis数据库和其组合使用的部件出现错误,这个出现的错误有很多种,例如Redis被配置为保存数据库快照,但它不能持久化到硬盘,用来修改集合数据的命令不能用。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools