燕山石化溯源三维电子沙盘-【后端】-服务
13693261870
2024-03-27 9d3afe6ccb9cabe5d0f8ce86dfbf81a82f989962
添加缓存设置
已修改5个文件
102 ■■■■ 文件已修改
src/main/java/com/yssh/controller/LocationController.java 11 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/yssh/controller/WeatherController.java 24 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/yssh/utils/CacheUtils.java 48 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application-test.yml 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/mapper/WeatherMapper.xml 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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
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")
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;
        }
    }
}
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
        # 最小连接池数量
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}