自定义注解设置缓存有效期的正确姿势

RedisCacheDuration:基于Spring-Data-Redis,自定义注解实现Redis缓存有效期的灵活设置 RedisCacheDuration 扩展自Spring-Data-Redis,自定义注解实现缓存有效期的灵活设置,如您觉得该项目对您有用,欢迎点击右上方的Star按钮,给予支持! 使用方法 将包代码加入项目后,将SpringRedisCacheManager注册为bean,然后在类或方法上标注CacheDuration注解,并设置缓存有效时间,如果不设置,默认是60秒 立即下载
引言

redis缓存的有效期可以通过xml配置文件设置(默认有效期),也可以通过编码的方式手动去设置,但是这两种方式都存在缺陷。xml方式设置的是全局的默认有效期,虽然灵活,但不能给某个缓存设置单独的有效期;硬编码方式虽然可以给不同的缓存设置单独的有效期,但是管理上不够灵活。Spring提供的Cache相关注解中并没有提供有效期的配置参数,so,自定义注解实现缓存有效期的灵活设置诞生了,具体源码前往github下载。

redis-queue.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
    ">
	<!-- 配置一个高可用环境的哨兵模式的redis环境 -->
     <bean id="redisSentinelConfiguration"
        class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
        <property name="master">
            <bean class="org.springframework.data.redis.connection.RedisNode">
                <property name="name" value="mymaster"/>
            </bean>
        </property>
        <property name="sentinels">
            <set>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis1.ip}"/>
                    <constructor-arg name="port" value="${redis1.port}"/>                       
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis2.ip}"/>
                    <constructor-arg name="port" value="${redis2.port}"/>               
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">                   
                    <constructor-arg name="host" value="${redis3.ip}"/>
                    <constructor-arg name="port" value="${redis3.port}"/>                   
                </bean>
            </set>
        </property>
   </bean>
     
     
    <!-- redis 连接池相关配置-->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" >
		<property name="maxTotal" value="${redis.maxTotal}"/> 
		<property name="maxIdle" value="${redis.maxIdle}"/> 
		<property name="maxWaitMillis" value="${redis.maxWaitMillis}" /> 
		<property name="minIdle" value="${redis.minIdle}"/>
		<property name="testOnBorrow" value="${redis.testOnBorrow}" />
	</bean>
	<!-- JedisConnectionFactory 相关配置-->
	<bean id="jedisFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="poolConfig" >
			<ref bean="jedisPoolConfig"/>
		</property>
			<constructor-arg name="sentinelConfig" ref="redisSentinelConfiguration"/> 
		<property name="timeout" value="${redis.timeout}"/>
	</bean>
	
	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory">
			<ref bean="jedisFactory"/>
		</property>
	</bean>
	

		<!-- 使用自定义的SpringRedisCacheManager类  -->
	<bean id="redisCacheManager" class="tf56.skynet.annotation.SpringRedisCacheManager">
		<!--为默认的构造方法设置值-->
		<constructor-arg name="redisTemplate" ref="redisTemplate"/>
		<!--设置redi缓存默认的过期时间-->
		<property name="defaultExpiration" value="${redis.expiration}"/>
	</bean>

    
    
</beans>

这里介绍RedisCacheManager中一个重要的方法,void setExpires(Map<String, Long> expires),该方法的传入参数是一个Map,Map的key值是@Cacheable(或@CacheEvict或@CachePut)注解的value值,Map的value值是缓存的有效期(单位秒),用于批量设置缓存的有效期。

自定义注解
直接贴代码了,如下。

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface CacheDuration {
    //Sets the expire time (in seconds).
    public long duration() default 60;
}
使用@CacheDuration

@Service("userService")
@CacheDuration(duration = 6)
public class UserService {
    @Cacheable(value = "User", key = "'UserId_' + #id", condition = "#id<=110")
    @CacheDuration(duration = 16)
    public String queryFullNameById(long id) {
        System.out.println("execute queryFullNameById method");
        return "ZhangSanFeng";
    }
}
新RedisCacheManager
新写了一个SpringRedisCacheManager,继承自RedisCacheManager,用于对@CacheDuration解析及有效期的设置,代码如下。
package tf56.skynet.annotation;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static com.google.common.collect.Sets.newHashSet;
import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
import static org.springframework.util.StringUtils.isEmpty;

/**
 * Created by Administrator on 2017/10/18.
 * 新写了一个SpringRedisCacheManager,继承自RedisCacheManager,
 * 用于对@CacheDuration解析及有效期的设置
 */
