From 5fc6392ea5c1e6b74751d8cb9ebabe10f18c6002 Mon Sep 17 00:00:00 2001
From: whycxzp <glperry@163.com>
Date: 星期三, 07 五月 2025 11:24:46 +0800
Subject: [PATCH] 本地缓存增加

---
 /dev/null                                             |   89 -----------------------------
 src/main/java/com/whyc/config/CacheConfig.java        |    9 +++
 src/main/java/com/whyc/service/UserService.java       |    7 ++
 src/main/resources/config/application-dev.yml         |    5 +
 src/main/java/com/whyc/service/UserBridgeService.java |    3 
 pom.xml                                               |   10 +--
 src/main/resources/config/application-prod.yml        |    4 +
 7 files changed, 30 insertions(+), 97 deletions(-)

diff --git a/pom.xml b/pom.xml
index 2c85b3c..6b9d4d7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -51,12 +51,6 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-aop</artifactId>
         </dependency>
-        <!--highgo鏁版嵁搴撹繛鎺ユ睜 瀹夊叏鐗圴4.5鐗堟湰鍙婁紒涓氱増V6鍏煎-->
-        <dependency>
-            <groupId>com.highgo</groupId>
-            <artifactId>HgdbJdbc</artifactId>
-            <version>6.2.3</version>
-        </dependency>
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
@@ -136,6 +130,10 @@
             <version>3.0.4</version>
         </dependency>
         <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-cache</artifactId>
+        </dependency>
+        <dependency>
             <groupId>com.github.ben-manes.caffeine</groupId>
             <artifactId>caffeine</artifactId>
         </dependency>
diff --git a/src/main/java/com/whyc/config/CacheConfig.java b/src/main/java/com/whyc/config/CacheConfig.java
new file mode 100644
index 0000000..8b71a70
--- /dev/null
+++ b/src/main/java/com/whyc/config/CacheConfig.java
@@ -0,0 +1,9 @@
+package com.whyc.config;
+
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+@EnableCaching
+public class CacheConfig {
+}
diff --git a/src/main/java/com/whyc/config/CaffeineConfig.java b/src/main/java/com/whyc/config/CaffeineConfig.java
deleted file mode 100644
index b785d8e..0000000
--- a/src/main/java/com/whyc/config/CaffeineConfig.java
+++ /dev/null
@@ -1,89 +0,0 @@
-package com.whyc.config;
-
-import com.github.benmanes.caffeine.cache.Caffeine;
-import org.springframework.cache.CacheManager;
-import org.springframework.cache.caffeine.CaffeineCache;
-import org.springframework.cache.support.SimpleCacheManager;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.context.annotation.Primary;
-
-import java.util.ArrayList;
-
-/**
- * Caffeine閰嶇疆
- */
-@Configuration
-public class CaffeineConfig {
-
-    public static enum Caches{
-        defaultCache(500),
-        defaultCache2Exp(500,24)
-        ;
-
-        Caches() {
-        }
-
-        Caches(int maxSize) {
-            this.maxSize = maxSize;
-        }
-
-        Caches(int maxSize, int ttl) {
-            this.maxSize = maxSize;
-            this.ttl = ttl;
-        }
-
-        private int maxSize;
-        private int ttl;
-
-        public int getMaxSize() {
-            return maxSize;
-        }
-
-        public void setMaxSize(int maxSize) {
-            this.maxSize = maxSize;
-        }
-
-        public int getTtl() {
-            return ttl;
-        }
-
-        public void setTtl(int ttl) {
-            this.ttl = ttl;
-        }
-    }
-
-    @Bean
-    @Primary
-    public CacheManager caffeineCacheManager() {
-        SimpleCacheManager cacheManager = new SimpleCacheManager();
-        ArrayList<CaffeineCache> caffeineCaches = new ArrayList<>();
-
-        for (Caches cache : Caches.values()) {
-            caffeineCaches.add(new CaffeineCache(cache.name(),
-                    Caffeine.newBuilder().recordStats()
-                    .maximumSize(cache.getMaxSize())
-                    .build()
-            ));
-        }
-        cacheManager.setCaches(caffeineCaches);
-        return cacheManager;
-
-    }
-
-    /*@Bean(value = "defaultCache")
-    public Cache<String,Object> defaultCache(){
-        return Caffeine.newBuilder().recordStats()
-                        .maximumSize(Caches.defaultCache.getMaxSize())
-                        .build();
-    }
-
-    @Bean(value = "defaultCache2Exp")
-    public Cache<String,Object> defaultCache2Exp(){
-        return Caffeine.newBuilder().recordStats()
-                        .maximumSize(Caches.defaultCache2Exp.getMaxSize())
-                        .expireAfterWrite(Caches.defaultCache2Exp.ttl, TimeUnit.HOURS)
-                        .build();
-    }*/
-
-}
diff --git a/src/main/java/com/whyc/service/UserBridgeService.java b/src/main/java/com/whyc/service/UserBridgeService.java
index c0ddeea..3e275e1 100644
--- a/src/main/java/com/whyc/service/UserBridgeService.java
+++ b/src/main/java/com/whyc/service/UserBridgeService.java
@@ -19,14 +19,13 @@
 @Service
 //Unified Naming
 //@CacheConfig(cacheNames ={"userBridge"})
