value值为随意的字符串,如 "1"
public boolean lock( String userId, String value, int timeOut ) {
return this.strSetIfAbsent( "lock:".concat( userId ), value, Duration.ofSeconds( timeOut ) );
}
public void unLock( String userId ) {
this.unlink( "lock:".concat( userId ) );
}
/**
* 只在键 key 不存在的情况下,将键 key 的值设置为 value 。<br/> 若键 key 已经存在, 则不做任何动作。<br/>添加过期时间
*
* @param timeout 过期时间
*/
public Boolean strSetIfAbsent( String key, String value, Duration timeout ) {
return stringRedisTemplate.opsForValue().setIfAbsent( key, value, timeout );
}
/** * 删除一个或多个key-value,但是,相比DEL会产生阻塞,该命令会在另一个线程中回收内存,因此它是非阻塞的。 */ public Boolean unlink( String keys ) { return stringRedisTemplate.unlink( keys ); }