update:holiday in Redis

This commit is contained in:
Yutousama 2022-09-07 13:58:31 +08:00
parent b653c52669
commit c2195c287a
2 changed files with 17 additions and 1 deletions

View File

@ -12,6 +12,7 @@ import java.util.*;
@SuppressWarnings("MagicConstant")
public class CalendarTools {
public static final String REDIS_TAG = "Calendar";
public static final String REDIS_HOLIDAY = "holiday";
public static JSONObject getHoliday() {
int year = Calendar.getInstance(DateFormat.getDateInstance().getTimeZone(), Locale.CHINA).get(Calendar.YEAR);
@ -20,7 +21,13 @@ public class CalendarTools {
@SneakyThrows
public static JSONObject getHoliday(String year) {
String get = HttpTools.get(String.format("https://timor.tech/api/holiday/year/%s", year));
String get;
if(RedisTools.exists(REDIS_HOLIDAY)){
get=RedisTools.get(REDIS_HOLIDAY);
}else {
get = HttpTools.get(String.format("https://timor.tech/api/holiday/year/%s", year));
RedisTools.set(REDIS_HOLIDAY,get);
}
JSONObject json = JSONObject.parseObject(get);
JSONObject date = new JSONObject();
JSONArray dateHoliday = new JSONArray();

View File

@ -135,6 +135,15 @@ public class RedisTools {
jedis.close();
return flag;
}
public static boolean exists(String key){
if(isNotInstallRedis){
return false;
}
Jedis jedis=getRedis();
boolean flag=jedis.exists(key);
jedis.close();
return flag;
}
public static Jedis getRedis() {
return new Jedis(host, port);