当前位置:网站首页>【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();
}
}
执行结果:
四、发布订阅


边栏推荐
- nodejs启动mqtt服务报错SchemaError: Expected `schema` to be an object or boolean问题解决
- 获得JD商品详情原数据 API
- VMware ESXI7.0版本的安装与配置
- Leetcode high frequency question 66. add one, give you an array to represent numbers, then add one to return the result
- 【论文笔记】—目标姿态估计—EPro-PnP—2022-CVPR
- OPENCV学习DAY6
- Binary tree - 112. Path sum
- Binary tree - 226. Flip binary tree
- 京东获取推荐商品列表 API
- appium 从启动到测试再到结束流程梳理
猜你喜欢

Getaverse,走向Web3的远方桥梁

栈的表示和实现(C语言)

"Animal coin" is fierce, trap or opportunity? 2021-05-12

Sort fake contacts

MySQL——多版本并发控制(MVCC)

letfaw

Unity—欧拉角,四元数
34 use of sparksql custom functions, architecture and calculation process of sparkstreaming, dstream conversion operation, and processing of sparkstreaming docking Kafka and offset

C语言 预处理详解

Binary tree -- 111. Minimum depth of binary tree
随机推荐
FreeRTOS personal notes - mutex
栈的表示和实现(C语言)
[one library] mapbox GL! A map engine out of the box
Old laptop becomes server (laptop + intranet penetration)
“动物币”凶猛,陷阱还是机遇?2021-05-12
appium 从启动到测试再到结束流程梳理
软件测试同行评审到底是什么?
用了MQ消息中间件后,我开始后悔了...
Getaverse,走向Web3的远方桥梁
Binary tree - 617. Merge binary tree
markdown写作平台
痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异
letfaw
Bond network card mode configuration
Under inflation, how to operate in the future? 2021-05-14
STM32 timer
如何让你的 JS 代码写得更漂亮
Leetcode200 - find detailed explanation of the number of islands
Binary tree -- 257. All paths of binary tree
拼多多根据ID取商品详情 API 的使用说明