乐趣区

关于java:redisTemplate批量操作

1.redisTemplate 的 incr 自增

    public Integer incr(String key, Date expireDate) {key = getKey(key);
        RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        entityIdCounter.expireAt(expireDate);
        Long increment = entityIdCounter.incrementAndGet();
        return increment.intValue();}

2. 查问蕴含某字符的 key 列表 scan 操作

    public Set<String> scan(String matchKey) {Set<String> keys = (Set<String>) redisTemplate.execute((RedisCallback<Set<String>>) connection -> {Set<String> keysTmp = new HashSet<>();
            Cursor<byte[]> cursor = connection.scan(new ScanOptions.ScanOptionsBuilder().match(matchKey + "*").count(1000).build());
            while (cursor.hasNext()) {keysTmp.add(new String(cursor.next()));
            }
            return keysTmp;
        });
        return keys;
    }

3. 批量查问 key 对应的值列表

// 批量查问缓存列表,返回值按顺序排列,在某个 key 不存在时,对应地位返回 null
    public List<Integer> getMutiIncr(Collection<String> list) {if (CollectionUtils.isEmpty(list)){return new ArrayList<>();
        }
        ValueOperations ops = redisTemplate.opsForValue();
        return ops.multiGet(list);
    }

4. 批量删除 key 列表对应的缓存

    public void deleteBachKeysWithEnv(Collection<String> keys) {if (!CollectionUtils.isEmpty(keys)) {redisTemplate.delete(keys);
        }
    }

5.key 的指定

hash 构造通常存储 map 类型的数据,不适宜存有 incr 需要的数据
key1:key2:key3 构造的数据,不便读取 

https://www.jianshu.com/p/4c8…

退出移动版