mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-08 02:04:04 +08:00
redis锁
This commit is contained in:
parent
81870bd802
commit
08a9f5ab73
@ -30,7 +30,7 @@ public class AppConfigCheck implements ApplicationRunner {
|
||||
setName("Logo");
|
||||
setSort(20);
|
||||
setFieldType(BackendConstant.APP_CONFIG_FIELD_TYPE_IMAGE);
|
||||
setKeyName("system.key");
|
||||
setKeyName("system.logo");
|
||||
setKeyValue("");
|
||||
}},
|
||||
new AppConfig() {{
|
||||
|
@ -20,7 +20,6 @@ public class RedisConfig {
|
||||
redisTemplate.setKeySerializer(stringRedisSerializer);
|
||||
redisTemplate.setHashKeySerializer(stringRedisSerializer);
|
||||
|
||||
// value值采用fastjson2序列化
|
||||
GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
|
||||
redisTemplate.setValueSerializer(jsonRedisSerializer);
|
||||
redisTemplate.setHashValueSerializer(jsonRedisSerializer);
|
||||
|
@ -20,11 +20,11 @@ import java.util.List;
|
||||
@Slf4j
|
||||
public class ExceptionController {
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public JsonResponse exceptionHandler(Exception e) {
|
||||
log.error(e.getMessage());
|
||||
return JsonResponse.error("系统错误", 500);
|
||||
}
|
||||
// @ExceptionHandler(Exception.class)
|
||||
// public JsonResponse exceptionHandler(Exception e) {
|
||||
// log.error(e.getMessage());
|
||||
// return JsonResponse.error("系统错误", 500);
|
||||
// }
|
||||
|
||||
@ExceptionHandler(ServiceException.class)
|
||||
public JsonResponse serviceExceptionHandler(ServiceException e) {
|
||||
|
@ -20,13 +20,13 @@ public class AppConfigController {
|
||||
@Autowired
|
||||
private AppConfigService configService;
|
||||
|
||||
@GetMapping("/")
|
||||
@GetMapping("")
|
||||
public JsonResponse index() {
|
||||
List<AppConfig> configs = configService.allShow();
|
||||
return JsonResponse.data(configs);
|
||||
}
|
||||
|
||||
@PutMapping("/")
|
||||
@PutMapping("")
|
||||
public JsonResponse save(@RequestBody AppConfigRequest req) {
|
||||
configService.saveFromMap(req.getData());
|
||||
return JsonResponse.data(null);
|
||||
|
56
src/main/java/xyz/playedu/api/util/RedisLockUtil.java
Normal file
56
src/main/java/xyz/playedu/api/util/RedisLockUtil.java
Normal file
@ -0,0 +1,56 @@
|
||||
package xyz.playedu.api.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/3/10 14:48
|
||||
*/
|
||||
@Slf4j
|
||||
public class RedisLockUtil {
|
||||
|
||||
public final static String LUA_LOCK_CREATE = """
|
||||
if redis.call("GET", KEYS[1]) == ARGV[1] then
|
||||
redis.call("SET", KEYS[1], ARGV[1], "PX", ARGV[2])
|
||||
return "OK"
|
||||
else
|
||||
return redis.call("SET", KEYS[1], ARGV[1], "NX", "PX", ARGV[2])
|
||||
end
|
||||
""";
|
||||
|
||||
public final static String LUA_LOCK_REMOVE = """
|
||||
if redis.call("GET", KEYS[1]) == ARGV[1] then
|
||||
return redis.call("DEL", KEYS[1])
|
||||
else
|
||||
return 0
|
||||
end""";
|
||||
|
||||
public static boolean lock(String key, String value, Integer expire) {
|
||||
DefaultRedisScript<Object> script = new DefaultRedisScript<>();
|
||||
script.setScriptText(LUA_LOCK_CREATE);
|
||||
// 脚本中的keys
|
||||
List<String> keys = new ArrayList<>();
|
||||
keys.add(key);
|
||||
|
||||
Object result = RedisUtil.handler().execute(script, keys, value, expire);
|
||||
log.info("上锁结果 {}", result);
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean remove(String key, String value) {
|
||||
DefaultRedisScript<String> script = new DefaultRedisScript<>();
|
||||
script.setScriptText(LUA_LOCK_REMOVE);
|
||||
script.setResultType(String.class);
|
||||
// 脚本中的keys
|
||||
List<String> keys = new ArrayList<>();
|
||||
keys.add(key);
|
||||
String result = RedisUtil.handler().execute(script, keys, value);
|
||||
log.info("解锁结果 {}", result);
|
||||
return "OK".equals(result);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user