当前位置:网站首页>Using scan in redistemplate
Using scan in redistemplate
2022-06-29 09:01:00 【lockie_ zou】
SCAN brief introduction
SCAN Orders and related SSCAN command 、 HSCAN Command and ZSCAN Commands are used to iterate incrementally (incrementally iterate) An episode of elements (a collection of elements):
- SCAN The command is used to iterate over the database keys in the current database .
- SSCAN The command is used to iterate over the elements in the set key .
- HSCAN The command is used to iterate over key value pairs in hash keys .
- ZSCAN The command is used to iterate over elements in an ordered set ( Include element members and element scores ).
SCAN and KEYS The difference between
When KEYS When the command is used to process a large database , Or, SMEMBERS When the command is used to handle a large set key , They lock redis library , May block the server for several seconds . High concurrency can cause a large number of requests to pile up and cause service avalanche . Some companies directly disable it in the production environment kyes * command . But in redis The server key When the number is small , Use keys There's no problem .
SCAN Orders and related SSCAN command 、 HSCAN Command and ZSCAN Commands are used to iterate incrementally , They return only a few elements each time they execute , It doesn't block the server , So these commands can be used in the production environment , But not like KEYS command 、 SMEMBERS Problems with orders .
SCAN It has its own problems :
- Because it's segmentation key, So it will ask for redis The server , This is bound to take the same key,scan Takes longer .
- In the process of incrementally iterating on the bond , The key may be modified , So the incremental iteration command can only provide limited guarantee for the returned elements
/** * redis Extension tools * * @author yuhao.wang3 * @since 2020/2/21 23:35 */ public abstract class RedisHelper { private static Logger logger = LoggerFactory.getLogger(RedisHelper.class); /** * scan Realization * * @param redisTemplate redisTemplate * @param pattern expression , Such as :abc*, Find out everything to abc The start key */ 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; }); } }
边栏推荐
- 图解SOC中的Timer(一):系统里有哪些Timer?
- 闭关修炼(二十五)基础web安全
- Verilog 大小端以及 +:使用
- The @dynamicmemberlookup and callasfunction features in swift implement the object transparent proxy function
- 手写 redux-thunk
- Intelligent hardware EVT DVT PVT mp
- JS获取图片或base64的宽高等基本信息
- sql server 用 administrator 权限运行吗?还是以普通用户运行呢?
- Self attention mechanism
- ES6 data type map & set
猜你喜欢
随机推荐
sql server 用 administrator 权限运行吗?还是以普通用户运行呢?
Wechat applet development, how to add multiple spaces
The sixth season of 2022 perfect children's model Qingyuan competition area audition came to a successful conclusion
51单片机中断与定时器计数器,基于普中科技HC6800-ESV2.0
[microservices openfeign] timeout of openfeign
今年的网络安全“体检”你做了吗?
Oracle-子查询
Analysis of c voice endpoint detection (VAD) implementation process
mysql insert 时出现Deadlock死锁场景分析
Tutorial on building open source Internet of things platform
ThinkPHP 6 使用 mongoDB
工厂模式
CDGA|交通行业做好数字化转型的核心是什么?
Self attention mechanism
Baodawei of the people's Chain: break down barriers and establish a global data governance sharing and application platform
Compare homekit, MI family, and zhiting family cloud edition for what scene based experiences
Résumé des différentes séries (harmoniques, géométriques)
2022第六季完美童模 合肥賽區 决賽圓滿落幕
Batch processing of experimental contact angle data matlab analysis
Leetcode (142) - circular linked list II



![[untitled]](/img/6e/5dd5dcff89a74f7d367c9186a77268.png)





