spring cache (ehcache方式)
创始人
2024-02-08 23:44:46

目录

  • 前置
  • pom: jar
  • 配置文件:
    • ehcache.xml
    • application.yml
  • MyEhCacheCacheConfiguration.java
  • 效果图
    • 禁用 MyEhCacheCacheConfiguration.java
    • 启用 MyEhCacheCacheConfiguration.java


前置

会演示springcache的使用方式
项目地址: https://gitee.com/xmaxm/test-code/blob/master/chaim-cache/chaim-spring-cache/chaim-spring-cache-ehcache/README.md

前置配置

本篇文章是基于上篇文章进行: spring cache (默认方式)

强调:

在追踪源码的时候发现, 是通过注入ehCacheCacheManager进行文件的初始化, 当然解析xml文件也是该地方开始. 也就有了MyEhCacheCacheConfiguration,java方法. 大致思路同源码一致, 只是对获取的数据进行拦截, 增加部分数据, 进行一遍赋值操作

源码部分
spring cache (默认方式) 一致, 只是实现缓存的方式不一样

关键类: org.springframework.cache.Cache
org.springframework.cache.interceptor.CacheInterceptor#invoke
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration
实现类: org.springframework.cache.ehcache.EhCacheCache

相关缓存文章

spring cache (默认方式)
spring cache (Redis方式)
spring cache (ehcache方式)


pom: jar

org.springframework.bootspring-boot-starter-cache
net.sf.ehcacheehcache
javax.cachecache-api

org.reflectionsreflections0.9.10


配置文件:

ehcache.xml



application.yml

spring:cache:# 程序启动时创建的缓存名称cache-names: chaim-name# 缓存类型 org.springframework.boot.autoconfigure.cache.CacheTypetype: ehcacheehcache:config: classpath:ehcache.xml

MyEhCacheCacheConfiguration.java

package com.chaim.spring.cache.ehcache.config;import net.sf.ehcache.CacheManager;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration;
import org.reflections.Reflections;
import org.reflections.scanners.MethodAnnotationsScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.ehcache.EhCacheManagerUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;/*** @author Chaim* @date 2022/9/23 0:11*/
@Component
@Primary
public class MyEhCacheCacheConfiguration {/*** 固定 ehcache.xml 中 cache标签name值为: CACHE_NAME 就进行全局配置*/private static final String CACHE_NAME = "CUSTOMIZE_GLOBAL";/*** 要扫描注解的包路径*/private static final String PACKAGE_NAME = "com.chaim.spring.cache.ehcache.controller";/*** 重写 org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration#ehCacheCacheManager* 实现: 多 cache 标签自动添加** @param cacheProperties* @return*/@BeanCacheManager ehCacheCacheManager(CacheProperties cacheProperties) {Resource location = cacheProperties.resolveConfigLocation(cacheProperties.getEhcache().getConfig());if (location != null) {// 源码: 从给定资源中解析 EhCache 配置,以供进一步使用Configuration configuration = EhCacheManagerUtils.parseConfiguration(location);// 对获取的 ehcache 资源进行重写Map cacheConfigurations = configuration.getCacheConfigurations();// 校验其是否是全局标签 (配置该标签, 就标记需要缓存的都是此一个文件进行加载)CacheConfiguration cacheConfiguration = cacheConfigurations.get(CACHE_NAME);if (cacheConfiguration != null) {// 获取指定路径下的 @Cacheable 的 value 值, 也就是需要进行创建的缓存文件名Set allCacheableValue = this.getAllCacheableValue();allCacheableValue.forEach(key -> {if (cacheConfigurations.get(key) == null) {// 对象克隆, 该处涉及深浅拷贝CacheConfiguration clone = cacheConfiguration.clone();// 修改缓存文件名为 @Cacheable 的 value 值clone.name(key);// 将需要的值 PUT 进入 CacheConfiguration, 对应的cache标签值除name其余不变cacheConfigurations.put(key, clone);}});// 移除全局配置配置文件, 避免创建该文件无效cacheConfigurations.remove(CACHE_NAME);}// 源码: 返回CacheManagerreturn new CacheManager(configuration);}return EhCacheManagerUtils.buildCacheManager();}/*** 获取指定路径下所有 Cacheable 的 value 值** @return Cacheable -> value*/private Set getAllCacheableValue() {Set set = new HashSet<>();// 要扫描的包ConfigurationBuilder configurationBuilder = new ConfigurationBuilder().addUrls(ClasspathHelper.forPackage(PACKAGE_NAME)).addScanners(new MethodAnnotationsScanner());Reflections ref = new Reflections(configurationBuilder);// 获取扫描到的标记注解的集合Set methodsAnnotatedWith = ref.getMethodsAnnotatedWith(Cacheable.class);for (Method method : methodsAnnotatedWith) {Cacheable cacheable = method.getAnnotation(Cacheable.class);String[] value = cacheable.value();set.addAll(Arrays.asList(value));}return set;}
}

效果图

禁用 MyEhCacheCacheConfiguration.java

在这里插入图片描述


启用 MyEhCacheCacheConfiguration.java

在这里插入图片描述

相关内容

热门资讯

埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...