@Component
public class MemcachedLock {
@Autowired
protected CacheUtils cacheUtils;
private final int minutes = 180;
public boolean lock(String key) {
if (cacheUtils.get(key) == null) {
if (cacheUtils.add(key, minutes, System.currentTimeMillis())) {
return true;
}
}
return false;
}
}
memcached实现分布式锁
最新推荐文章于 2025-11-29 00:59:33 发布
本文介绍了一种使用Memcached实现分布式锁的方法。通过检查指定键是否存在,如果不存在则创建该键并设置过期时间,以此来实现加锁。该方法适用于需要在分布式环境中协调资源访问的场景。
172万+

被折叠的 条评论
为什么被折叠?



