经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
SpringBoot整合Ehcache3
来源:cnblogs  作者:code2roc  时间:2022/1/3 16:46:34  对本文有异议

前言

公司部门老项目要迁移升级java版本,需要进行缓存相关操作,原框架未支持这部分,经过调研java相关缓存方案大致分为ehcache和redis两种,redis的value最大值为500mb且超过1mb会对存取有性能影响,业务系统需要支持列表查询缓存就不可避免的涉及到大量的数据存取过滤,ehcache支持内存+磁盘缓存不用担心缓存容量问题,所以框架初步版本决定集成ehcache3,设计流程结构如下图所示

缓存配置

maven引用

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-cache</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.ehcache</groupId>
  7. <artifactId>ehcache</artifactId>
  8. </dependency>

个性化配置

  1. #缓存配置
  2. cache:
  3. ehcache:
  4. heap: 1000
  5. offheap: 100
  6. disk: 500
  7. diskDir: tempfiles/cache/
  1. @Component
  2. @ConfigurationProperties("frmae.cache.ehcache")
  3. public class EhcacheConfiguration {
  4. /**
  5. * ehcache heap大小
  6. * jvm内存中缓存的key数量
  7. */
  8. private int heap;
  9. /**
  10. * ehcache offheap大小
  11. * 堆外内存大小, 单位: MB
  12. */
  13. private int offheap;
  14. /**
  15. * 磁盘持久化目录
  16. */
  17. private String diskDir;
  18. /**
  19. * ehcache disk
  20. * 持久化到磁盘的大小, 单位: MB
  21. * diskDir有效时才生效
  22. */
  23. private int disk;
  24. public EhcacheConfiguration(){
  25. heap = 1000;
  26. offheap = 100;
  27. disk = 500;
  28. diskDir = "tempfiles/cache/";
  29. }
  30. }

代码注入配置

因为springboot默认缓存优先注入redis配置,所以需要手动声明bean进行注入,同时ehcache的value值必须支持序列化接口,不能使用Object代替,这里声明一个缓存基类,所有缓存value对象必须继承该类

  1. public class BaseSystemObject implements Serializable {
  2. }
  1. @Configuration
  2. @EnableCaching
  3. public class EhcacheConfig {
  4. @Autowired
  5. private EhcacheConfiguration ehcacheConfiguration;
  6. @Autowired
  7. private ApplicationContext context;
  8. @Bean(name = "ehCacheManager")
  9. public CacheManager getCacheManager() {
  10. //资源池生成器配置持久化
  11. ResourcePoolsBuilder resourcePoolsBuilder = ResourcePoolsBuilder.newResourcePoolsBuilder()
  12. // 堆内缓存大小
  13. .heap(ehcacheConfiguration.getHeap(), EntryUnit.ENTRIES)
  14. // 堆外缓存大小
  15. .offheap(ehcacheConfiguration.getOffheap(), MemoryUnit.MB)
  16. // 文件缓存大小
  17. .disk(ehcacheConfiguration.getDisk(), MemoryUnit.MB);
  18. //生成配置
  19. ExpiryPolicy expiryPolicy = ExpiryPolicyBuilder.noExpiration();
  20. CacheConfiguration config = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, BaseSystemObject.class, resourcePoolsBuilder)
  21. //设置永不过期
  22. .withExpiry(expiryPolicy)
  23. .build();
  24. CacheManagerBuilder cacheManagerBuilder = CacheManagerBuilder.newCacheManagerBuilder()
  25. .with(CacheManagerBuilder.persistence(ehcacheConfiguration.getDiskDir()));
  26. return cacheManagerBuilder.build(true);
  27. }
  28. }

缓存操作

缓存预热

针对缓存框架选择的双写策略,即数据库和缓存同时写入,所以在系统启动时需要预先将数据库数据加载到缓存中

针对单表声明自定义注解,个性化缓存定义自定义接口

  1. @Target({ElementType.TYPE})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Inherited
  5. public @interface HPCache {
  6. }
  1. public interface IHPCacheInitService {
  2. String getCacheName();
  3. void initCache();
  4. }

