当前位置:网站首页>【redis】③ 数据淘汰策略、pipeline 管道命令、发布订阅
【redis】③ 数据淘汰策略、pipeline 管道命令、发布订阅
2022-07-26 00:10:00 【张国庆(庆壹)】
一、数据淘汰策略
Redis 之所以快最大的原因是:它把数据存储在内存之中。内存的容量和硬盘是无法比拟的,内存的容量总会达到最大值。当内存的容量达到某个值的时候需要对 Redis 存储在内存中的数据进行删除(淘汰)操作,删除那些数据呢?何时删除呢?
在 Redis 中,允许用户设置最大的内存大小(Redis 能够存储的数据的最大容量)【在 Redis 的配置文件中配置】
maxmemory 1G
maxmemory-policy noeviction # 没有淘汰策略,超出1G报错
lft:根据 key 的使用次数进行淘汰
lru:根据 key 的最近使用时间进行淘汰
- volatile-lru:设定超时时间的数据中,删除最不常用的数据
- allkeys-lru:所有 key 中最不常使用的数据进行删除
- volatile-random:在已经设定了超时的数据中随机删除
- allkeys-random:所有的 key 中随机删除
- volatile-ttl:查询全部设定超时时间的数据,随后马上排序,将马上要过期的数据删除掉
- noeviction:不会进行删除操作,如果内存溢出则报错
- volatile-lfu:从所有配置了过期的时间的键中驱逐使用频率最少的键
- allkeys-lfu:从所有键中驱逐使用频率最少的键
二、Java 基本使用 Redis(Jedis)
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
public class BasicTest {
private Jedis jedis;
@Before
public void init() {
jedis = new Jedis("192.168.80.130", 6379);
}
@Test
public void test1() {
jedis.flushAll();
System.out.println(jedis.dbSize());
}
@After
public void destroy() {
if (jedis != null)
jedis.close();
}
}
三、pipeline
- 把命令放入管道之中,批量发送到 Redis 服务端,提高性能,大大减少网络消耗
- 批量执行的命令之间相互没有联系
- 并不是所有的数据结构能够使用 pipeline 命令
- pipeline 的操作并非原子性的

public class PipelineTest {
private Jedis jedis;
@Before
public void init() {
jedis = new Jedis("192.168.80.130", 6379);
}
@Test
public void testPipeline() {
Times.test("没有使用管道命令", this::noPipeline);
Times.test("使用管道命令", this::pipeline);
}
private void noPipeline() {
for (int i = 0; i < 66666; i++) {
jedis.set("no" + i, String.valueOf(i));
}
}
private void pipeline() {
// 创建管道
Pipeline pipeline = jedis.pipelined();
for (int i = 0; i < 66666; i++) {
// 往管道中添加命令
pipeline.set("pipeline" + i, String.valueOf(i));
}
// 同步执行命令
pipeline.sync();
}
@After
public void destroy() {
if (jedis != null)
jedis.close();
}
}
执行结果:
四、发布订阅


边栏推荐
- Unity—欧拉角,四元数
- Binary tree - 404. Sum of left leaves
- 计算物理期刊修改
- 复盘:推荐系统—— 负采样策略
- The bull market is not over yet, and there is still 2021-05-18 in the second half
- Redirection and request forwarding
- 京东按关键字搜索商品 API 的使用说明
- markdown写作平台
- Matlab makes the image of serial port output data in real time
- 07_ UE4 advanced_ MP value of firing fireball and mechanism of attacking blood deduction
猜你喜欢

Js理解之路:Object.call与Object.create()实现继承的原理

MPLS实验

Binary tree -- 104. Maximum depth of binary tree

Installation and configuration of VMware esxi7.0

YoloV4-tiny网络结构

How long can this bull market last Answers to questions 2021-05-11

Binary tree - 226. Flip binary tree

如何用120行代码,实现一个交互完整的拖拽上传组件?

LeetCode高频题66. 加一,给你一个数组表示数字,则加1返回结果

Backtracking - 17. Letter combinations of phone numbers
随机推荐
matlab实时作出串口输出数据的图像
FreeRTOS personal notes - mutex
Binary tree - 404. Sum of left leaves
测试7年,面试华为最后面议要薪1万,HR说我不尊重华为,他们没有那么低薪资的岗位~
如何用120行代码,实现一个交互完整的拖拽上传组件?
Js理解之路:什么是原型链
京东获取推荐商品列表 API
JSON data development
MPLS中的包交换和标签交换
Sort fake contacts
Pytoch learning record (I): introduction to pytoch
J9数字论:什么是DAO模式?DAO发展过程的阻碍
Binary tree - 110. Balanced binary tree
The bull market will continue. Take your money 2021-05-08
Appium中控件元素封装类梳理
MySQL - database log
C语言 预处理详解
Unity -- Euler angle, quaternion
网站服务器停止响应是什么意思?
自动化测试之数据驱动DDT