public class SpringRedisCacheManager extends RedisCacheManager implements ApplicationContextAware, InitializingBean {
    private ApplicationContext applicationContext;
    public SpringRedisCacheManager(RedisTemplate redisTemplate) {
        super(redisTemplate);
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
    @Override
    public void afterPropertiesSet() {
        parseCacheDuration(applicationContext);
    }
    private void parseCacheDuration(ApplicationContext applicationContext) {
        final Map<String, Long> cacheExpires = new HashMap<>();
        String[] beanNames = applicationContext.getBeanNamesForType(Object.class);
        for (String beanName : beanNames) {
            final Class clazz = applicationContext.getType(beanName);
            Service service = findAnnotation(clazz, Service.class);
            if (null == service) {
                continue;
            }
            addCacheExpires(clazz, cacheExpires);
        }
        //设置有效期
        super.setExpires(cacheExpires);
    }
    private void addCacheExpires(final Class clazz, final Map<String, Long> cacheExpires) {
        ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
            @Override
            public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                ReflectionUtils.makeAccessible(method);
                CacheDuration cacheDuration = findCacheDuration(clazz, method);
                Cacheable cacheable = findAnnotation(method, Cacheable.class);
                CacheConfig cacheConfig = findAnnotation(clazz, CacheConfig.class);
                Set<String> cacheNames = findCacheNames(cacheConfig, cacheable);
                for (String cacheName : cacheNames) {
                    cacheExpires.put(cacheName, cacheDuration.duration());
                }
            }
        }, new ReflectionUtils.MethodFilter() {
            @Override
            public boolean matches(Method method) {
                return null != findAnnotation(method, Cacheable.class);
            }
        });
    }
    /**
     * CacheDuration标注的有效期,优先使用方法上标注的有效期
     * @param clazz
     * @param method
     * @return
     */
    private CacheDuration findCacheDuration(Class clazz, Method method) {
        CacheDuration methodCacheDuration = findAnnotation(method, CacheDuration.class);
        if (null != methodCacheDuration) {
            return methodCacheDuration;
        }
        CacheDuration classCacheDuration = findAnnotation(clazz, CacheDuration.class);
        if (null != classCacheDuration) {
            return classCacheDuration;
        }
        throw new IllegalStateException("No CacheDuration config on Class " + clazz.getName() + " and method " + method.toString());
    }
    private Set<String> findCacheNames(CacheConfig cacheConfig, Cacheable cacheable) {
        return isEmpty(cacheable.value()) ?
                newHashSet(cacheConfig.cacheNames()) : newHashSet(cacheable.value());
    }
}
redis.properties配置如下:

redis.maxTotal=20
redis.maxIdle=10
redis.maxWaitMillis=3000
redis.minIdle=1
redis.testOnBorrow=true
redis.timeout=10000
redis1.port=26379
redis1.ip=10.7.13.152
redis2.port=26379
redis2.ip=10.7.13.160
redis3.port=26379
redis3.ip=10.7.13.209
redis.expiration=3000
最后将redis.properties 、redis-queue.xml 配置文件配置加载到项目主配置文件xxx-servlet.xml中  部分配置如下所示,对应着配置即可:

<?xml version="1.0" encoding="UTF-8"?>

<!-- DispatcherServlet Context: defines this servlet's request-processing 
	infrastructure -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
	    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">

    <bean id="handlerMapping"
          class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="order" value="1"/>
    </bean>
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
    <!-- 解决@responseBody返回乱码问题 -->
    <bean
            class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean
                        class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
                <bean
                        class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/plain;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
                <!-- <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                    <property name="supportedMediaTypes" value="application/json" /> </bean> -->
                <bean
                        class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
                <bean
                        class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
                <bean
                        class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"/>
                <bean
                        class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
            </list>
        </property>
        <property name="webBindingInitializer">
            <bean
                    class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
                <property name="validator" ref="validator"/>
                <!-- <property name="conversionService">
                    <bean
                        class="org.springframework.format.support.FormattingConversionServiceFactoryBean"></bean>
                </property> -->
            </bean>
        </property>
    </bean>
    <mvc:default-servlet-handler/>
    <!-- 指定redisCacheManager-->
    <cache:annotation-driven cache-manager="redisCacheManager"/>

    <bean id="configBean"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:config/redis.properties</value>
            </list>
        </property>
    </bean>
    <context:component-scan base-package="tf56.skynet"/>
    <import resource="classpath:/config/redis-queue.xml"/>
    <context:annotation-config/>
    <mvc:annotation-driven/>
</beans>

编写service实现类:

