企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
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