springbootredisTemplate简单锁简记

编程

当spring boot集成redis但是又未引入jedis时,简单锁写法如上

 

当spring boot集成redis同时又引入了jedis时,简单锁写法如下

    @Autowired

private RedisTemplate<String, Object> redisTemplate;

/**

* 简单锁 10s

*/

@SuppressWarnings("ConstantConditions")

private boolean sampleLock(String lockName){

return redisTemplate.execute((RedisCallback<Boolean>) redisConnection -> {

RedisSerializer valueSerializer = redisTemplate.getValueSerializer();

RedisSerializer keySerializer = redisTemplate.getKeySerializer();

Object obj = connection.execute("set",

keySerializer.serialize(key),

valueSerializer.serialize(value),

SafeEncoder.encode("NX"),

SafeEncoder.encode("EX"),

Protocol.toByteArray(exptime));

return obj != null;

});

}

private byte[] getBytes(String obj){

return obj.getBytes(StandardCharsets.UTF_8);

}

 

以上是 springbootredisTemplate简单锁简记 的全部内容, 来源链接: utcz.com/z/513592.html

回到顶部