-@DependsOn("caffeineCacheManager")
 public class UserBridgeService {
 
     @Resource
     private UserMapper userMapper;
 
     @Resource
-    private CacheManager caffeineCacheManager;
+    private CacheManager cacheManager;
 
     public User findPasswordByUserName(String name) {
         User user = null;
diff --git a/src/main/java/com/whyc/service/UserService.java b/src/main/java/com/whyc/service/UserService.java
index 5444641..05b42a4 100644
--- a/src/main/java/com/whyc/service/UserService.java
+++ b/src/main/java/com/whyc/service/UserService.java
@@ -11,6 +11,9 @@
 import com.whyc.util.RSAUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.CachePut;
+import org.springframework.cache.annotation.Cacheable;
 import org.springframework.context.ApplicationContext;
 import org.springframework.stereotype.Service;
 
@@ -61,6 +64,8 @@
             return new Response<>().set(0);
         }
     }
+
+    @CacheEvict(value = "userPage",allEntries = true)
     public Response addByRSA(User user) {
         User userFound = userBridgeService.findPasswordByUserName(user.getName());
         if (userFound.getId() != 0) {
@@ -124,6 +129,7 @@
         return users;
     }
 
+    @Cacheable(value = "userPage")
     public Response getPage(int pageNum,int pageSize) {
         PageHelper.startPage(pageNum,pageSize);
         List<User> list = userMapper.selectList((Wrapper<User>) CommonUtil.objectNull);
@@ -134,6 +140,7 @@
         return new Response().set(1,pageInfo,"鏌ヨ鎴愬姛");
     }
 
+    @CacheEvict(value = "userPage",allEntries = true)
     public Response update(User user) {
         User userFound = userBridgeService.findPasswordByUserName(user.getName());
         User userOfPhoneNumber = userBridgeService.getUserByPhoneNumber(user.getPhoneNumber());
diff --git a/src/main/resources/config/application-dev.yml b/src/main/resources/config/application-dev.yml
index 4ff2b84..b8437d2 100644
--- a/src/main/resources/config/application-dev.yml
+++ b/src/main/resources/config/application-dev.yml
@@ -43,6 +43,11 @@
     username: whyc_doc@163.com
     password: JAMKMEQUOAGGMSVV
     default-encoding: UTF-8
+  cache:
+    type: caffeine
+    caffeine:
+      spec: maximumSize=100,expireAfterWrite=30m
+
 jasypt:
   encryptor:
     #password: 9Lu6HgEvttjj8vYhy3ID+PqPbumuXhcH
diff --git a/src/main/resources/config/application-prod.yml b/src/main/resources/config/application-prod.yml
index a6d1bc9..1cf7cca 100644
--- a/src/main/resources/config/application-prod.yml
+++ b/src/main/resources/config/application-prod.yml
@@ -40,6 +40,10 @@
     username: whyc_doc@163.com
     password: JAMKMEQUOAGGMSVV
     default-encoding: UTF-8
+  cache:
+    type: caffeine
+    caffeine:
+      spec: maximumSize=100,expireAfterWrite=30m
 jasypt:
   encryptor:
     algorithm: PBEWithMD5AndDES

--
Gitblit v1.9.1