分布式锁公共方法

This commit is contained in:
Jack 2025-11-12 10:46:39 +08:00
parent a43df002dc
commit b06826593a

View File

@ -7,6 +7,34 @@ import org.springframework.stereotype.Component;
import java.util.UUID;
/**
* 分布式锁工具类
*
* <p>使用示例:</p>
* <pre>
* {@code
* // 方式1: 简单使用
* DistributedLockHelper lockHelper = new DistributedLockHelper();
* if (lockHelper.tryLock("order_create_lock", 30)) {
* try {
* // 执行需要加锁的业务逻辑
* createOrder();
* } finally {
* lockHelper.releaseLock("order_create_lock");
* }
* }
*
* // 方式2: 使用自动续期锁
* try (DistributedLockHelper.LockHolder lockHolder =
* lockHelper.tryLockWithAutoRenew("batch_process_lock", 30, 300)) {
* if (lockHolder != null) {
* // 执行长时间业务逻辑
* processBatchData();
* }
* }
* }
* </pre>
*/
@Component
public class DistributedLockHelper {