12 changed files with 511 additions and 356 deletions
@ -0,0 +1,59 @@
|
||||
package com.mh.user.config; |
||||
|
||||
import com.github.benmanes.caffeine.cache.*; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
/** |
||||
* @author LJF |
||||
* @title : |
||||
* @description 使用caffeine缓存技术 |
||||
* @updateTime 2020-12-15 |
||||
* @throws : |
||||
*/ |
||||
@Configuration |
||||
public class CaffeineCacheConfig { |
||||
|
||||
// 软引用: 如果一个对象只具有软引用,则内存空间足够,垃圾回收器就不会回收它;如果内存空间不足了,就会回收这些对象的内存。
|
||||
// 弱引用: 弱引用的对象拥有更短暂的生命周期。在垃圾回收器线程扫描它所管辖的内存区域的过程中,一旦发现了只具有弱引用的对象,
|
||||
// 不管当前内存空间足够与否,都会回收它的内存
|
||||
@Bean(name = "caffeineCache") |
||||
public Cache<String, Object> caffeineCache() { |
||||
return Caffeine.newBuilder() |
||||
// 软引用
|
||||
.softValues() |
||||
// 弱引用
|
||||
// .weakValues()
|
||||
// 设置最后一次写入或访问后经过固定时间过期
|
||||
.expireAfterWrite(8*60*60, TimeUnit.SECONDS) |
||||
// 初始的缓存空间大小
|
||||
.initialCapacity(100) |
||||
// 缓存的最大条数
|
||||
.maximumSize(1000) |
||||
.build(); |
||||
} |
||||
|
||||
//定义缓存,可直接使用
|
||||
@Bean |
||||
public LoadingCache expiryCache(){ |
||||
LoadingCache<String, Object> loadingCache = Caffeine.newBuilder() |
||||
.initialCapacity(100) |
||||
.maximumSize(1000) |
||||
//缓存写入/删除监控
|
||||
.writer(new CacheWriter<Object, Object>() { |
||||
@Override |
||||
public void write(Object key, Object value) { //此方法是同步阻塞的
|
||||
System.out.println("--缓存写入--:key=" + key + ", value=" + value); |
||||
} |
||||
@Override |
||||
public void delete(Object key, Object value, RemovalCause cause) { System.out.println("--缓存删除--:key=" + key); } |
||||
}) |
||||
.expireAfterAccess(6, TimeUnit.HOURS) //过期时间
|
||||
.build((String key)->"刷新的数据"); //cacheload实现类,刷新时候调用
|
||||
// loadingCache.put("name","侯征");
|
||||
return loadingCache; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,79 @@
|
||||
package com.mh.user.utils; |
||||
|
||||
import com.alibaba.fastjson2.JSONArray; |
||||
import com.alibaba.fastjson2.JSONObject; |
||||
import com.github.benmanes.caffeine.cache.Cache; |
||||
import com.mh.user.constants.Constant; |
||||
import com.mh.user.entity.ChillersEntity; |
||||
import com.mh.user.entity.DeviceCodeParamEntity; |
||||
import com.mh.user.service.DeviceCodeParamService; |
||||
import org.springframework.context.ApplicationContext; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @Classname CacheUtil |
||||
* Todo: 缓存工具类 |
||||
* @Date 2021-11-21 10:03 |
||||
* @Created by LJF |
||||
*/ |
||||
public class CacheUtil { |
||||
|
||||
// 调用service
|
||||
ApplicationContext context = SpringBeanUtil.getApplicationContext(); |
||||
Cache caffeineCache = (Cache) context.getBean("caffeineCache"); |
||||
|
||||
DeviceCodeParamService deviceCodeParamService = context.getBean(DeviceCodeParamService.class); |
||||
|
||||
private static class SingletonHolder{ |
||||
private static final CacheUtil instance=new CacheUtil(); |
||||
} |
||||
private CacheUtil(){ |
||||
createDeviceParams(); |
||||
} |
||||
public static CacheUtil getInstance(){ |
||||
return SingletonHolder.instance; |
||||
} |
||||
|
||||
/** |
||||
* 把采集ddc设置的报文存入到缓存中 |
||||
*/ |
||||
private void createDeviceParams() { |
||||
// 缓存压变、温控
|
||||
if (caffeineCache.getIfPresent("1") == null) { |
||||
List<DeviceCodeParamEntity> deviceCodeParamEntities = deviceCodeParamService.queryCodeParam3(null); |
||||
caffeineCache.put("1", deviceCodeParamEntities); |
||||
} |
||||
// 缓存水、电表、状态检测
|
||||
if (caffeineCache.getIfPresent("2") == null) { |
||||
List<DeviceCodeParamEntity> deviceCodeParam2Entities = deviceCodeParamService.queryCodeParam4(null); |
||||
caffeineCache.put("2", deviceCodeParam2Entities); |
||||
} |
||||
// 水位开关
|
||||
if (caffeineCache.getIfPresent("3") == null) { |
||||
List<DeviceCodeParamEntity> deviceCodeParam3Entities = deviceCodeParamService.queryCodeParam5(null); |
||||
caffeineCache.put("3", deviceCodeParam3Entities); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 通过发生类型获取发生的报文集合(返回List类型) |
||||
* @param type |
||||
* @return |
||||
*/ |
||||
public List<DeviceCodeParamEntity> getDeviceParamsByType(String type) { |
||||
Object cacheObject = caffeineCache.getIfPresent(type); |
||||
// 如果为空,重新添加
|
||||
if (cacheObject == null) { |
||||
createDeviceParams(); |
||||
// 在重新获取数据
|
||||
cacheObject = caffeineCache.getIfPresent(type); |
||||
} |
||||
return JSONArray.parseArray(JSONObject.toJSONString(cacheObject), DeviceCodeParamEntity.class); |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue