spring配置ConcurrentMap实现缓存
2024-08-31 09:54:27
spring本身内置了对Cache的支持,本次记录的是基于Java API的ConcurrentMap的CacheManager配置。
1、xml文件中增加命名空间
<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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,有一个cache-manager属性用来指定当前所使用的CacheManager对应的bean的名称,默认是cacheManager -->
<cache:annotation-driven/>
</beans>
<cache:annotation-driven/>有一个cache-manager属性用来指定当前所使用的CacheManager对应的bean的名称,默认是cacheManager
2、配置CacheManager
CacheManager是Spring定义的一个用来管理Cache的接口。Spring自身已经为我们提供了两种CacheManager的实现。一种是基于Java API的ConcurrentMap,另一种是基于第三方Cache实现--EhCache.。
基于ConcurrentMap的配置
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
<!--xxxx对应@Cacheable中的value值-->
<property name="name" value="xxxx"/>
</bean>
</set>
</property>
</bean>
完成以上步骤后,可进行测试下。
第一次调用时,走其中的方法,第二次不走其中方法,直接从缓存中抽取。
最新文章
- 数据库热备份工具innobackupex的安装
- Handler机制来处理子线程去更新UI线程控件
- 【mysql5.6】数据类型
- Mac显示和隐藏文件的命令
- 转】Mahout分步式程序开发 基于物品的协同过滤ItemCF
- Hibernate 二级缓存 总结整理(转)
- As3 里的正则相关
- HDU 1394 Minimum Inversion Number (线段树 单点更新 求逆序数)
- kendo ui 单击取消编辑数据grid减少的原因和治疗方法的数据
- linux里的那么点东西(持续更新)
- 【openstack N版】——摘除一个计算节点
- 文件描述符与FILE
- RABBITMQ too many heartbeats missed
- ASP.NET Web API 2 之文件下载
- centos 7 添加中文输入法
- Python基础(函数部分)-day04
- PAT 乙级 1069 微博转发抽奖(20) C++版
- jq给单选框 radio添加或删除选中状态
- HDU 4532 湫秋系列故事——安排座位 (组合+DP)
- Dubbo原码解析(version:2.5.3)
热门文章
- 深入浅出 java.String
- -bash: fultter: command not found
- 负载均衡配置篇(Nginx)
- Python—使用列表构造栈数据结构
- Shell语法 【if while for】
- Acunetix WVS安全测试软件使用教程(入门级)
- 实现迭代器(\_\_next\_\_和\_\_iter\_\_)
- BTree
- jquery预加载的几种例子
- 函数(Python)