惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
D
Docker
月光博客
月光博客
MongoDB | Blog
MongoDB | Blog
Vercel News
Vercel News
美团技术团队
博客园 - 叶小钗
I
InfoQ
Jina AI
Jina AI
博客园 - 司徒正美
雷峰网
雷峰网
B
Blog
Y
Y Combinator Blog
A
About on SuperTechFans
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
Microsoft Security Blog
Microsoft Security Blog
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Announcements
Recent Announcements
V
V2EX
N
Netflix TechBlog - Medium

博客园 - Johnson_wang

对云服务器挂载cos桶 记录一次xxlJob升级版本导致的 xxl-job remoting error(Connection reset) 记一次NoClassDeffoundEror问题解决过程 nginx配置支持ws,并解决跨域 关于消费端接入dubbo,连接失败问题 RedisTemplate关于key出现前缀\xac\xed\x00\x05t\x00\x0f - Johnson_wang - 博客园 ES 单索引大表拆分 关于ES索引被聚合查询导致filedata堵塞 (pressure too high, (smooth) bulk request circuit break) 发送HTML格式邮件 elasticsearch报错FORBIDDEN/12/index read-only / allow delete spring Boot 相关问题以及修复(后续待补充) mybatis数据加解密处理方案 队列缓存区-db写入 synchronized的实现原理 Scheduler踩坑记录 关于HashMap的加载因子相关理解 Mybatis 分页插件PageHelper 遇坑 Linux 下 Mysql忘记密码重置 Eclipse MAT和jvisualvm分析内存溢出
关于RedisTemplate的map存储踩坑记录
Johnson_wang · 2020-07-29 · via 博客园 - Johnson_wang

现在需要在Redis中存储一个map,使用RedisTemplate 建立链接后,执行以下代码:

//cacheKey  缓存key
Map<String,String> map = new HashMap<String,String>();
map.put("a","1");
map.put("b","2");
map.put("c","3");
template.boundHashOps(cacheKey).putAll(map);

  发现Redis存储数据成功。

    现在想要修改map中的数据,当前需要批量修改 将 key = a/b 的数据value修改为3;

还是使用上面的逻辑,执行成功,数据变更。

现在想要把key=a的数据删除,写了如下代码:

Map<String,String> map = new HashMap<String,String>();
map.put("b","2");
map.put("c","3");
template.boundHashOps(cacheKey).putAll(map);

  发现Redis中当前cacheKey对应的数据还是3条记录,后面查看源码:

 public void putAll(K key, Map<? extends HK, ? extends HV> m) {
        if (!m.isEmpty()) {
            byte[] rawKey = this.rawKey(key);
            Map<byte[], byte[]> hashes = new LinkedHashMap(m.size());
            Iterator var5 = m.entrySet().iterator();

            while(var5.hasNext()) {
                Entry<? extends HK, ? extends HV> entry = (Entry)var5.next();
                hashes.put(this.rawHashKey(entry.getKey()), this.rawHashValue(entry.getValue()));
            }

            this.execute((connection) -> {
                connection.hMSet(rawKey, hashes);
                return null;
            }, true);
        }
    }

putAll方法只对传入的map对应key做了处理,并不是对整个缓存的key做覆盖,只能做追加和修改。

所以需要使用hdel删除对应key:

template.boundHashOps(cacheKey).delete(needRemoveKeys.toArray());