当前位置:网站首页>[redis] ③ data elimination strategy, pipeline command, publish and subscribe
[redis] ③ data elimination strategy, pipeline command, publish and subscribe
2022-07-26 00:20:00 【Zhangguoqing (Qing Yi)】
Catalog
One 、 Data elimination strategy
Redis The biggest reason why it is fast is : It stores data in Memory In . The capacity of memory is incomparable with that of hard disk , The memory capacity will always reach the maximum . When the memory capacity reaches a certain value, you need to correct Redis Delete the data stored in memory ( Eliminate ) operation , Delete those data ? When to delete ?
stay Redis in , Allows the user to set the maximum memory size (Redis The maximum capacity of data that can be stored )【 stay Redis Configuration in the configuration file of 】
maxmemory 1G
maxmemory-policy noeviction # No elimination strategy , beyond 1G Report errors 
lft: according to key Use frequency To eliminate
lru: according to key Recent use of Time To eliminate
- volatile-lru: Set the timeout time in the data , Delete the least commonly used data
- allkeys-lru: all key Delete the least frequently used data in
- volatile-random: Delete randomly in the data with timeout set
- allkeys-random: be-all key Randomly delete
- volatile-ttl: Query all set timeout data , Then sort it immediately , Delete the data that is about to expire
- noeviction: Delete operation will not be performed , If the memory overflows, an error is reported
- volatile-lfu: Expel the least frequently used key from all keys configured with expiration time
- allkeys-lfu: Eject the least frequently used key from all keys
Two 、Java Basic use 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();
}
}
3、 ... and 、pipeline
- Put the command into the pipeline , Bulk send to Redis Server side , Improve performance , Greatly reduce network consumption
- There is no connection between commands executed in batches
- Not all data structures can be used pipeline command
- pipeline The operation of is not atomic

public class PipelineTest {
private Jedis jedis;
@Before
public void init() {
jedis = new Jedis("192.168.80.130", 6379);
}
@Test
public void testPipeline() {
Times.test(" Pipeline command is not used ", this::noPipeline);
Times.test(" Use the pipeline command ", this::pipeline);
}
private void noPipeline() {
for (int i = 0; i < 66666; i++) {
jedis.set("no" + i, String.valueOf(i));
}
}
private void pipeline() {
// Create pipes
Pipeline pipeline = jedis.pipelined();
for (int i = 0; i < 66666; i++) {
// Add commands to the pipeline
pipeline.set("pipeline" + i, String.valueOf(i));
}
// Synchronous execution of commands
pipeline.sync();
}
@After
public void destroy() {
if (jedis != null)
jedis.close();
}
}
Execution results :
Four 、 Publish subscribe


边栏推荐
猜你喜欢

How to use 120 lines of code to realize an interactive and complete drag and drop upload component?

分布式事务 :可靠消息最终一致性方案

Research progress of data traceability based on the perspective of data element circulation

Security document archiving software

Representation and implementation of stack (C language)

二进制表示--2的幂

Js理解之路:写一个比较完美的组合继承(ES5)

Appium中控件元素封装类梳理

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

Binary tree -- 111. Minimum depth of binary tree
随机推荐
12. Neural network model
基于网络分析和文本挖掘的意见领袖影响力研究
FreeRTOS个人笔记-信号量
IP Core: PLL
LeetCode_ 55_ Jumping game
京东获取推荐商品列表 API
Revision of Journal of Computational Physics
Leetcode high frequency question 66. add one, give you an array to represent numbers, then add one to return the result
mysql事务的四大特性以及隔离级别
FreeMarker view integration
[brother hero July training] day 24: linear tree
C语言 预处理详解
letfaw
Unity -- Euler angle, quaternion
关于拼多多根据关键词取商品列表 API 的使用说明
Thymeleaf view integration
redis的使用
Binary tree -- 222. Number of nodes of a complete binary tree
How long can this bull market last Answers to questions 2021-05-11
"Demons dance", is the bull market over? 2021-05-13