当前位置:网站首页>redis 模糊查询 批量删除
redis 模糊查询 批量删除
2022-06-13 08:57:00 【Little___Turtle】
场景: 文章评论分页=>redis存 key为articleId+页数 更新了点东西不好删除该分页涉及到的的全部内容
思路: 把分页的key全部查找出来 然后依次便利删除
实现:
/**
* 查找匹配key
*
* @param pattern key
* @return /
*/
public List<String> scan(String pattern) {
ScanOptions options = ScanOptions.scanOptions().match(pattern + "*").build();
RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
RedisConnection rc = Objects.requireNonNull(factory).getConnection();
Cursor<byte[]> cursor = rc.scan(options);
List<String> result = new ArrayList<>();
while (cursor.hasNext()) {
result.add(new String(cursor.next()));
}
try {
RedisConnectionUtils.releaseConnection(rc, factory);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 清除缓存分页
* 实现思路 key:普通key redis模糊匹配出对应的key 然后便利删除
* @param redisKeyPrefix
* @return Boolean
* @author zhang
* @date 2022/5/24 8:18
*/
public void deleteKeys(String redisKeyPrefix) {
//判定key是否存在 查找匹配key
log.info("匹配到的redisKeyPrefix为============={}=============", redisKeyPrefix);
List<String> list = scan(redisKeyPrefix);
log.info("匹配到的key为============={}=============", list);
if (ObjectUtil.isNotEmpty(list)) {
list.stream()
.filter(Objects::nonNull)
.forEach(s -> {
Boolean del = del(s);
log.info("删除==={}", del);
}
);
}
使用:
@Autowired
private RedisUtil redisUtil;
@Test
void testPay() {
redisUtil.deleteKeys("articleComment::151742802195560448");
}
结果展示:
边栏推荐
- 5. Attribute selector
- 15. copy constructor
- CentOS installing MySQL and setting up remote access
- Collection of garbled code problems in idea development environment
- 网络安全漏洞分析之重定向漏洞分析
- MySQL 8.0 modifying SQL_ mode=only_ full_ group_ by
- Redirect vulnerability analysis of network security vulnerability analysis
- 国债逆回购能开户吗,国债逆回购在APP上可以直接开通券商安全吗 ,买股票怎么网上开户
- Number of parameters of pytorch statistical model
- 20211115 矩阵对角化的充要条件;满秩矩阵不一定有n个线性无关的特征向量;对称矩阵一定可以对角化
猜你喜欢
4、 Js-es5-i / O
【安全】零基础如何从0到1逆袭成为安全工程师
20211104 为什么相似矩阵的迹相同
Knowledge points related to system architecture 3
How does jupyter notebook directly output the values of multiple variables after running?
Use of grep
4. Relationship selector (parent-child relationship, ancestor offspring relationship, brother relationship)
Uni app essay
教程篇(5.0) 02. 管理 * FortiEDR * Fortinet 网络安全专家 NSE 5
VI editor
随机推荐
What are the bank financial products? How long is the liquidation period?
GBase 8a磁盘问题及处理
Top+jstack to analyze the causes of excessive CPU
pytorch统计模型的参数个数
Redis distributed cluster setup
12. constructor explanation, explicit, initialization list
20211115 矩阵对角化的充要条件;满秩矩阵不一定有n个线性无关的特征向量;对称矩阵一定可以对角化
Jfinal and swagger integration
How to become a white hat hacker? I suggest you start from these stages
Uni app subcontracting loading and optimization
Detailed explanation of C language callback function
How excel adds hyperlinks to some text in a cell
Collection of various books
Paging query template of Oracle
关于RSA加密解密原理
JS string method
Onnx crop intermediate node
1.初步认识express
PHP wechat special merchant incoming V3 packaging interface
The Jenkins console does not output custom shell execution logs