Browse Source

加上缓存

yufeng 3 years ago
parent
commit
8669af5265

+ 13 - 0
pom.xml

@@ -528,6 +528,12 @@
528 528
             <groupId>com.alibaba</groupId>
529 529
             <artifactId>easyexcel</artifactId>
530 530
             <version>2.1.6</version>
531
+            <exclusions>
532
+		        <exclusion>
533
+		            <groupId>org.ehcache</groupId>
534
+		            <artifactId>ehcache</artifactId>
535
+		        </exclusion>
536
+		    </exclusions>
531 537
         </dependency>
532 538
         
533 539
         <dependency>
@@ -535,6 +541,13 @@
535 541
 		    <artifactId>jakarta.validation-api</artifactId>
536 542
 		    <version>2.0.2</version>
537 543
 		</dependency>
544
+		
545
+		<!-- ehcache缓存 -->
546
+		<dependency>
547
+			<groupId>net.sf.ehcache</groupId>
548
+			<artifactId>ehcache</artifactId>
549
+			<version>2.10.6</version>
550
+		</dependency>
538 551
 
539 552
     </dependencies>
540 553
 

+ 47 - 0
src/main/java/com/ylcm/sys/config/CacheConfig.java

@@ -0,0 +1,47 @@
1
+package com.ylcm.sys.config;
2
+
3
+import java.lang.reflect.Method;
4
+import java.util.LinkedHashMap;
5
+import java.util.Map;
6
+
7
+import org.springframework.cache.annotation.EnableCaching;
8
+import org.springframework.cache.ehcache.EhCacheCacheManager;
9
+import org.springframework.cache.interceptor.KeyGenerator;
10
+import org.springframework.context.annotation.Bean;
11
+import org.springframework.context.annotation.Configuration;
12
+import org.springframework.util.DigestUtils;
13
+
14
+import com.alibaba.fastjson.JSONObject;
15
+
16
+@Configuration
17
+@EnableCaching
18
+public class CacheConfig {
19
+
20
+	@Bean("cacheManager")
21
+	public EhCacheCacheManager EhcacheManager() {
22
+    	EhCacheCacheManager ehCacheManager = new EhCacheCacheManager();
23
+    	return ehCacheManager;
24
+    }
25
+	
26
+	@Bean
27
+    public KeyGenerator cacheKeyGenerator(){
28
+        CacheKeyGenerator cacheKeyGenerator = new CacheKeyGenerator();
29
+        return cacheKeyGenerator;
30
+
31
+    }
32
+    public class CacheKeyGenerator implements KeyGenerator {
33
+        @Override
34
+        public Object generate(Object target, Method method, Object... params) {
35
+            Map<String, Object> map = new LinkedHashMap<>();
36
+            if (params != null && params.length > 0) {
37
+                int i = 0;
38
+                for (Object o : params) {
39
+                    map.put("params-" + i, o);
40
+                    i++;
41
+                }
42
+            }
43
+            String str = JSONObject.toJSON(map).toString();
44
+            return method.getName() + "-" + DigestUtils.md5DigestAsHex(str.getBytes());
45
+        }
46
+    }
47
+}

+ 2 - 0
src/main/java/com/ylcm/sys/service/impl/SpecialAnalysisServiceImpl.java

@@ -4,6 +4,7 @@ import java.util.List;
4 4
 
5 5
 import javax.annotation.Resource;
6 6
 
7
+import org.springframework.cache.annotation.Cacheable;
7 8
 import org.springframework.stereotype.Service;
8 9
 
9 10
 import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -26,6 +27,7 @@ public class SpecialAnalysisServiceImpl implements SpecialAnalysisService {
26 27
 		return adSpecialMapper.getPage(new Page<AdSpecial>(queryForm.getPageNo(), queryForm.getPageSize()), queryForm);
27 28
 	}
28 29
 
30
+	@Cacheable(value = "SpecialAnalysis",keyGenerator = "cacheKeyGenerator")
29 31
 	@Override
