From 9d3afe6ccb9cabe5d0f8ce86dfbf81a82f989962 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 27 三月 2024 11:00:20 +0800
Subject: [PATCH] 添加缓存设置

---
 src/main/java/com/yssh/utils/CacheUtils.java              |   48 +++++++++++++++++++++++-
 src/main/resources/mapper/WeatherMapper.xml               |   11 +++++
 src/main/resources/application-test.yml                   |    8 ++-
 src/main/java/com/yssh/controller/LocationController.java |   11 ++++-
 src/main/java/com/yssh/controller/WeatherController.java  |   24 +++++++++--
 5 files changed, 89 insertions(+), 13 deletions(-)

diff --git a/src/main/java/com/yssh/controller/LocationController.java b/src/main/java/com/yssh/controller/LocationController.java
index 833ff01..ba91b0f 100644
--- a/src/main/java/com/yssh/controller/LocationController.java
+++ b/src/main/java/com/yssh/controller/LocationController.java
@@ -1,5 +1,6 @@
 package com.yssh.controller;
 
+import com.yssh.utils.CacheUtils;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 
@@ -48,10 +49,16 @@
     @GetMapping("/list")
     @ApiOperation(value = "鏌ヨ鎵�鏈夌偣浣嶆暟鎹�", notes = "鏌ヨ鎵�鏈夊巶鍖虹儹鐐圭偣浣嶆暟鎹�")
     public Result list() {
-        List<Location> list = locationService.getAll();
+        //List<Location> list = locationService.getAll();
+        String key = "locationService.getAll";
+        List<Location> list = CacheUtils.getListByKey(key);
+        if (null == list) {
+            list = locationService.getAll();
+            CacheUtils.putListByKey(key, list);
+        }
+
         return Result.OK(list);
     }
-
 
     @ApiOperation(value = "鏂板鐐逛綅鏁版嵁", notes = "鏂板鐐逛綅璇︽儏鏁版嵁")
     @PostMapping
diff --git a/src/main/java/com/yssh/controller/WeatherController.java b/src/main/java/com/yssh/controller/WeatherController.java
index 71c1476..dd85333 100644
--- a/src/main/java/com/yssh/controller/WeatherController.java
+++ b/src/main/java/com/yssh/controller/WeatherController.java
@@ -1,7 +1,9 @@
 package com.yssh.controller;
 
+import com.yssh.entity.Location;
 import com.yssh.entity.Weather;
 import com.yssh.service.WeatherService;
+import com.yssh.utils.CacheUtils;
 import com.yssh.utils.Result;
 
 import io.swagger.annotations.Api;
@@ -14,6 +16,7 @@
 import javax.annotation.Resource;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
 