@Service("userService")
@CacheDuration(duration = 6)
public class UserService {
    @Cacheable(value = "User", key = "'UserId_' + #id", condition = "#id<=110")
    @CacheDuration(duration = 16)
    public String queryFullNameById(long id) {
        System.out.println("execute queryFullNameById method");
        return "ZhangSanFeng";
    }
}
测试代码:

@Test
    public void testRedisCacheManager() {
        ApplicationContext context = new ClassPathXmlApplicationContext("redisCacheContext.xml");
        UserService userService = (UserService) context.getBean("userService");
        RedisTemplate redisTemplate = (RedisTemplate) context.getBean("redisTemplate");
        System.out.println("第一次执行查询:" + userService.queryFullNameById(100L));
        System.out.println("----------------------------------");
        System.out.println("第二次执行查询:" + userService.queryFullNameById(100L));
        System.out.println("----------------------------------");
        System.out.println("UserId_100有效期(单位秒):" + redisTemplate.getExpire("UserId_100", TimeUnit.SECONDS));
        System.out.println("----------------------------------");
        try {
            Thread.sleep(3000);
            System.out.println("主线程休眠3秒后");
            System.out.println("----------------------------------");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("UserId_100有效期(单位秒):" + redisTemplate.getExpire("UserId_100", TimeUnit.SECONDS));
        System.out.println("----------------------------------");
        System.out.println("第三次执行查询:" + userService.queryFullNameById(100l));
    }
测试结果:

execute queryFullNameById method
第一次执行查询:ZhangSanFeng
----------------------------------
第二次执行查询:ZhangSanFeng
----------------------------------
UserId_100有效期(单位秒):15
----------------------------------
主线程休眠3秒后
----------------------------------
UserId_100有效期(单位秒):12
----------------------------------
第三次执行查询:ZhangSanFeng

结果分析
UserService类上标注的CacheDuration设置有效期是6秒,而方法queryFullNameById上CacheDuration设置的有效期是16秒,最后生效的是16秒。


参考原文:

http://www.jianshu.com/p/2633fb37862c









超声波法检测局部放电的原理与实现 摘要:超声波法通过捕捉局部放电产生的声波信号进行检测,系统由超声波传感器、信号调理单元和数据采集系统组成。其核心应用是局放源定位,主要采用时差定位法和声-电联合定位法。该方法具有抗电磁干扰强、定位精确等优点,但也存在信号衰减大、传播路径复杂等缺点,典型应用于变压器、GIS等设备的在线监测与故障诊断。 阅读详情

相关推荐

OpenSceneGraph编译、安装、开发环境

详细描述WIndows 10下使用Visual Studio 2019编译OpenSceneGraph 3.6.5的操作流程

国产CAx(CAD/CAE/CAM)软件研发 8771

缓存技术

如果每次进入页面的时候都查询数据库生成页面内容的话,如果访问量非常大,则网站性能会非常差。而如果只有第一次访问的时候才查询数据库生成页面内容,以后都直接输出内容,则能提高系统性能。这样无论有多少人访问都只访问一次数据库,数据库压力不变。 缓存(Cache)是一种用空间换取时间的技术,存在于计算机中很多地方,用来将一些慢速设备中的常用数据保存在快速设备中,取数据的时候直接从快速设备中取。比如CPU

rogerzhanglijie的专栏 826

Solidworks转URDF避坑实录:从履带机器人到ROS仿真的完整配置流程

本文详细介绍了将Solidworks履带机器人模型转换为URDF格式并配置ROS仿真的完整流程,重点解析了坐标系设置、复杂关节定义、插件配置等关键步骤中的常见问题与解决方案,帮助开发者高效实现履带机器人在RVIZ和Gazebo中的精确仿真。

weixin_30693683的博客 456

Cache技术――OSCache(三)

三、OSCache的基本用法(缓存JSP页面中部分) (一): Cache-OSCache 提供的缓存标签 这是 OSCache 提供的标签库中最重要的一个标签,包括在标签中的内容将应用缓存机制进行处理,处理的方式将取决于编程者对 cache 标签属性的设置。 第一次请求到达时,标签中的内容被处理并且缓存起来,当下一个请求到达时,缓存系统会检查这部分内容的缓存是否已...

JAVA 176

新手必看!Stata工具变量分析中的三大陷阱:如何正确解读LM统计量、CDW检验和Hansen J值

本文深度解析Stata工具变量分析中的三大核心检验:不可识别检验、弱IV检验和过度识别检验,帮助新手避开常见陷阱。详细解读LM统计量、CDW检验和Hansen J值的正确使用方法,提供实战案例和Stata操作指南,确保研究结论的可靠性。

weixin_29231027的博客 572

基于UC3842的反激电源的全面深入解析与MATLAB仿真

内置电流检测,可实现电流模式控制,提高系统的动态响应性。多种保护功能,如欠压锁定、过温保护、短路保护等,确保系统的安全可靠运行。宽工作电压范围,适应不同的电源设计需求。简单的外部元件配置,易于实现。UC3842的典型应用包括开关电源、DC-DC转换器和反激式电源等,其稳定性和可靠性在实际应用中得到了广泛验证。

qq_38334677的博客 1086

基于spring @Cacheable扩展实现缓存自动过期时间以及即将到期自动刷新

形如下通过#分隔,#后面部分代表过期时间(单位为秒)1、自定义缓存管理器并继承RedisCacheManager,同时重写createRedisCache方法@Override2、将默认的缓存管理器改成我们自定义的缓存管理器@Bean通过如上2个步骤,即可实现缓存过期第一种方法的实现是简单,但缺点是语义不直观,因此得做好宣导以及wiki,不然对于新人来说,他可能都不知道cacheName用#分割是代表啥意思方法二的实现逻辑步骤如下1、自定义注解LybGeekCacheable。

yunnansunshitao的博客 2297

数据分片的三种方式:哈希、一致性哈希、按照数据范围分片

数据分片就是按照一定的规则,将数据集划分成相互独立、正交的数据子集,然后将数据子集分布到不同的节点上。通过设计合理的数据分片规则,可将系统中的数据分布在不同的物理数据库中,达到提升应用系统数据处理速度的目的。三种常用的数据分片方式:1. 哈希分片2. 一致性哈希分片(Consistent Hash)3. 按照数据范围分片(Range Based)数据分片的应用场景:1. 数据库扩展2. 分布式缓存对集群部署多个缓存节点,提高缓存的处理能力。

wjianwei666的专栏 1133

.Net C# Web Service 中的cacheDuration解释

Web Service 中的cacheDuration解释Web服务定义一个单独的 GetInfo 方法,其CacheDuration值设为3600秒。在这个方法第一次被调用之后,随后3600秒之内如果参数sIndex相同,则都将返回相同的响应结果,即使数据(GetInfoFromDBByIndex(sIndex)的返回值)是从数据库中动态获得的也是如此。也就是说在3600秒内只对数据库进

编程岁月 1872

20_文件系统的制作 -JFFS2

JFFS2 文件系统是通过工具 mkfs.jffs2 将文件系统目录制成映像文件。制作工具mkfs.jffs2 需要编译 zhb 库和 mtd-utils,下面详细介绍其制作过程

Leon的博客 549

从win系统远程使用服务器中的nsight system、nsight compute

本文介绍了如何在Windows系统中远程使用服务器上的Nsight System和Nsight Compute工具。主要内容包括:1)双系统同步安装相同版本的Nsight工具,配置环境变量;2)命令行生成profile文件或直接运行分析;3)Windows远程连接服务器的配置方法;4)解决权限问题的具体方案,如修改文件夹权限或调整NVIDIA驱动配置。文章提供了详细的安装步骤和常见问题解决方案,适用于需要进行GPU性能分析和调试的开发人员。

