eladmin icon indicating copy to clipboard operation
eladmin copied to clipboard

redis hset 尝试 expire 整个 hash 表,可以看作 bug 或者方法歧义

Open riclava opened this issue 1 year ago • 0 comments

RedisUtils 中的 hashSet 是不支持对 hash 内部 key 进行 expire,此方法会产生歧义,会直接删除整个 hash table

参考 how-to-expire-the-hset-child-key-in-redis

  • src/main/java/me/zhengjie/utils/RedisUtils.java
    /**
     * 向一张hash表中放入数据,如果不存在将创建
     *
     * @param key   键
     * @param item  项
     * @param value 值
     * @param time  时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
     * @return true 成功 false失败
     */
    public boolean hset(String key, String item, Object value, long time) {
        try {
            redisTemplate.opsForHash().put(key, item, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        }
    }

riclava avatar Jul 29 '22 08:07 riclava