Guava缓存

参考:https://blog.csdn.net/chinabestchina/article/details/78009220

1. pom文件


<!--  除了spring core/beans/context 等,必须要引入的依赖 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.3.6.RELEASE</version>
</dependency>

2. spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns="http://www.springframework.org/schema/beans"
       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">

    <!--开启缓存注解-->
    <cache:annotation-driven/>

    <!-- 扫描包路径 -->
    <context:component-scan base-package="com.test.dao"/>

    <bean id="cacheManager" class="org.springframework.cache.guava.GuavaCacheManager">
        <property name="cacheSpecification" value="expireAfterWrite=3600s"/>
        <property name="cacheNames">
            <list>
                <value>myCache</value>
            </list>
        </property>
    </bean>

</beans>

3. 使用

package com.test.dao;

public interface DemoMapper {
    @Cacheable(value = "myCache", key = "#p0.name + '_' + #p0.age", unless="#result == null")
    DemoVo queryDemo(Request request);
}

注意:在接口上使用注解时,必须使用 #p0 #p1 等取参数,使用参数名(#request)无法取到属性值,会报错。普通类里的方法两种都可以。

unless="#result == null" 表示结果为空时不缓存。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容