一、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"
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-3.2.xsd">
<context:annotation-config/>
<!--缓存 20秒过期 初始化容量100-->
<cache:annotation-driven/>
<bean id="cacheManager" class="org.springframework.cache.guava.GuavaCacheManager">
<property name="cacheSpecification" value="expireAfterWrite=20s,initialCapacity=100"/>
</bean>
</beans>
二、使用
说明:p0代表第一个参数,后面的为参数属性。如果第二次请求的参数中的三个属性与上一次相同,那么就会被缓存(在有效期内)
@RequestMapping(value = "/query/hisen", method = RequestMethod.GET)
@ResponseBody
@Cacheable(value = "hisen", key = "#p0.name + #p0.age + #p0.gender")
public ResultBean<List<QueryRes>> queryHisen(@ModelAttribute QueryReq req) {
//someThing
}