呃,可能没什么用,就当练习
场景:注册用户时,userId 基于上一个用户+1
实现代码如下
public String getId()
{
boolean flag = redisTemplate.hasKey("userId");
if (flag)
{
String a =redisTemplate.opsForValue().get("userId").toString();
int b = Integer.parseInt(a)+1;
redisTemplate.opsForValue().set("userId",b);
}else{
redisTemplate.opsForValue().set("userId",1);
}
String userId =redisTemplate.opsForValue().get("userId").toString();
System.out.println(userId);
return userId;
}
解释:
1.先判断redis中是否有userId这个key
2.如果没有从1开始
3.如果有object转string,再转int +1
4.然后放入