skyline 2 лет назад
Родитель
Сommit
1824916d8c

+ 19 - 0
common/src/main/java/com/kym/common/annotation/DynamicCache.java

@@ -0,0 +1,19 @@
+package com.kym.common.annotation;
+
+import java.lang.annotation.*;
+
+/**
+ * @author skyline
+ * @description
+ * @date 2023-11-08 22:20
+ */
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface DynamicCache {
+    String value() default "";
+
+    String key() default "";
+
+    long timeout() default 5 * 60 * 1000L;
+}

+ 50 - 0
common/src/main/java/com/kym/common/aspect/DynamicCacheAspect.java

@@ -0,0 +1,50 @@
+package com.kym.common.aspect;
+
+import com.kym.common.annotation.DynamicCache;
+import com.kym.common.cache.DynamicDataCache;
+import com.kym.common.utils.CommUtil;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.springframework.stereotype.Component;
+
+import java.lang.reflect.Method;
+
+/**
+ * @author skyline
+ * @description 数据缓存切面
+ * @date 2023-11-08 22:27
+ */
+@Component
+@Aspect
+public class DynamicCacheAspect {
+    private DynamicDataCache cache = DynamicDataCache.INSTANCE;
+
+    @Pointcut("@annotation(com.kym.common.annotation.DynamicCache)")
+    public void cachePointCut() {
+    }
+
+    @Around("cachePointCut()")
+    Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
+        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
+        Method method = signature.getMethod();
+        DynamicCache dynamicCache = method.getAnnotation(DynamicCache.class);
+        // 获取方法名
+        var methodName = signature.getName();
+        // 获取注解上的缓存时间
+        var timeout = dynamicCache.timeout();
+        // 获取注解上的缓存key
+        var key = dynamicCache.key();
+        var data = cache.get(CommUtil.isEmptyOrNull(key) ? methodName : key);
+        if (data != null) {
+            return data;
+        }
+        // 缓存无数据则执行原方法,缓存新数据
+        Object result = joinPoint.proceed();
+        // 缓存数据
+        cache.put(CommUtil.isEmptyOrNull(key) ? methodName : key, result, timeout >= 0 ? timeout : 5 * 60 * 1000);
+        return result;
+    }
+}

+ 31 - 0
common/src/main/java/com/kym/common/cache/DynamicDataCache.java

@@ -0,0 +1,31 @@
+package com.kym.common.cache;
+
+import cn.hutool.cache.CacheUtil;
+import cn.hutool.cache.impl.TimedCache;
+
+/**
+ * @author skyline
+ * @description 动态数据缓存
+ * @date 2023-11-08 22:02
+ */
+public enum DynamicDataCache {
+    INSTANCE;
+    public static TimedCache<String, Object> timedCache = CacheUtil.newTimedCache(5 * 60 * 1000);
+
+    public void put(String key, Object value) {
+        timedCache.put(key, value);
+    }
+
+    public void put(String key, Object value, Long timeout) {
+        timedCache.put(key, value, timeout);
+    }
+
+    public  Object get(String key) {
+        return timedCache.get(key);
+    }
+
+    public void remove(String key) {
+        timedCache.remove(key);
+    }
+
+}

+ 1 - 0
service/src/main/java/com/kym/service/admin/impl/StationServiceImpl.java

@@ -52,6 +52,7 @@ public class StationServiceImpl extends ServiceImpl<StationMapper, Station> impl
     }
 
     @Override
+    @DynamicCache // 方法的返回结果加一层方法缓存,5分钟内不变
     public List<StationVo> queryStationInfo(int pageNum, int pageSize) {
         var param = """
                 {