个人记录:
redis:旧版2.1以前,为了达到redis分布式锁基本都是加锁+加唯一ID+过期时间
具体实现代码:重写setIfAbsent
public Boolean setIfAbsent(String key, String value, long timeout, TimeUnit unit) {
byte[] rawKey = key.getByte();
byte[] rawValue = value.getByte();
Expiration expiration = Expiration.from(timeout, unit);
return redisTemplate.execute(connection -> connection.set(rawKey, rawValue, expiration, RedisStringCommands.SetOption.ifAbsent()), true);
}
redis:2.1以后就没这么多东西,直接用
redisTemplate.opsForValue().setIfAbsent(K key,V value,long timeout,TimeUnit unit)