田源
2025-04-03 9b4433fddf5b401edb0aace8a404ac733b122702
Source/BladeX-Tool/blade-starter-redis/src/main/java/org/springblade/core/redis/lock/RedisLockClient.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,91 @@
/*
 *      Copyright (c) 2018-2028, DreamLu All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: DreamLu å¢æ˜¥æ¢¦ (596392912@qq.com)
 */
package org.springblade.core.redis.lock;
import org.springblade.core.tool.function.CheckedSupplier;
import java.util.concurrent.TimeUnit;
/**
 * é”å®¢æˆ·ç«¯
 *
 * @author L.cm
 */
public interface RedisLockClient {
   /**
    * å°è¯•获取锁
    *
    * @param lockName  é”å
    * @param lockType  é”ç±»åž‹
    * @param waitTime  ç­‰å¾…æ—¶é—´
    * @param leaseTime è‡ªåŠ¨è§£é”æ—¶é—´ï¼Œè‡ªåŠ¨è§£é”æ—¶é—´ä¸€å®šå¾—å¤§äºŽæ–¹æ³•æ‰§è¡Œæ—¶é—´
    * @param timeUnit  æ—¶é—´å‚æ•°
    * @return æ˜¯å¦æˆåŠŸ
    * @throws InterruptedException InterruptedException
    */
   boolean tryLock(String lockName, LockType lockType, long waitTime, long leaseTime, TimeUnit timeUnit) throws InterruptedException;
   /**
    * è§£é”
    *
    * @param lockName é”å
    * @param lockType é”ç±»åž‹
    */
   void unLock(String lockName, LockType lockType);
   /**
    * è‡ªå®šèŽ·å–é”åŽæ‰§è¡Œæ–¹æ³•
    *
    * @param lockName  é”å
    * @param lockType  é”ç±»åž‹
    * @param waitTime  ç­‰å¾…锁超时时间
    * @param leaseTime è‡ªåŠ¨è§£é”æ—¶é—´ï¼Œè‡ªåŠ¨è§£é”æ—¶é—´ä¸€å®šå¾—å¤§äºŽæ–¹æ³•æ‰§è¡Œæ—¶é—´ï¼Œå¦åˆ™ä¼šå¯¼è‡´é”æå‰é‡Šæ”¾ï¼Œé»˜è®¤100
    * @param timeUnit  æ—¶é—´å•位
    * @param supplier  èŽ·å–é”åŽçš„å›žè°ƒ
    * @return è¿”回的数据
    */
   <T> T lock(String lockName, LockType lockType, long waitTime, long leaseTime, TimeUnit timeUnit, CheckedSupplier<T> supplier);
   /**
    * å…¬å¹³é”
    *
    * @param lockName  é”å
    * @param waitTime  ç­‰å¾…锁超时时间
    * @param leaseTime è‡ªåŠ¨è§£é”æ—¶é—´ï¼Œè‡ªåŠ¨è§£é”æ—¶é—´ä¸€å®šå¾—å¤§äºŽæ–¹æ³•æ‰§è¡Œæ—¶é—´ï¼Œå¦åˆ™ä¼šå¯¼è‡´é”æå‰é‡Šæ”¾ï¼Œé»˜è®¤100
    * @param supplier  èŽ·å–é”åŽçš„å›žè°ƒ
    * @return è¿”回的数据
    */
   default <T> T lockFair(String lockName, long waitTime, long leaseTime, CheckedSupplier<T> supplier) {
      return lock(lockName, LockType.FAIR, waitTime, leaseTime, TimeUnit.SECONDS, supplier);
   }
   /**
    * å¯é‡å…¥é”
    *
    * @param lockName  é”å
    * @param waitTime  ç­‰å¾…锁超时时间
    * @param leaseTime è‡ªåŠ¨è§£é”æ—¶é—´ï¼Œè‡ªåŠ¨è§£é”æ—¶é—´ä¸€å®šå¾—å¤§äºŽæ–¹æ³•æ‰§è¡Œæ—¶é—´ï¼Œå¦åˆ™ä¼šå¯¼è‡´é”æå‰é‡Šæ”¾ï¼Œé»˜è®¤100
    * @param supplier  èŽ·å–é”åŽçš„å›žè°ƒ
    * @return è¿”回的数据
    */
   default <T> T lockReentrant(String lockName, long waitTime, long leaseTime, CheckedSupplier<T> supplier) {
      return lock(lockName, LockType.REENTRANT, waitTime, leaseTime, TimeUnit.SECONDS, supplier);
   }
}