Justinyh的博客 401

Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用

前言 看到这篇文章写得不错,转载一下,既是温习又是笔记了 在原文的基础上,将XML的配置改成了springboot @Configuration的配置方式 1. 基于注解的支持 Spring为我们提供了几个注解来支持Spring Cache。其核心主要是@Cacheable和@CacheEvict。使用@Cacheable标记的方法在执行后Spring Cache将缓存其返回结果,而使用@CacheEvict标记的方法会在方法执行前或者执行后移除Spring Cache中的某些元素。下面我们将来详细介绍一下

灼烧的疯狂 1万+

破盾实录|Vueling航空阿卡迈防护逆向:从503挑战到Cookie生成的全流程拆解

摘要:文章分享了作者在爬取西班牙伏林航空(Vueling)机票数据时,遭遇阿卡迈(Akamai)防护系统的实战经历。起初简单的请求被拦截返回503错误,分析发现阿卡迈通过混淆JS代码实施"挑战-验证"机制,需模拟真实浏览器环境才能获取有效Cookie。作者详细拆解了逆向过程:从抓包分析JS挑战流程,到破解代码混淆和控制流平坦化,最终用Python+Pyppeteer成功模拟浏览器特征,突破防护获取数据。整个过程涉及JS反混淆、环境检测绕过和Cookie生成验证等关键技术点,为类似防护系统

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1642

