beta,gamma的Guava缓存实体类增加init方法,读取nacos配置时间实现控制缓存时长

人工交互启动类增加方法生成beta,gamma的Gauva缓存实体初始化
增加DurationProperties实体类接收读取nacos配置缓存时间信息
This commit is contained in:
qiaoqinzheng 2023-12-14 10:48:30 +08:00
parent 01b6f4e9bf
commit c65d5bc9ae
4 changed files with 75 additions and 29 deletions

View File

@ -0,0 +1,16 @@
package org.jeecg.common.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.io.Serializable;
@Data
@Component
@ConfigurationProperties(prefix = "duration")
public class DurationProperties implements Serializable {
private Integer cache;
}

View File

@ -2,29 +2,36 @@ package org.jeecg.common.cache;
import com.google.common.cache.Cache; import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
import org.jeecg.common.properties.DurationProperties;
import org.jeecg.modules.entity.vo.BetaDataFile; import org.jeecg.modules.entity.vo.BetaDataFile;
import org.jeecg.modules.entity.vo.PHDFile; import org.springframework.beans.factory.annotation.Autowired;
import org.jeecg.modules.entity.vo.SeriseData;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@Component @Component
public class BetaCache { public class BetaCache {
private Cache<String, BetaDataFile> betaCache = CacheBuilder.newBuilder()
//设置缓存初始大小应该合理设置后续会扩容 @Autowired
.initialCapacity(10) private DurationProperties durationProperties;
//最大值
.maximumSize(100) private Cache<String, BetaDataFile> betaCache;
//并发数设置
.concurrencyLevel(5) public void initCache() {
//缓存过期时间写入后5秒钟过期 betaCache = CacheBuilder.newBuilder()
.expireAfterWrite(24, TimeUnit.HOURS) //设置缓存初始大小应该合理设置后续会扩容
//统计缓存命中率 .initialCapacity(10)
.recordStats() //最大值
.build(); .maximumSize(100)
//并发数设置
.concurrencyLevel(5)
//缓存过期时间写入后XX小时后过期
.expireAfterWrite(durationProperties.getCache(), TimeUnit.HOURS)
//统计缓存命中率
.recordStats()
.build();
}
public Cache<String, BetaDataFile> getBetaCache() { public Cache<String, BetaDataFile> getBetaCache() {
return betaCache; return betaCache;

View File

@ -3,25 +3,36 @@ package org.jeecg.common.cache;
import com.google.common.cache.Cache; import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
import org.jeecg.modules.entity.vo.PHDFile; import org.jeecg.modules.entity.vo.PHDFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Map; import java.util.Objects;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@Component @Component
public class LocalCache { public class LocalCache {
private Cache<String, PHDFile> phdCache = CacheBuilder.newBuilder()
//设置缓存初始大小应该合理设置后续会扩容 @Value("${duration.cache}")
.initialCapacity(10) private Long duration;
//最大值
.maximumSize(100) private Cache<String, PHDFile> phdCache;
//并发数设置
.concurrencyLevel(5) public void initCache() {
//缓存过期时间写入后5秒钟过期 phdCache = CacheBuilder.newBuilder()
.expireAfterWrite(24, TimeUnit.HOURS) //设置缓存初始大小应该合理设置后续会扩容
//统计缓存命中率 .initialCapacity(10)
.recordStats() //最大值
.build(); .maximumSize(100)
//并发数设置
.concurrencyLevel(5)
//缓存过期时间写入后XX小时后过期
.expireAfterWrite(duration, TimeUnit.HOURS)
//统计缓存命中率
.recordStats()
.build();
}
public Cache<String, PHDFile> getPHDCache() { public Cache<String, PHDFile> getPHDCache() {
return phdCache; return phdCache;

View File

@ -1,7 +1,10 @@
package org.jeecg; package org.jeecg;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.cache.BetaCache;
import org.jeecg.common.cache.LocalCache;
import org.jeecg.common.util.oConvertUtils; import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@ -20,6 +23,12 @@ import java.net.UnknownHostException;
@EnableFeignClients(basePackages = {"org.jeecg"}) @EnableFeignClients(basePackages = {"org.jeecg"})
@EnableScheduling @EnableScheduling
public class JeecgSpectrumAnalysisApplication extends SpringBootServletInitializer implements CommandLineRunner { public class JeecgSpectrumAnalysisApplication extends SpringBootServletInitializer implements CommandLineRunner {
@Autowired
private LocalCache localCache;
@Autowired
private BetaCache betaCache;
@Override @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(JeecgSpectrumAnalysisApplication.class); return application.sources(JeecgSpectrumAnalysisApplication.class);
@ -44,5 +53,8 @@ public class JeecgSpectrumAnalysisApplication extends SpringBootServletInitializ
//加载dll工具库 //加载dll工具库
System.loadLibrary("ReadPHDFile"); System.loadLibrary("ReadPHDFile");
System.loadLibrary("GammaAnaly"); System.loadLibrary("GammaAnaly");
//创建缓存
betaCache.initCache();
localCache.initCache();
} }
} }