This commit is contained in:
none 2023-03-10 15:49:12 +08:00
parent 81870bd802
commit 08a9f5ab73
5 changed files with 64 additions and 9 deletions

View File

@ -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() {{

View File

@ -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);

View File

@ -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) {

View File

@ -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);

View 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);
}
}