public static boolean lock(CacheConfigBean cacheConfigBean, String key, Object valueObj, int expireTimeInSecond, long timeOutInSecond) {
        final LockData lockData = LOCK_THREAD_DATA.get();
        if (lockData != null) {
            lockData.getLockCount().incrementAndGet();
            return true;
        }

        long startNano = System.nanoTime();

        // 重试间隔等待时间
        final int retryAwait = (int) timeOutInSecond * 1000 / TRY_NUM;

        int autoIncrementTryNum = 0;
        do {
            autoIncrementTryNum++;

            String result = "nil";
            try (Jedis jedis = Redis.useDataSource(cacheConfigBean).getResource()) {
                if (jedis != null)
                    result = ObjectCacheOperate.set(jedis, key, valueObj, "EX", expireTimeInSecond, "NX");
            } catch (Exception e) {
                LOG.error(e);
            }

            if (!EnvUtil.getEnv().equals(EnvUtil.PRODUCTION))
                LOG.info(String.format("SET %s \"%s\" EX %s NX = result: %s, 第 %s 次尝试", key, valueObj, expireTimeInSecond, result, autoIncrementTryNum));

            if ("OK".equals(result)) {
                LOCK_THREAD_DATA.set(new LockData(key, FormatUtil.formatValue(valueObj)));

                if (!EnvUtil.getEnv().equals(EnvUtil.PRODUCTION)) {
                    long endNano = System.nanoTime();
                    long consumeTime = (endNano - startNano) / 1000000;
                    LOG.info(String.format("key: %s, 分布式加锁, 尝试次数： %s, 累计耗时: %s 纳秒", key, autoIncrementTryNum, consumeTime));
                }

                return true;
            }

            if (!EnvUtil.getEnv().equals(EnvUtil.PRODUCTION))
                LOG.info(String.format("key: %s, 分布式加锁, 目前尝试次数: %s, 暂停: %s 毫秒后继续尝试", key, autoIncrementTryNum, retryAwait));

            LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(retryAwait));

        } while (autoIncrementTryNum < TRY_NUM);

        return false;
    }
public static boolean lock ( CacheConfigBean cacheConfigBean , String key , Object valueObj , int expireTimeInSecond , long timeOutInSecond ) { final LockData lockData = LOCK_THREAD_DATA . get ( ) ; if ( lockData != null ) { lockData . getLockCount ( ) . incrementAndGet ( ) ; return true ; } long startNano = System . nanoTime ( ) ; final int retryAwait = ( int ) timeOutInSecond * 1000 / TRY_NUM ; int autoIncrementTryNum = 0 ; do { autoIncrementTryNum ++ ; String result = "nil" ; try ( Jedis jedis = Redis . useDataSource ( cacheConfigBean ) . getResource ( ) ) { if ( jedis != null ) result = ObjectCacheOperate . set ( jedis , key , valueObj , "EX" , expireTimeInSecond , "NX" ) ; } catch ( Exception e ) { LOG . error ( e ) ; } if ( ! EnvUtil . getEnv ( ) . equals ( EnvUtil . PRODUCTION ) ) LOG . info ( String . format ( "SET %s \"%s\" EX %s NX = result: %s, 第 %s 次尝试" , key , valueObj , expireTimeInSecond , result , autoIncrementTryNum ) ) ; if ( "OK" . equals ( result ) ) { LOCK_THREAD_DATA . set ( new LockData ( key , FormatUtil . formatValue ( valueObj ) ) ) ; if ( ! EnvUtil . getEnv ( ) . equals ( EnvUtil . PRODUCTION ) ) { long endNano = System . nanoTime ( ) ; long consumeTime = ( endNano - startNano ) / 1000000 ; LOG . info ( String . format ( "key: %s, 分布式加锁, 尝试次数： %s, 累计耗时: %s 纳秒" , key , autoIncrementTryNum , consumeTime ) ) ; } return true ; } if ( ! EnvUtil . getEnv ( ) . equals ( EnvUtil . PRODUCTION ) ) LOG . info ( String . format ( "key: %s, 分布式加锁, 目前尝试次数: %s, 暂停: %s 毫秒后继续尝试" , key , autoIncrementTryNum , retryAwait ) ) ; LockSupport . parkNanos ( TimeUnit . MILLISECONDS . toNanos ( retryAwait ) ) ; } while ( autoIncrementTryNum < TRY_NUM ) ; return false ; }
加锁

@param cacheConfigBean
@param key
@param valueObj
@param expireTimeInSecond
@param timeOutInSecond
@return