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...
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。