zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    图像处理之基础---图像缩放中的一些 灰度插值算法
    多媒体开发之---h264 取流解码分析
    校验算法之二进制反码求和
    c++学习笔记之基础---类内声明函数后在类外定义的一种方法
    图像增强---中值滤波
    阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第4节 多态_19_使用多态的好处
    阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第4节 多态_18_多态中成员方法的使用特点
    阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第4节 多态_17_多态中成员变量的使用特点
    阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第4节 多态_16_多态的格式与使用
    阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第4节 多态_15_多态的概述
  • 原文地址:https://www.cnblogs.com/leemz-coding/p/7474221.html
Copyright © 2011-2022 走看看