30 32
 	public List<AdSpecialTopVO> specialTop(Integer type, String startDate, String endDate) {
31 33
 		return adSpecialMapper.specialTop(type, startDate, endDate);

+ 1 - 0
src/main/resource/applicationContext-beans.xml

@@ -26,6 +26,7 @@
26 26
 		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
27 27
 	</context:component-scan>
28 28
 	<context:component-scan base-package="com.ylcm.sys.task" />
29
+	<context:component-scan base-package="com.ylcm.sys.config" />
29 30
 	
30 31
 	<!-- 定时任务 -->
31 32
 	<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  

+ 4 - 0
src/main/resource/applicationContext-plugin.xml

@@ -2,15 +2,19 @@
2 2
 <beans xmlns="http://www.springframework.org/schema/beans"
3 3
 	xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 4
 	xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"
5
+	xmlns:cache="http://www.springframework.org/schema/cache"
5 6
 	xsi:schemaLocation="
6 7
 		http://www.springframework.org/schema/beans
7 8
 		http://www.springframework.org/schema/beans/spring-beans.xsd
8 9
 		http://activemq.apache.org/schema/core
9 10
 		http://activemq.apache.org/schema/core/activemq-core.xsd
11
+		http://www.springframework.org/schema/cache
12
+        http://www.springframework.org/schema/cache/spring-cache.xsd
10 13
 		http://cxf.apache.org/jaxws
11 14
 		http://cxf.apache.org/schemas/jaxws.xsd
12 15
 		http://cxf.apache.org/core
13 16
 		http://cxf.apache.org/schemas/core.xsd">
14 17
 
18
+	<cache:annotation-driven cache-manager="cacheManager" />  
15 19
 
16 20
 </beans>

+ 45 - 0
src/main/resource/ehcache.xml

@@ -0,0 +1,45 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
+    xsi:noNamespaceSchemaLocation="ehcache.xsd">
4
+    <!--timeToIdleSeconds 当缓存闲置n秒后销毁 -->
5
+    <!--timeToLiveSeconds 当缓存存活n秒后销毁 -->
6
+    <!-- 缓存配置 
7
+        name:缓存名称。 
8
+        maxElementsInMemory:缓存最大个数。 
9
+        eternal:对象是否永久有效,一但设置了,timeout将不起作用。 
10
+        timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。 
11
+        timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。 
12
+        overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。 diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。 
13
+        maxElementsOnDisk:硬盘最大缓存个数。 
14
+        diskPersistent:是否缓存虚拟机重启期数据 Whether the disk 
15
+        store persists between restarts of the Virtual Machine. The default value 
16
+        is false. 
17
+        diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。  memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是 
18
+LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。 
19
+        clearOnFlush:内存数量最大时是否清除。 -->
20
+    <!-- 磁盘缓存位置 -->
21
+    <diskStore path="java.io.tmpdir" />
22
+    <!-- 默认缓存 -->
23
+    <defaultCache 
24
+        maxElementsInMemory="10000" 
25
+        eternal="false"
26
+        timeToIdleSeconds="120" 
27
+        timeToLiveSeconds="120" 
28
+        maxElementsOnDisk="10000000"
29
+        diskExpiryThreadIntervalSeconds="120" 
30
+        memoryStoreEvictionPolicy="LRU">
31
+        <persistence strategy="localTempSwap" />
32
+    </defaultCache>
33
+
34
+    <!-- 指定cache,即对应cacheName的值 -->
35
+    <cache name="SpecialAnalysis" 
36
+        eternal="false" 
37
+        timeToIdleSeconds="2400"
38
+        timeToLiveSeconds="3600" 
39
+        maxEntriesLocalHeap="10000"
40
+        maxEntriesLocalDisk="10000000" 
41
+        diskExpiryThreadIntervalSeconds="120"
42
+        overflowToDisk="false" 
43
+        memoryStoreEvictionPolicy="LRU">
44
+    </cache>
45
+</ehcache>

+ 2 - 0
src/main/resource/env/dev/application.properties

@@ -16,3 +16,5 @@ initSize=10
16 16
 maxSize=100
17 17
 psCacheSize=30
18 18
 
19
+spring.cache.ehcache.config=ehcache.xml
20
+

+ 3 - 1
src/main/resource/env/prod/application.properties

@@ -17,4 +17,6 @@ jdbc.readonly.password=WkmYZxsVtF8nqWVYAH4J6zeQAnjO5nsZgFa+T4kljBrIRcec1/gMgVjs+
17 17
 #连接
18 18
 initSize=10
19 19
 maxSize=100
20
-psCacheSize=30
20
+psCacheSize=30
21
+
22
+spring.cache.ehcache.config=ehcache.xml

+ 3 - 1
src/main/resource/env/qa/application.properties

@@ -14,4 +14,6 @@ jdbc.readonly.password=Biyu5YzU+6sxDRbmWEa3B2uUcImzDo0BuXjTlL505+/pTb+/0Oqd3ou1R
14 14
 #连接
15 15
 initSize=10
16 16
 maxSize=100
17
-psCacheSize=30
17
+psCacheSize=30
18
+
19
+spring.cache.ehcache.config=ehcache.xml