@@ -41,7 +44,6 @@
     })
     @GetMapping("/query/{begin}/{end}")
     public Result query(@PathVariable("begin") String begin, @PathVariable("end") String end) {
-        List<Weather> data = new ArrayList<>();
         try {
             if (null != begin && begin.length() != 19) {
                 begin = null;
@@ -49,15 +51,27 @@
             if (null != end && end.length() != 19) {
                 end = null;
             }
-            if (null == begin && null == end) {
-                begin = dateFormat.format(new Date());
+            if (null == begin) {
+                Calendar calendar = Calendar.getInstance();
+                calendar.add(Calendar.DAY_OF_MONTH, -30);
+                begin = dateFormat.format(calendar.getTime());
+            }
+            if (null == end) {
+                end = dateFormat.format(new Date());
             }
 
-            data = weatherService.query(begin, end);
+            //List<Weather> list = weatherService.query(begin, end);
+            String key = CacheUtils.getMd5("weatherService.query_" + begin + "_" + end);
+            List<Weather> list = CacheUtils.getListByKey(key);
+            if (null == list) {
+                list = weatherService.query(begin, end);
+                CacheUtils.putListByKey(key, list);
+            }
+
+            return Result.OK(list);
         } catch (Exception e) {
             return Result.error(e.getMessage());
         }
-        return Result.OK(data);
     }
 
     @GetMapping("/getAll")
diff --git a/src/main/java/com/yssh/utils/CacheUtils.java b/src/main/java/com/yssh/utils/CacheUtils.java
index 10527b1..1292f87 100644
--- a/src/main/java/com/yssh/utils/CacheUtils.java
+++ b/src/main/java/com/yssh/utils/CacheUtils.java
@@ -4,6 +4,9 @@
 import com.github.benmanes.caffeine.cache.Caffeine;
 import org.checkerframework.checker.nullness.qual.NonNull;
 
+import java.math.BigInteger;
+import java.security.MessageDigest;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
 
 public class CacheUtils {
@@ -12,8 +15,8 @@
     public static void init() {
         cache = Caffeine.newBuilder()
                 .initialCapacity(2)
-                .maximumSize(1024)
-                .expireAfterWrite(24, TimeUnit.HOURS)
+                .maximumSize(4096)
+                .expireAfterWrite(24 * 7, TimeUnit.HOURS)
                 .build();
     }
 
@@ -32,4 +35,45 @@
     public static void clear() {
         cache.invalidateAll();
     }
+
+    public static <T> List<T> getListByKey(String key) {
+        Object obj = get(key);
+        if (obj instanceof List<?>) {
+            return (List<T>) obj;
+        }
+
+        return null;
+    }
+
+    public static <T> void putListByKey(String key, List<T> list) {
+        if (null != list && list.size() > 0) {
+            put(key, list);
+        }
+    }
+
+    public static String getMd5(String str) {
+        if (StringUtils.isEmpty(str)) {
+            return null;
+        }
+
+        try {
+            MessageDigest md5 = MessageDigest.getInstance("MD5");
+            md5.update(str.getBytes());
+            byte[] byteArray = md5.digest();
+
+            BigInteger bigInt = new BigInteger(1, byteArray);
+
+            // 鍙傛暟16琛ㄧず16杩涘埗
+            String result = bigInt.toString(16);
+
+            // 涓嶈冻32浣嶉珮浣嶈ˉ闆�
+            while (result.length() < 32) {
+                result = "0" + result;
+            }
+
+            return result;
+        } catch (Exception e) {
+            return null;
+        }
+    }
 }
diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml
index cb60645..38df4aa 100644
--- a/src/main/resources/application-test.yml
+++ b/src/main/resources/application-test.yml
@@ -1,7 +1,7 @@
 # 鐢靛瓙閭欢璁剧疆
 email:
     userName: 252740454
-    password:
+    password: xqyyvhomnvpybgfb
     smtpHost: smtp.qq.com
     smtpPort: 587
     smtpAuth: true
@@ -21,9 +21,11 @@
     datasource:
         type: com.alibaba.druid.pool.DruidDataSource
         driverClassName: com.mysql.cj.jdbc.Driver
-        url: jdbc:mysql://127.0.0.1:3306/yssh?useUnicode=true&rewriteBatchedStatements=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
+        #url: jdbc:mysql://127.0.0.1:3306/yssh?useUnicode=true&rewriteBatchedStatements=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
+        url: jdbc:mysql://192.168.11.206:3306/yssh?useUnicode=true&rewriteBatchedStatements=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
         username: root
-        password: mysql
+        #password: mysql
+        password: 123456
         # 鍒濆杩炴帴鏁�
         initialSize: 10
         # 鏈�灏忚繛鎺ユ睜鏁伴噺
diff --git a/src/main/resources/mapper/WeatherMapper.xml b/src/main/resources/mapper/WeatherMapper.xml
index 3f32373..9eacbe3 100644
--- a/src/main/resources/mapper/WeatherMapper.xml
+++ b/src/main/resources/mapper/WeatherMapper.xml
@@ -9,11 +9,20 @@
         <id column="weather_condition" property="weatherCondition" javaType="java.lang.String" jdbcType="VARCHAR"/>
         <id column="temperature" property="temperature" javaType="java.lang.String" jdbcType="VARCHAR"/>
     </resultMap>
-    
+
     <select id="query" resultMap="WeatherResult">
         select id, time, format(wind_speed, 2) "wind_speed", wind_direction, weather_condition, format(temperature, 2) "temperature"
         from yssh_weather
         <where>
+            time between #{begin} and #{end}
+        </where>
+        order by time;
+    </select>
+    
+    <select id="query2" resultMap="WeatherResult">
+        select id, time, format(wind_speed, 2) "wind_speed", wind_direction, weather_condition, format(temperature, 2) "temperature"
+        from yssh_weather
+        <where>
 	        1 = 1
 	        <if test="begin != null">
                 and str_to_date(time, '%Y-%m-%d-%H:%i:%S') >= #{begin}

--
Gitblit v1.9.3