redis 正则匹配符合条件的key 进行删除

复制代码
//根据名字的key,如果key数量超过100,就进行一次删除

public
int clearRedis(@PathVariable("prefix") String prefix) throws IOException { ScanOptions options = ScanOptions.scanOptions().match(prefix + "*").count(1000).build(); Cursor cursor = redisTemplate.getConnectionFactory().getConnection().scan(options); int index = 0; try { List<String> list = new ArrayList<>(); while (cursor.hasNext()) { index++; try { String key = new String((byte[]) cursor.next()); list.add(key); if (list.size() >= 100) { redisTemplate.unlink(list); log.info("删除{},{},{},{}", index, cursor.getPosition(), list.get(0), key); list.clear(); } } catch (Exception e) { log.error("error,{},{}", index, cursor.getPosition()); } } if (list.size() > 0) { redisTemplate.unlink(list); log.info("删除{},{},{},{}", index, cursor.getPosition(), list.get(0), list.get(list.size() - 1)); list.clear(); } } finally { cursor.close(); } return index; }
复制代码

 

posted @   翱翔的小鱼  阅读(218)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示