系统初始化时同步进行缓存初始化,扫描注解实体类与接口实现Bean

  1. @Async
  2. public void initCache(Class runtimeClass, List<String> extraPackageNameList) {
  3. List<Class<?>> cacheEntityList = new ArrayList<>();
  4. if (!runtimeClass.getPackage().getName().equals(Application.class.getPackage().getName())) {
  5. cacheEntityList.addAll(ScanUtil.getAllClassByPackageName_Annotation(runtimeClass.getPackage(), HPCache.class));
  6. }
  7. for (String packageName : extraPackageNameList) {
  8. cacheEntityList.addAll(ScanUtil.getAllClassByPackageName_Annotation(packageName, HPCache.class));
  9. }
  10. for (Class clazz : cacheEntityList) {
  11. TableName tableName = (TableName) clazz.getAnnotation(TableName.class);
  12. List<LinkedHashMap<String, Object>> resultList = commonDTO.selectList(tableName.value(), "*", "1=1", "", new HashMap<>(), false);
  13. for (LinkedHashMap<String, Object> map : resultList) {
  14. Cache cache = cacheManager.getCache(clazz.getName(), String.class, BaseSystemObject.class);
  15. String unitguid = ConvertOp.convert2String(map.get("UnitGuid"));
  16. try {
  17. Object obj = clazz.newInstance();
  18. obj = ConvertOp.convertLinkHashMapToBean(map, obj);
  19. cache.put(unitguid, obj);
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. }
  25. //自定义缓存
  26. Map<String, IHPCacheInitService> res = context.getBeansOfType(IHPCacheInitService.class);
  27. for (Map.Entry en : res.entrySet()) {
  28. IHPCacheInitService service = (IHPCacheInitService) en.getValue();
  29. service.initCache();
  30. }
  31. System.out.println("缓存初始化完毕");
  32. }

需要注意,在EhcacheConfig配置类中需要进行缓存名称的提前注册,否则会导致操作缓存时空指针异常

  1. Map<String, Object> annotatedBeans = context.getBeansWithAnnotation(SpringBootApplication.class);
  2. Class runtimeClass = annotatedBeans.values().toArray()[0].getClass();
  3. //do,dao扫描
  4. List<String> extraPackageNameList = new ArrayList<String>();
  5. extraPackageNameList.add(Application.class.getPackage().getName());
  6. List<Class<?>> cacheEntityList = new ArrayList<>();
  7. if (!runtimeClass.getPackage().getName().equals(Application.class.getPackage().getName())) {
  8. cacheEntityList.addAll(ScanUtil.getAllClassByPackageName_Annotation(runtimeClass.getPackage(), HPCache.class));
  9. }
  10. for (String packageName : extraPackageNameList) {
  11. cacheEntityList.addAll(ScanUtil.getAllClassByPackageName_Annotation(packageName, HPCache.class));
  12. }
  13. for (Class clazz : cacheEntityList) {
  14. cacheManagerBuilder = cacheManagerBuilder.withCache(clazz.getName(), config);
  15. }
  16. //自定义缓存
  17. Map<String, IHPCacheInitService> res = context.getBeansOfType(IHPCacheInitService.class);
  18. for (Map.Entry en :res.entrySet()) {
  19. IHPCacheInitService service = (IHPCacheInitService)en.getValue();
  20. cacheManagerBuilder = cacheManagerBuilder.withCache(service.getCacheName(), config);
  21. }

更新操作

手动获取ehcache的bean对象,调用put,repalce,delete方法进行操作

  1. private CacheManager cacheManager = (CacheManager) SpringBootBeanUtil.getBean("ehCacheManager");
  2. public void executeUpdateOperation(String cacheName, String key, BaseSystemObject value) {
  3. Cache cache = cacheManager.getCache(cacheName, String.class, BaseSystemObject.class);
  4. if (cache.containsKey(key)) {
  5. cache.replace(key, value);
  6. } else {
  7. cache.put(key, value);
  8. }
  9. }
  10. public void executeDeleteOperation(String cacheName, String key) {
  11. Cache cache = cacheManager.getCache(cacheName, String.class, BaseSystemObject.class);
  12. cache.remove(key);
  13. }

查询操作

缓存存储单表以主键—object形式存储,个性化缓存为key-object形式存储,单条记录可以通过getCache方法查询,列表查询需要取出整个缓存按条件进行过滤

  1. public Object getCache(String cacheName, String key){
  2. Cache cache = cacheManager.getCache(cacheName, String.class, BaseSystemObject.class);
  3. return cache.get(key);
  4. }
  5. public List<Object> getAllCache(String cacheName){
  6. List result = new ArrayList<>();
  7. Cache cache = cacheManager.getCache(cacheName, String.class, BaseSystemObject.class);
  8. Iterator iter = cache.iterator();
  9. while (iter.hasNext()) {
  10. Cache.Entry entry = (Cache.Entry) iter.next();
  11. result.add(entry.getValue());
  12. }
  13. return result;
  14. }

缓存与数据库数据一致性

数据库数据操作与缓存操作顺序为先操作数据后操作缓存,在开启数据库事务的情况下针对单条数据单次操作是没有问题的,如果是组合操作一旦数据库操作发生异常回滚,缓存并没有回滚就会导致数据的不一致,比如执行顺序为dbop1=》cacheop1=》dbop2=》cacheop2,dbop2异常,cacheop1的操作已经更改了缓存

这里选择的方案是在数据库全部执行完毕后统一操作缓存,这个方案有一个缺点是如果缓存操作发生异常还是会出现上述问题,实际过程中缓存只是对内存的操作异常概率较小,对缓存操作持乐观状态,同时我们提供手动重置缓存的功能,算是一个折中方案,下面概述该方案的一个实现

声明自定义缓存事务注解

  1. @Target({ElementType.METHOD})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Inherited
  5. public @interface CacheTransactional {
  6. }

声明切面监听,在标记了CacheTransactional注解的方法执行前进行Redis标识,统一执行完方法体后执行缓存操作

  1. @Aspect
  2. @Component
  3. @Order(value = 101)
  4. public class CacheExecuteAspect {
  5. @Autowired
  6. private CacheExecuteUtil cacheExecuteUtil;
  7. /**
  8. * 切面点 指定注解
  9. */
  10. @Pointcut("@annotation(com.haopan.frame.common.annotation.CacheTransactional) " +
  11. "|| @within(com.haopan.frame.common.annotation.CacheTransactional)")
  12. public void cacheExecuteAspect() {
  13. }
  14. /**
  15. * 拦截方法指定为 repeatSubmitAspect
  16. */
  17. @Around("cacheExecuteAspect()")
  18. public Object around(ProceedingJoinPoint point) throws Throwable {
  19. MethodSignature signature = (MethodSignature) point.getSignature();
  20. Method method = signature.getMethod();
  21. CacheTransactional cacheTransactional = method.getAnnotation(CacheTransactional.class);
  22. if (cacheTransactional != null) {
  23. cacheExecuteUtil.putCacheIntoTransition();
  24. try{
  25. Object obj = point.proceed();
  26. cacheExecuteUtil.executeOperation();
  27. return obj;
  28. }catch (Exception e){
  29. e.printStackTrace();
  30. throw e;
  31. }
  32. } else {
  33. return point.proceed();
  34. }
  35. }
  36. }

将缓存操作以线程id区分放入待执行队列中序列化到redis,提供方法统一操作

  1. public class CacheExecuteModel implements Serializable {
  2. private String obejctClazzName;
  3. private String cacheName;
  4. private String key;
  5. private BaseSystemObject value;
  6. private String executeType;
  7. }
  1. private CacheManager cacheManager = (CacheManager) SpringBootBeanUtil.getBean("ehCacheManager");
  2. @Autowired
  3. private RedisUtil redisUtil;
  4. public void putCacheIntoTransition(){
  5. String threadID = Thread.currentThread().getName();
  6. System.out.println("init threadid:"+threadID);
  7. CacheExecuteModel cacheExecuteModel = new CacheExecuteModel();
  8. cacheExecuteModel.setExecuteType("option");
  9. redisUtil.redisTemplateSetForCollection(threadID,cacheExecuteModel, GlobalEnum.RedisDBNum.Cache.get_value());
  10. redisUtil.setExpire(threadID,5, TimeUnit.MINUTES, GlobalEnum.RedisDBNum.Cache.get_value());
  11. }
  12. public void putCache(String cacheName, String key, BaseSystemObject value) {
  13. if(checkCacheOptinionInTransition()){
  14. String threadID = Thread.currentThread().getName();
  15. CacheExecuteModel cacheExecuteModel = new CacheExecuteModel("update", cacheName, key, value.getClass().getName(),value);
  16. redisUtil.redisTemplateSetForCollection(threadID,cacheExecuteModel, GlobalEnum.RedisDBNum.Cache.get_value());
  17. redisUtil.setExpire(threadID,5, TimeUnit.MINUTES, GlobalEnum.RedisDBNum.Cache.get_value());
  18. }else{
  19. executeUpdateOperation(cacheName,key,value);
  20. }
  21. }
  22. public void deleteCache(String cacheName, String key) {
  23. if(checkCacheOptinionInTransition()){
  24. String threadID = Thread.currentThread().getName();
  25. CacheExecuteModel cacheExecuteModel = new CacheExecuteModel("delete", cacheName, key);
  26. redisUtil.redisTemplateSetForCollection(threadID,cacheExecuteModel, GlobalEnum.RedisDBNum.Cache.get_value());
  27. redisUtil.setExpire(threadID,5, TimeUnit.MINUTES, GlobalEnum.RedisDBNum.Cache.get_value());
  28. }else{
  29. executeDeleteOperation(cacheName,key);
  30. }
  31. }
  32. public void executeOperation(){
  33. String threadID = Thread.currentThread().getName();
  34. if(checkCacheOptinionInTransition()){
  35. List<LinkedHashMap> executeList = redisUtil.redisTemplateGetForCollectionAll(threadID, GlobalEnum.RedisDBNum.Cache.get_value());
  36. for (LinkedHashMap obj:executeList) {
  37. String executeType = ConvertOp.convert2String(obj.get("executeType"));
  38. if(executeType.contains("option")){
  39. continue;
  40. }
  41. String obejctClazzName = ConvertOp.convert2String(obj.get("obejctClazzName"));
  42. String cacheName = ConvertOp.convert2String(obj.get("cacheName"));
  43. String key = ConvertOp.convert2String(obj.get("key"));
  44. LinkedHashMap valueMap = (LinkedHashMap)obj.get("value");
  45. String valueMapJson = JSON.toJSONString(valueMap);
  46. try{
  47. Object valueInstance = JSON.parseObject(valueMapJson,Class.forName(obejctClazzName));
  48. if(executeType.equals("update")){
  49. executeUpdateOperation(cacheName,key,(BaseSystemObject)valueInstance);
  50. }else if(executeType.equals("delete")){
  51. executeDeleteOperation(cacheName,key);
  52. }
  53. }catch (Exception e){
  54. e.printStackTrace();
  55. }
  56. }
  57. redisUtil.redisTemplateRemove(threadID,GlobalEnum.RedisDBNum.Cache.get_value());
  58. }
  59. }
  60. public boolean checkCacheOptinionInTransition(){
  61. String threadID = Thread.currentThread().getName();
  62. System.out.println("check threadid:"+threadID);
  63. return redisUtil.isValid(threadID, GlobalEnum.RedisDBNum.Cache.get_value());
  64. }

原文链接:http://www.cnblogs.com/yanpeng19940119/p/15759640.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号