• spring cache之自定义keys的过期时间


    spring @cacheable注解默认不支持方法级别的缓存失效时间,只能通过配置来配置全局的失效时间

    如果需要实现对方法级别的缓存支持失效时间机制,有一种比较简单的方法,spring配置文件如下:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:cache="http://www.springframework.org/schema/cache"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
        <context:component-scan base-package="redis.cache"/>
        <context:annotation-config/>
        <cache:annotation-driven cache-manager="redisCacheManager"/>
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:redis.properties</value>
                </list>
            </property>
        </bean>
        <!-- 配置JedisPoolConfig实例 -->
        <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
            <property name="maxIdle" value="${redis.maxIdle}"/>
            <property name="maxTotal" value="${redis.maxActive}"/>
            <property name="maxWaitMillis" value="${redis.maxWait}"/>
            <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
        </bean>
        <!-- 配置JedisConnectionFactory -->
        <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
            <property name="hostName" value="${redis.host}"/>
            <property name="port" value="${redis.port}"/>
            <property name="password" value="${redis.pass}"/>
            <property name="database" value="${redis.dbIndex}"/>
            <property name="poolConfig" ref="poolConfig"/>
        </bean>
        <!-- 配置RedisTemplate -->
        <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
            <property name="connectionFactory" ref="jedisConnectionFactory"/>
        </bean>
        <!-- 配置RedisCacheManager -->
        <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
            <constructor-arg name="redisOperations" ref="redisTemplate"/>
            <property name="defaultExpiration" value="${redis.expiration}"/>
            <property name="expires">
                <map>
                    <entry key="test" value="30"/>
                </map>
            </property>
        </bean>
    </beans>

    配置文件中的redisCacheManager对象配置了expires属性,该属性是一个map,可以用来设置某些keys的过期时间

    defaultExpiration属性设置了全局的默认失效时间,而expires属性则根据map指定的key单独设置失效时间,可以指定多对key value,

    时间单位为:秒

    注意:expires属性中map的key对应的是@cacheable注解中的value属性指定的值,而不是key。例如:

    @Cacheable(value = "test", key = "'testUser_' + #id")
    public String testExpire(Long id) {
        return "test";
    }

    如果要为上述方法设置过期时间30秒,则配置文件中expires属性的key值一定要指定test,而不是testUser_#id

  • 相关阅读:
    python自动化测试-D9-学习笔记之二(线程锁)
    python习题:封装好的测试报告(report.py)
    python自动化测试-D9-学习笔记之二(守护线程)
    python自动化测试-D9-学习笔记之二(多线程)
    python自动化测试-D9-学习笔记之一(线程池)
    python自动化测试-D9-学习笔记之一(unittest模块)
    python习题:写一个备份数据库的脚本
    python习题:【多线程】有100个数据,启动5个线程,每个线程分20个数据,怎么把这20个数据分别传给每个线程。
    自然语言处理NLTK之入门
    python画一颗拳头大的💗
  • 原文地址:https://www.cnblogs.com/leemz-coding/p/7474221.html
Copyright © 2020-2023  润新知