From c2195c287aa8fbcaf1da6fa7503a3652760f2041 Mon Sep 17 00:00:00 2001 From: Yutousama <583819556@qq.com> Date: Wed, 7 Sep 2022 13:58:31 +0800 Subject: [PATCH] update:holiday in Redis --- src/main/java/com/yutou/tools/utils/CalendarTools.java | 9 ++++++++- src/main/java/com/yutou/tools/utils/RedisTools.java | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/yutou/tools/utils/CalendarTools.java b/src/main/java/com/yutou/tools/utils/CalendarTools.java index c701538..c7a92c4 100644 --- a/src/main/java/com/yutou/tools/utils/CalendarTools.java +++ b/src/main/java/com/yutou/tools/utils/CalendarTools.java @@ -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(); diff --git a/src/main/java/com/yutou/tools/utils/RedisTools.java b/src/main/java/com/yutou/tools/utils/RedisTools.java index 866026f..244c495 100644 --- a/src/main/java/com/yutou/tools/utils/RedisTools.java +++ b/src/main/java/com/yutou/tools/utils/RedisTools.java @@ -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);