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

推荐订阅源

T
The Blog of Author Tim Ferriss
I
InfoQ
H
Hackread – Cybersecurity News, Data Breaches, AI and More
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
有赞技术团队
有赞技术团队
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
博客园_首页
Y
Y Combinator Blog
V
Visual Studio Blog
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
雷峰网
雷峰网
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
H
Help Net Security
P
Proofpoint News Feed
B
Blog
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - 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());