当前位置:网站首页>Redis使用场景
Redis使用场景
2022-07-25 09:26:00 【colours starry sky】
1、缓存
Redis当今最为人熟知的使用场景。再提升服务器性能方面非常有效; 一些频繁被访问的数据,经常被访问的数据如果放在关系型数据库,每次查询的开销都会很大,而放在redis中, 因为redis 是放在内存中的可以很高效的访问。
例如:热点数据缓存(例如报表、明星出轨),对象缓存、全页缓存、可以提升热点数据的访问数据。
2、数据共享分布式
String 类型,因为 Redis 是分布式的独立服务,可以在多个应用之间共享
例如:分布式Session
1 2 3 4 |
|
默认Session是保存在服务器的文件中,如果是集群服务,同一个用户过来可能落在不同机器上, 这就会导致用户频繁登陆;采用Redis保存Session后,无论用户落在那台机器上都能够获取到对应的Session信 息。
3、排行榜
在使用传统的关系型数据库(mysql oracle 等)来做这个事儿,非常的麻烦,而利用Redis的SortSet(有序集合) 数据结构能够简单的搞定;

4.抽奖
自带一个随机获得值
1 |
|
5、购物车
String 或hash。所有String可以做的hash都可以做

- key:用户id;field:商品id;value:商品数量。
- +1:hincr。-1:hdecr。删除:hdel。全选:hgetall。商品数:hlen。
6、计算器/限速器
int类型,incr方法
Redis中原子性的自增操作,我们可以统计类似用户点赞数、用户访问数等,这类操作如果用MySQL,频繁的读 写会带来相当大的压力;限速器比较典型的使用场景是限制某个用户访问某个API的频率,常用的有抢购时,防 止用户疯狂点击带来不必要的压力;
7、简单消息队列
除了Redis自身的发布/订阅模式,我们也可以利用List来实现一个队列机制,比如:到货通知、邮件发送之类的 需求,不需要高可靠,但是会带来非常大的DB压力,完全可以用List来完成异步解耦;
List提供了两个阻塞的弹出操作:blpop/brpop,可以设置超时时间
- blpop:blpop key1 timeout 移除并获取列表的第一个元素,如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
- brpop:brpop key1 timeout 移除并获取列表的最后一个元素,如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
上面的操作。其实就是java的阻塞队列。学习的东西越多。学习成本越低
- 队列:先进先除:rpush blpop,左头右尾,右边进入队列,左边出队列
- 栈:先进后出:rpush brpop
8、分布式锁
分布式场景下,无法使用单机环境下的锁来对多个节点上的进程进行同步。可以使用 Redis 自带的 SETNX 命令 实现分布式锁,除此之外,还可以使用官方提供的 RedLock 分布式锁实现。
1 2 3 4 5 6 7 8 9 10 11 |
|
String 类型setnx方法,只有不存在时才能添加成功,返回true
边栏推荐
- 概率论与数理统计 3 Discrete Random Variables and Probability Distributions(离散随机变量与概率分布) (下篇)
- 1、 Initial mysql, MySQL installation, environment configuration, initialization
- 小程序H5获取手机号方案
- 代码整洁之道--直击痛点
- ~2 CCF 2022-03-1 uninitialized warning
- LoRA转4G及网关中继器工作原理
- Introduction to testbench
- 概率机器人学习笔记第二章
- Data viewing and parameter modification of multi-channel vibrating wire, temperature and analog sensing signal acquisition instrument
- oracle 解析同名xml 问题
猜你喜欢

Download and installation of QT 6.2
![[deployment of deep learning model] deploy the deep learning model using tensorflow serving + tornado](/img/62/78abf16bb6c66726c6e394c9fb4f81.png)
[deployment of deep learning model] deploy the deep learning model using tensorflow serving + tornado

TM1638 LED数码显示模块ARDUINO驱动代码

Mlx90640 infrared thermal imaging sensor temperature measurement module development notes (II)

Probabilistic robot learning notes Chapter 2

Configuring ROS development environment with vscode: Causes and solutions to the problem of ineffective code modification

Mlx90640 infrared thermal imager temperature measurement module development instructions

文件的上传功能

安装 oh my zsh

Internal structure of SOC chip
随机推荐
CCF 201604-2 Tetris
Probability theory and mathematical statistics 4 continuous random variables and probability distributions (Part 1)
mysql历史数据补充新数据
Data viewing and parameter modification of multi-channel vibrating wire, temperature and analog sensing signal acquisition instrument
动态规划,购物单问题
canal实现mysql数据同步
拷贝过来老的项目变成web项目
小程序企业发放红包功能
ESP32连接阿里云MQTT物联网平台
js利用requestAnimationFrame实时检测当前动画的FPS帧率
Probability theory and mathematical statistics 3 discrete random variables and probability distributions (Part 2)
LOAM 融合 IMU 细节之 TransformToEnd 函数
ROS distributed operation -- launch file starts nodes on multiple machines
Introduction to Verdi Foundation
链表相关(设计链表及环链表问题)
Qt 6.2的下载和安装
数据库MySQL详解
js数字千位分割的常用方法
The economic basis of a hardware siege lion
Excel导入导出源码分析