久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

spring redis 如何實現模糊查找key

瀏覽:2日期:2023-06-26 11:57:03
spring redis 模糊查找key用法

Set<String> keySet = stringRedisTemplate.keys('keyprefix:'+'*'); 需要使用StringRedisTemplate,或自定義keySerializer為StringRedisSerializer的redisTemplate redis里模糊查詢key允許使用的通配符:

* 任意多個字符

? 單個字符

[] 括號內的某1個字符

源碼

org.springframework.data.redis.core.RedisTemplatepublic Set<K> keys(K pattern) { byte[] rawKey = rawKey(pattern); Set<byte[]> rawKeys = execute(connection -> connection.keys(rawKey), true); return keySerializer != null ? SerializationUtils.deserialize(rawKeys, keySerializer) : (Set<K>) rawKeys;}改善 Redis2.8以后可以使用scan獲取key 基于游標迭代分次遍歷key,不會一次性掃描所有key導致性能消耗過大,減少服務器阻塞

可以通過count參數設置掃描的范圍

Set<String> keys = new LinkedHashSet<>();stringRedisTemplate.execute((RedisConnection connection) -> { try (Cursor<byte[]> cursor = connection.scan( ScanOptions.scanOptions() .count(Long.MAX_VALUE) .match(pattern) .build() )) {cursor.forEachRemaining(item -> { keys.add(RedisSerializer.string().deserialize(item));});return null; } catch (Exception e) {throw new RuntimeException(e); }});

Reids SCAN命令官方文檔

redis-redisTemplate模糊匹配刪除

 String key = 'noteUserListenedPoi:*'; redisTemplate.delete(key); LOGGER.info('redis中用戶收聽歷史被清空');

后來測試發現模糊查詢是可以用的, 刪除改成

Set<String> keys = redisTemplate.keys('noteUserListenedPoi:' + '*'); redisTemplate.delete(keys); LOGGER.info('{}, redis中用戶收聽歷史被清空'

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 牡丹江市| 苏尼特左旗| 仲巴县| 大名县| 巴林左旗| 海南省| 哈密市| 龙州县| 晋江市| 道真| 深州市| 文成县| 白朗县| 南京市| 筠连县| 孟津县| 南投市| 防城港市| 峨边| 从化市| 大埔区| 江西省| 贵定县| 稷山县| 龙岩市| 沾益县| 黔江区| 綦江县| 建瓯市| 衡水市| 吴旗县| 武邑县| 罗江县| 阳新县| 鲁山县| 延边| 靖西县| 赣州市| 元江| 永丰县| 贵阳市|