spring mvc自帶了一個簡易的緩存
首先需要在spring的配置文件設置
~~~
<!-- 緩存設置 -->
<!-- 啟用緩存注解功能(請將其配置在Spring主配置文件中) -->
<cache:annotation-driven cache-manager="cacheManager"/>
<!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap實現的緩存管理器(該功能是從Spring3.1開始提供的) -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean id="ownCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/>
</set>
</property>
</bean>
~~~
然后在需要緩存的方法加上注釋
~~~
package server;
import org.springframework.cache.annotation.Cacheable;
public class T {
@Cacheable(value="ownCache", key="'t2'")
public String getData(){
System.out.print(123);
return "ok";
}
}
~~~
然后需要注意的:
1. value是必須的,與配置文件的bean一樣(org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean)
2. key就是cache的key
3. 緩存的方法不能與調用它的方法在同一個類
依賴的包有:
spring-aop-4.2.4.RELEASE.jar
spring-aspects-4.2.4.RELEASE.jar
aopalliance-1.0.jar