当前位置:网站首页>在 RedisTemplate 中使用 scan
在 RedisTemplate 中使用 scan
2022-06-29 08:21:00 【lockie_zou】
SCAN 简介
SCAN 命令及其相关的 SSCAN 命令、 HSCAN 命令和 ZSCAN 命令都用于增量地迭代(incrementally iterate)一集元素(a collection of elements):
SCAN 和 KEYS 的区别
当 KEYS 命令被用于处理一个大的数据库时, 又或者 SMEMBERS 命令被用于处理一个大的集合键时, 它们会锁定 redis 库, 可能会阻塞服务器达数秒之久。在高并发下会导致请求大量堆积进而导致服务雪崩。有些公司在生产环境直接禁用 kyes * 命令。但是在 redis 服务器 key 的数量不大的情况下,使用 keys 也是没啥问题的。
SCAN 命令及其相关的 SSCAN 命令、 HSCAN 命令和 ZSCAN 命令都用于增量地迭代 ,它们每次执行都只会返回少量元素,不会阻塞服务器, 所以这些命令可以用于生产环境, 而不会出现像 KEYS 命令、 SMEMBERS 命令带来的问题。
SCAN 一样有它自己的问题:
- 因为是分段获取 key,所以它会多次请求 redis 服务器,这样势必取同样的 key,scan 耗时更长。
- 在对键进行增量式迭代的过程中, 键可能会被修改, 所以增量式迭代命令只能对被返回的元素提供有限的保证
/** * redis扩展工具 * * @author yuhao.wang3 * @since 2020/2/21 23:35 */ public abstract class RedisHelper { private static Logger logger = LoggerFactory.getLogger(RedisHelper.class); /** * scan 实现 * * @param redisTemplate redisTemplate * @param pattern 表达式,如:abc*,找出所有以abc开始的键 */ public static Set<String> scan(RedisTemplate<String, Object> redisTemplate, String pattern) { return redisTemplate.execute((RedisCallback<Set<String>>) connection -> { Set<String> keysTmp = new HashSet<>(); try (Cursor<byte[]> cursor = connection.scan(new ScanOptions.ScanOptionsBuilder() .match(pattern) .count(10000).build())) { while (cursor.hasNext()) { keysTmp.add(new String(cursor.next(), "Utf-8")); } } catch (Exception e) { logger.error(e.getMessage(), e); throw new RuntimeException(e); } return keysTmp; }); } }
边栏推荐
- Is it safe for the top ten securities companies to open accounts? Is it reliable?
- 各种级数(调和、几何)总结
- The sixth season of 2022 perfect children's model Qingyuan competition area audition came to a successful conclusion
- New spark in intelligent era: wireless irrigation with Lora wireless transmission technology
- 对话| 数字时代,隐私计算的发展前景与挑战
- Core development board & debugger
- 2022第六季完美童模 清远赛区 海选赛圆满落幕
- 闭关修炼(二十四)浅入了解跨域问题
- 闭关修炼(二十)如何做好单元测试
- 关于父母离婚后子女姓名变更有关问题的批复
猜你喜欢

The return values of hostname -f and uname -n may be different

Typescript variable declaration - type assertion

Does the SQL server run with administrator privileges? Or run it as a normal user?

Résumé des différentes séries (harmoniques, géométriques)

城通网盘仿蓝奏网盘源码 附带视频教程

sql server 用 administrator 权限运行吗?还是以普通用户运行呢?

闭关修炼(二十四)浅入了解跨域问题

人民链鲍大伟:打破壁垒,建立全域数据治理共享及应用平台

Leetcode (142) - circular linked list II
![Target tracking [single target tracking (vot/sot), target detection, pedestrian re identification (re ID)]](/img/f2/d42032f05214a4ad9339ea18966cc2.jpg)
Target tracking [single target tracking (vot/sot), target detection, pedestrian re identification (re ID)]
随机推荐
批量处理实验接触角数据-MATLAB分析
ES6数据类型Map&Set
Feature selection: maximum information coefficient (MIC) [used to measure the degree of correlation between two variables X and y, linear or nonlinear strength, commonly used for feature selection of
Oracle-子查询
sql server 用 administrator 权限运行吗?还是以普通用户运行呢?
sed 替换值为变量
hugetlbfs的写时复制
Is the securities account of qiniu school really safe and reliable?
TypeScript 變量聲明 —— 類型斷言
积分商城运营要如何做才能获取到利润
(III) encoder self attention mask
P6776-[NOI2020]超现实树
MQTT第二话 -- emqx高可用集群实现
Standard | China payment and clearing Association releases the first privacy computing financial specification
A high-frequency problem, three kinds of model thinking to solve this risk control problem
Uber前安全主管面临欺诈指控 曾隐瞒数据泄露事件
Huawei equipment is configured with small network WLAN basic services
NP3 格式化输出(一)
机器人代码生成器之Robcogen使用教程
Tutorial on building open source Internet of things platform