添加分布式部署定时任务

This commit is contained in:
梁琦涛
2023-04-11 16:32:00 +08:00
parent ccec2afad1
commit 3619d8de54
5 changed files with 237 additions and 0 deletions
@@ -1,10 +1,12 @@
package com.jero.common.util;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
@@ -609,4 +611,37 @@ public class RedisUtil {
e.printStackTrace();
}
}
/**
* setnx
*/
public boolean setnx(String key, String value) {
try {
return redisTemplate.opsForValue().setIfAbsent(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* setnx
*/
public boolean setnx(String key, String value,long time) {
try {
return redisTemplate.opsForValue().setIfAbsent(key,value, Duration.ofSeconds(time));
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* eval
*/
public Boolean eval(RedisScript<Long> luaScripts, List<String> keys, String values) {
Long flag = redisTemplate.execute(luaScripts, keys, values);
//判断是不是为1
return flag == 1L;
}
}