简单朋友圈项目中可能用到的stringRedisTemplate的基础应用

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中的冰山一角,更多的功能欢迎大家评论区留言!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值