ValueOperations:字符串类型操作
@Resource(name = "stringRedisTemplate") private ValueOperations<String, Object> valueOpsCache;
例如redis获取锁,获取到锁的线程执行业务逻辑
//redis 锁
String redisKey = RedisConstant.KEY;
if (!valueOpsCache.setIfAbsent(redisKey, "1")) {
return true;
}
valueOpsCache.set(redisKey, "1", 300, TimeUnit.SECONDS);
底层就是调用的setnx命令,如果设置成功就获取到锁