🔨 优化锁逻辑.

This commit is contained in:
lijiahangmax
2025-08-23 15:05:03 +08:00
parent 393286d309
commit 3c75aedcec
30 changed files with 443 additions and 131 deletions

View File

@@ -20,7 +20,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.visor.common.interfaces;
package org.dromara.visor.common.cipher;
import cn.orionsec.kit.lang.utils.codec.Base62s;
import cn.orionsec.kit.lang.utils.crypto.symmetric.SymmetricCrypto;

View File

@@ -20,7 +20,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.visor.common.interfaces;
package org.dromara.visor.common.cipher;
/**
* rsa 解密器

View File

@@ -20,7 +20,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.visor.common.interfaces;
package org.dromara.visor.common.file;
import java.io.InputStream;
import java.io.OutputStream;

View File

@@ -1,55 +0,0 @@
/*
* Copyright (c) 2023 - present Dromara, All rights reserved.
*
* https://visor.dromara.org
* https://visor.dromara.org.cn
* https://visor.orionsec.cn
*
* Members:
* Jiahang Li - ljh1553488six@139.com - author
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.visor.common.interfaces;
import java.util.function.Supplier;
/**
* 分布式锁
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024/5/16 12:24
*/
public interface Locker {
/**
* 尝试获取锁
*
* @param key key
* @param run run
* @return 是否获取到锁
*/
boolean tryLock(String key, Runnable run);
/**
* 尝试获取锁
*
* @param key key
* @param call call
* @param <T> T
* @return 执行结果
*/
<T> T tryLock(String key, Supplier<T> call);
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright (c) 2023 - present Dromara, All rights reserved.
*
* https://visor.dromara.org
* https://visor.dromara.org.cn
* https://visor.orionsec.cn
*
* Members:
* Jiahang Li - ljh1553488six@139.com - author
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.visor.common.lock;
import cn.orionsec.kit.lang.able.Executable;
import java.util.function.Supplier;
/**
* 空实现的锁
*
* @author Jiahang Li
* @version 1.0.0
* @since 2025/8/23 13:59
*/
public class EmptyLocker implements Locker {
@Override
public boolean tryLockExecute(String key, Executable executable) {
executable.exec();
return true;
}
@Override
public boolean tryLockExecute(String key, long timeout, Executable executable) {
executable.exec();
return true;
}
@Override
public <T> T tryLockExecute(String key, Supplier<T> callable) {
return callable.get();
}
@Override
public <T> T tryLockExecute(String key, long timeout, Supplier<T> callable) {
return callable.get();
}
@Override
public void lockExecute(String key, Executable executable) {
executable.exec();
}
@Override
public void lockExecute(String key, long timeout, Executable executable) {
executable.exec();
}
@Override
public <T> T lockExecute(String key, Supplier<T> callable) {
return callable.get();
}
@Override
public <T> T lockExecute(String key, long timeout, Supplier<T> callable) {
return callable.get();
}
}

View File

@@ -0,0 +1,116 @@
/*
* Copyright (c) 2023 - present Dromara, All rights reserved.
*
* https://visor.dromara.org
* https://visor.dromara.org.cn
* https://visor.orionsec.cn
*
* Members:
* Jiahang Li - ljh1553488six@139.com - author
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.visor.common.lock;
import cn.orionsec.kit.lang.able.Executable;
import java.util.function.Supplier;
/**
* 分布式锁
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024/5/16 12:24
*/
public interface Locker {
/**
* 尝试获取锁并执行
*
* @param key key
* @param executable exec
* @return 是否获取到锁
*/
boolean tryLockExecute(String key, Executable executable);
/**
* 尝试获取锁并执行
*
* @param key key
* @param timeout timeout
* @param executable exec
* @return 是否获取到锁
*/
boolean tryLockExecute(String key, long timeout, Executable executable);
/**
* 尝试获取锁并执行 未获取到锁则抛出异常
*
* @param key key
* @param callable callable
* @param <T> T
* @return 执行结果
*/
<T> T tryLockExecute(String key, Supplier<T> callable);
/**
* 尝试获取锁并执行 未获取到锁则抛出异常
*
* @param key key
* @param timeout timeout
* @param callable callable
* @param <T> T
* @return 执行结果
*/
<T> T tryLockExecute(String key, long timeout, Supplier<T> callable);
/**
* 阻塞获取锁并执行
*
* @param key key
* @param executable exec
*/
void lockExecute(String key, Executable executable);
/**
* 阻塞获取锁并执行
*
* @param key key
* @param timeout timeout
* @param executable exec
*/
void lockExecute(String key, long timeout, Executable executable);
/**
* 阻塞获取锁并执行
*
* @param key key
* @param callable callable
* @param <T> T
* @return 执行结果
*/
<T> T lockExecute(String key, Supplier<T> callable);
/**
* 阻塞获取锁并执行
*
* @param key key
* @param timeout timeout
* @param callable callable
* @param <T> T
* @return 执行结果
*/
<T> T lockExecute(String key, long timeout, Supplier<T> callable);
}

View File

@@ -23,7 +23,7 @@
package org.dromara.visor.common.utils;
import cn.orionsec.kit.lang.utils.Exceptions;
import org.dromara.visor.common.interfaces.AesEncryptor;
import org.dromara.visor.common.cipher.AesEncryptor;
/**
* aes 数据加密工具类

View File

@@ -22,9 +22,10 @@
*/
package org.dromara.visor.common.utils;
import cn.orionsec.kit.lang.able.Executable;
import cn.orionsec.kit.lang.utils.Exceptions;
import lombok.extern.slf4j.Slf4j;
import org.dromara.visor.common.interfaces.Locker;
import org.dromara.visor.common.lock.Locker;
import java.util.function.Supplier;
@@ -44,26 +45,97 @@ public class LockerUtils {
}
/**
* 尝试获取锁
* 尝试获取锁并执行
*
* @param key key
* @param run run
* @param key key
* @param executable exec
* @return 是否获取到锁
*/
public static boolean tryLock(String key, Runnable run) {
return delegate.tryLock(key, run);
public static boolean tryLockExecute(String key, Executable executable) {
return delegate.tryLockExecute(key, executable);
}
/**
* 尝试获取锁
* 尝试获取锁并执行
*
* @param key key
* @param call call
* @param <T> T
* @param key key
* @param timeout timeout
* @param executable exec
* @return 是否获取到锁
*/
public static boolean tryLockExecute(String key, long timeout, Executable executable) {
return delegate.tryLockExecute(key, timeout, executable);
}
/**
* 尝试获取锁并执行 未获取到锁则抛出异常
*
* @param key key
* @param callable callable
* @param <T> T
* @return 执行结果
*/
public static <T> T tryLock(String key, Supplier<T> call) {
return delegate.tryLock(key, call);
public static <T> T tryLockExecute(String key, Supplier<T> callable) {
return delegate.tryLockExecute(key, callable);
}
/**
* 尝试获取锁并执行 未获取到锁则抛出异常
*
* @param key key
* @param timeout timeout
* @param callable callable
* @param <T> T
* @return 执行结果
*/
public static <T> T tryLockExecute(String key, long timeout, Supplier<T> callable) {
return delegate.tryLockExecute(key, timeout, callable);
}
/**
* 阻塞获取锁并执行
*
* @param key key
* @param executable exec
*/
public static void lockExecute(String key, Executable executable) {
delegate.lockExecute(key, executable);
}
/**
* 阻塞获取锁并执行
*
* @param key key
* @param timeout timeout
* @param executable exec
*/
public static void lockExecute(String key, long timeout, Executable executable) {
delegate.lockExecute(key, timeout, executable);
}
/**
* 阻塞获取锁并执行
*
* @param key key
* @param callable callable
* @param <T> T
* @return 执行结果
*/
public static <T> T lockExecute(String key, Supplier<T> callable) {
return delegate.lockExecute(key, callable);
}
/**
* 阻塞获取锁并执行
*
* @param key key
* @param timeout timeout
* @param callable callable
* @param <T> T
* @return 执行结果
*/
public static <T> T lockExecute(String key, long timeout, Supplier<T> callable) {
return delegate.lockExecute(key, timeout, callable);
}
public static void setDelegate(Locker delegate) {

View File

@@ -23,7 +23,7 @@
package org.dromara.visor.common.utils;
import cn.orionsec.kit.lang.utils.Exceptions;
import org.dromara.visor.common.interfaces.RsaDecryptor;
import org.dromara.visor.common.cipher.RsaDecryptor;
/**
* rsa 参数解密工具类