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…
发表回复