diff --git a/.gitignore b/.gitignore index ff96ea4d..80a24dc6 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ target **/*.log dist logs +cache diff --git a/pom.xml b/pom.xml index 4dc13d53..798ce214 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ - 1.0.0-SNAPSHOT + 2.0.0-SNAPSHOT 1.0.0-SNAPSHOT 1.0.0-SNAPSHOT 1.0.0-SNAPSHOT diff --git a/report-core/pom.xml b/report-core/pom.xml index 372f3517..9afe842f 100644 --- a/report-core/pom.xml +++ b/report-core/pom.xml @@ -16,7 +16,7 @@ com.anjiplus.template.gaea template-gaea-common - + @@ -25,7 +25,11 @@ - + + org.springframework.boot + spring-boot-starter-data-redis + + @@ -49,6 +53,17 @@ test + + org.springframework.boot + spring-boot-starter-cache + + + + net.sf.ehcache + ehcache + 2.10.6 + + diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/cache/ReportCacheHelper.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/cache/ReportCacheHelper.java new file mode 100644 index 00000000..5010945c --- /dev/null +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/cache/ReportCacheHelper.java @@ -0,0 +1,141 @@ +package com.anjiplus.template.gaea.business.cache; + + +import com.anji.plus.gaea.cache.CacheHelper; +import com.google.common.collect.Maps; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.Cache; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +public class ReportCacheHelper implements CacheHelper, ApplicationContextAware { + + @Autowired + private Cache cache; + + @Override + public String stringGet(String key) { + Cache.ValueWrapper valueWrapper = cache.get(key); + if (valueWrapper != null) { + return (String)valueWrapper.get(); + } + return CacheHelper.super.stringGet(key); + } + + @Override + public Boolean setIfAbsent(String key, String value) { + cache.putIfAbsent(key, value); + return true; + } + + + @Override + public boolean exist(String key) { + return cache.get(key)!=null; + } + + + @Override + public void stringSet(String key, String value) { + cache.put(key, value); + } + + @Override + public void stringSetExpire(String key, String value, long time, TimeUnit timeUnit) { + CacheHelper.super.stringSetExpire(key, value, time, timeUnit); + } + + @Override + public String regKey(String key) { + return CacheHelper.super.regKey(key); + } + + @Override + public void stringSetExpire(String key, String value, long seconds) { + CacheHelper.super.stringSetExpire(key, value, seconds); + } + + @Override + public Map hashGet(String key) { + Cache.ValueWrapper t = cache.get(key); + if (t != null) { + return (Map) t.get(); + } + return Maps.newHashMap(); + } + + @Override + public String hashGetString(String key, String hashKey) { + Map stringStringMap = hashGet(key); + return stringStringMap.get(hashKey); + } + + @Override + public void hashDel(String key, String hashKey) { + Map stringStringMap = hashGet(key); + stringStringMap.remove(hashKey); + } + + @Override + public void hashBatchDel(String key, Set hashKeys) { + Map stringStringMap = hashGet(key); + hashKeys.forEach(stringStringMap::remove); + } + + @Override + public boolean hashExist(String key, String hashKey) { + if (exist(key)) { + Map map = hashGet(key); + return map.containsKey(hashKey); + } + return false; + } + + @Override + public boolean hashAnyExist(String key, String[] hashKeys) { + return CacheHelper.super.hashAnyExist(key, hashKeys); + } + + @Override + public void hashSet(String key, String hashKey, String hashValue) { + Map map; + if (exist(key)) { + map = hashGet(key); + } else { + map = new HashMap<>(); + } + map.put(hashKey, hashValue); + hashSet(key, map); + } + + @Override + public void hashSet(String key, Map hash) { + cache.put(key, hash); + } + + @Override + public boolean delete(String key) { + cache.evict(key); + return true; + } + + @Override + public boolean delete(List keys) { + keys.forEach(this::delete); + return true; + } + + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + /*基于内存的本地缓存*/ + cache = (Cache)applicationContext.getBean("ehCacheCache"); + } +} diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/config/BusinessAutoConfiguration.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/config/BusinessAutoConfiguration.java index 9c6751a1..ce4ade6f 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/config/BusinessAutoConfiguration.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/config/BusinessAutoConfiguration.java @@ -1,8 +1,11 @@ package com.anjiplus.template.gaea.business.config; +import com.anji.plus.gaea.cache.CacheHelper; +import com.anjiplus.template.gaea.business.cache.ReportCacheHelper; import com.anjiplus.template.gaea.business.runner.ApplicationInitRunner; - import org.mybatis.spring.annotation.MapperScan; +import org.springframework.cache.ehcache.EhCacheCache; +import org.springframework.cache.ehcache.EhCacheCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -26,4 +29,23 @@ public class BusinessAutoConfiguration { public ApplicationInitRunner applicationInitRunner() { return new ApplicationInitRunner(); } + + @Bean("gaeaCacheHelper") + public CacheHelper gaeaCacheHelper(){ + return new ReportCacheHelper(); + } + + @Bean + public EhCacheCache ehCacheCache() { + return (EhCacheCache) ehCacheCacheManager().getCache("reportCache"); + } + + /** + * 创建ehCacheCacheManager + */ + @Bean + public EhCacheCacheManager ehCacheCacheManager() { + + return new EhCacheCacheManager(); + } } diff --git a/report-core/src/main/resources/bootstrap-dev.yml b/report-core/src/main/resources/bootstrap-dev.yml index c5650e52..e606b533 100644 --- a/report-core/src/main/resources/bootstrap-dev.yml +++ b/report-core/src/main/resources/bootstrap-dev.yml @@ -19,11 +19,13 @@ spring: url: jdbc:mysql://10.108.26.197:3306/aj_report?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false username: root password: appuser@anji - redis: - host: 10.108.26.197 - port: 6379 - password: appuser@anji - database: 1 +# 禁用redis,使用ehcache +# redis: +# host: 10.108.26.197 +# port: 6379 +# password: appuser@anji +# database: 1 + flyway: baseline-on-migrate: true #数据库连接配置 diff --git a/report-core/src/main/resources/ehcache.xml b/report-core/src/main/resources/ehcache.xml new file mode 100644 index 00000000..2d8bb8d9 --- /dev/null +++ b/report-core/src/main/resources/ehcache.xml @@ -0,0 +1,36 @@ + + + + + + + + + + diff --git a/report-core/src/main/resources/logback.xml b/report-core/src/main/resources/logback.xml index b6752c24..b3488525 100644 --- a/report-core/src/main/resources/logback.xml +++ b/report-core/src/main/resources/logback.xml @@ -1,7 +1,7 @@ - + @@ -34,4 +34,4 @@ - \ No newline at end of file +