opsForValue
/*value
* 存入token等
* */
ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
// 存
opsForValue.set("key", "value");
// 取
opsForValue.get("key");
//删除
stringRedisTemplate.delete("key");
opsForList
/*list
*
* */
ListOperations<String, String> opsForList = stringRedisTemplate.opsForList();
// 取
List<String> key = opsForList.range("key", 0, -1);
//判断是否为空
if (key.isEmpty()) {
}
// 存
opsForList.leftPush("key", "value");
//删除
stringRedisTemplate.delete("key");
opsForSet
/*set
* 判断是否点赞 是否收藏
* */
SetOperations<String, String> opsForSet = stringRedisTemplate.opsForSet();
// 判断是否存在
Boolean isMember = opsForSet.isMember("user:collect:", "");
// 添加缓存
opsForSet.add("user:collect:", "");
// 删除缓存
opsForSet.remove("user:collect:", "");
opsForZSet
/*zet
*计数
*/
ZSetOperations<String, String> opsForZSet = stringRedisTemplate.opsForZSet();
// 存(1、是要统计什么内容 2、统计的那个用户 3、1是每次加一,-1是每次减一)
opsForZSet.incrementScore("key", "用户ID", 1);
opsForZSet.incrementScore("key", "用户ID", -1);
// 取
Set<ZSetOperations.TypedTuple<String>> rangeWithScores = opsForZSet.rangeWithScores("key", 0, -1);
for (ZSetOperations.TypedTuple<String> rangeWithScore : rangeWithScores) {
// 这个是给谁计数的(业务就是哪个用户)
String zSetKey = rangeWithScore.getValue();
// 这个是计数的(业务就是登录次数)
int i = rangeWithScore.getScore().intValue();
}
opsForHash
/*hash*/
HashOperations<String, Object, Object> opsForHash = stringRedisTemplate.opsForHash();
// 存1
opsForHash.increment("key", "mapKey", 1);
opsForHash.increment("key", "mapKey", -1);
// 取1
Map<Object, Object> entries = opsForHash.entries("key");
// 取map1里面的数据
Object value1 = entries.get("key");
// 存
opsForHash.putAll("key", new HashMap<>());
// 取
Object value2 = opsForHash.get("key", "mapKey");
这些只是stringRedisTemplate中的冰山一角,更多的功能欢迎大家评论区留言!