This commit is contained in:
Raod 2021-06-23 11:02:48 +08:00
parent 8224781011
commit bac71beec3
8 changed files with 228 additions and 11 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ target
**/*.log
dist
logs
cache

View File

@ -21,7 +21,7 @@
</modules>
<properties>
<gaea.version>1.0.0-SNAPSHOT</gaea.version>
<gaea.version>2.0.0-SNAPSHOT</gaea.version>
<gaea.security.version>1.0.0-SNAPSHOT</gaea.security.version>
<gaea.export.version>1.0.0-SNAPSHOT</gaea.export.version>
<gaea.generator.version>1.0.0-SNAPSHOT</gaea.generator.version>

View File

@ -16,7 +16,7 @@
<dependency>
<groupId>com.anjiplus.template.gaea</groupId>
<artifactId>template-gaea-common</artifactId>
<!-- <exclusions>-->
<exclusions>
<!-- <exclusion>-->
<!-- <groupId>com.alibaba.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>-->
@ -25,7 +25,11 @@
<!-- <groupId>com.alibaba.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
@ -49,6 +53,17 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.6</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.alibaba.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>-->

View File

@ -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<String, String> hashGet(String key) {
Cache.ValueWrapper t = cache.get(key);
if (t != null) {
return (Map<String, String>) t.get();
}
return Maps.newHashMap();
}
@Override
public String hashGetString(String key, String hashKey) {
Map<String, String> stringStringMap = hashGet(key);
return stringStringMap.get(hashKey);
}
@Override
public void hashDel(String key, String hashKey) {
Map<String, String> stringStringMap = hashGet(key);
stringStringMap.remove(hashKey);
}
@Override
public void hashBatchDel(String key, Set<String> hashKeys) {
Map<String, String> stringStringMap = hashGet(key);
hashKeys.forEach(stringStringMap::remove);
}
@Override
public boolean hashExist(String key, String hashKey) {
if (exist(key)) {
Map<String, String> 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<String, String> 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<String, String> hash) {
cache.put(key, hash);
}
@Override
public boolean delete(String key) {
cache.evict(key);
return true;
}
@Override
public boolean delete(List<String> keys) {
keys.forEach(this::delete);
return true;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
/*基于内存的本地缓存*/
cache = (Cache)applicationContext.getBean("ehCacheCache");
}
}

View File

@ -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();
}
}

View File

@ -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
#数据库连接配置

View File

@ -0,0 +1,36 @@
<ehcache>
<!--
磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
path:指定在硬盘上存储对象的路径
path可以配置的目录有
user.home用户的家目录
user.dir用户当前的工作目录
java.io.tmpdir默认的临时目录
ehcache.disk.store.direhcache的配置目录
绝对路径d:\\ehcache
查看路径方法String tmpDir = System.getProperty("java.io.tmpdir");
-->
<diskStore path="./cache" />
<!--
defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理
maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象
eternal:代表对象是否永不过期 (指定true则下面两项配置需为0无限期)
timeToIdleSeconds:最大的发呆时间 /秒
timeToLiveSeconds:最大的存活时间 /秒
overflowToDisk:是否允许对象被写入到磁盘
说明下列配置自缓存建立起600秒(10分钟)有效 。
在有效的600秒(10分钟)内如果连续120秒(2分钟)未访问缓存,则缓存失效。
就算有访问也只会存活600秒。
-->
<!--<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="true" />
-->
<cache name="reportCache" maxElementsInMemory="10000" eternal="true"
maxElementsOnDisk="0" diskPersistent="true"
timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="true" />
<!--<cache name="persistentCacheCfg" maxElementsInMemory="1" eternal="true"
diskPersistent="true" maxElementsOnDisk="0"
timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="true" />-->
</ehcache>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_HOME" value="./logs"/>
<property name="LOG_NAME" value="gaea-business"/>
<property name="LOG_NAME" value="ai-report"/>
<!--控制台-->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
@ -34,4 +34,4 @@
<appender-ref ref="LOGFILE" />
</root>
</configuration>
</configuration>