springboot基础(66):spring缓存@Cacheable设置缓存有效期

在使用@Cacheable注解时,数据被缓存到redis服务器,但是没有设置失效时间。查看Cacheable的源码,也没有提供设置缓存时间的属性和办法。如何给缓存设置时间呢?

不积跬步,无以至千里 7876

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Spring 缓存注解这样用,太香了!

其实我们可以通过自定义类继承提供的类后,重写方法来完成自定义缓存有效期的功能,代码如下,@Override// 解析 @Cacheable 注解的 value 属性用以单独设置有效期类逻辑如下,继承提供的类。重写方法。解析 name 参数,根据字符串进行分割,获取缓存 key 名称以及缓存有效期。重新设置缓存 key 名称以及缓存有效期。调用父类的方法来完成缓存写入。接着我们修改下类的方法用以使用类。代码如下,@Bean最后在使用@Cacheable注解时,在原有。

Reische的博客 417

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

SpringBoot的Cacheable缓存注解

在本文中,我们介绍了如何在 Spring Boot 应用程序中使用 Redis 进行缓存。我们介绍了如何通过自定义 RedisTemplate Bean 来配置自己的 Redis 序列化器,在 Cacheable 注解中指定缓存区域和缓存键,以及如何使用 @CacheEvict 方法来清除 Redis 缓存中的数据。同时,我们还展示了更高级的功能,例如使用 CacheManager 对象管理缓存。

小虎哥的技术博客 835

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Spring - Spring Cache 缓存注解这样用,实在是太香了!

Spring - Spring Cache 缓存注解这样用,实在是太香了!

905

java cache 有效期_springboot cache 自定义过期时间及自定义缓存key前缀

本篇文章主要是springboot2 中 redis cache中的一些内容,主要包含2个功能点:自定义缓存过期时间(全局自定义,每个key不能单独指定)自定义缓存key前缀废话不多说,上代码@Configuration@EnableCachingpublic class CacheConfig extends CachingConfigurerSupport {private static fi...

weixin_33520510的博客 1843

AOP和注解实现自定义缓存

使用自定义注解和AOP实现添加缓存

qq_63612259的博客 413

SpringBoot——缓存(注解的使用,整合Redis,自定义CacheManager)

1.JSR107 Java Caching定义了5个核心接口: CachingProvider:定义了创建,配置,获取,管理和控制多个CacheManager。一个应用可以在运行期间访问多个CachingProvider。 CacheManager:定义了创建,配置,获取和控制多个Cache,这些Cache存在于CacheManager的上下文中。一个CacheManager仅被一个CachingProvider所拥有 Cache:一个类似Map的数据结构并临时存储以Key为索引的值。一个Cache

努力找工作呀 3698

Spring Cache 缓存注解这样用,实在是太香了!

其实我们可以通过自定义类继承提供的类后,重写方法,代码如下,@Override// 解析 @Cacheable 注解的 value 属性用以单独设置有效期类逻辑如下,继承提供的类。重写方法。解析 name 参数,根据字符串进行分割,获取缓存 key 名称以及缓存有效期。接着我们修改下类的方法用以使用类。代码如下,@Bean最后我们修改下@Cacheable注解使用方式,在原有value属性的值后添加#600,单独标识缓存有效期。代码如下,@Override...看下。

竹林幽深 233

Spring 缓存注解

其实我们可以通过自定义类继承提供的类后,重写方法来完成自定义缓存有效期的功能,代码如下@Override// 解析 @Cacheable 注解的 value 属性用以单独设置有效期。

softshow1026的博客 669

@cachingconfigurersupport注解结合redis自定义缓存读写机制

方法中,我们使用了 RedisCacheConfiguration 来配置 Redis 缓存的一些属性,比如缓存有效期、禁止缓存 null 值等。方法中,我们创建了一个 RedisTemplate 对象,并设置了 key 序列化器、value 序列化器、hash key 序列化器和 hash value 序列化器。方法中,我们使用了一个自定义的缓存键生成器,该生成器将目标对象的类名、方法名和参数列表拼接成一个字符串作为缓存键。方法来配置缓存管理器、缓存键生成器和缓存解析器。方法中所创建的缓存管理器。

m0_50549031的博客 1338
上一篇: 数据库事务的四大特性以及事务的隔离级别
下一篇: 缓存穿透,缓存击穿,缓存雪崩解决方案分析
java1993666
博客等级 码龄11年 22粉丝 29原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值