spring-cache并不是一个缓存框架的具体实现,仅仅是一个缓存框架的抽象,同时又默认对接了几种第三方的缓存实现。如java本身的ConcurrentMap、ehcache、guava cache等。
下面是一个spring-cache的简单实现。
因为spring-cache默认是基于AOP的方式去进行缓存处理的,所以几个缓存的注解@Cacheable@CacheEvict@CachePut必须放在实现类上,并且该类被spring代理了,而且只能在被外部调用时才有效,如果对象内部调用,默认设置下该缓存是无效的。如果需要对象内部调用,spring提供了参数配置。
现在来简单配置一个spring-cache。
1、首先maven的依赖
springcache在spring-context中,所以不需要额外的jia包了。
2、建立一个可以被spring加载的xml文件,文件内容如下
springcache简单配置 写道
<?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: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/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd"
>
<description>spring cache配置</description>
<!-- 如果没有其他配置文件扫描缓存所在的包,请添加该扫描 -->
<!-- <context:component-scan base-package="com.scm.base.cache" /> -->
<!-- 基于注解的驱动,默认使用基于接口的动态代理,如果要基于类则修改proxy-target-class="true",这样缓存就支持对象内部的调用 -->
<cache:annotation-driven/>
<!-- 定义cache管理,简单使用java的concurrentMap来作为简单缓存的实现,请注意缓存中使用的缓存名字必须和这里配置的名字匹配 -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" >
<property name="name" value="default"></property>
</bean>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
<property name="name" value="allDic"></property>
</bean>
</set>
</property>
</bean>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd"
>
<description>spring cache配置</description>
<!-- 如果没有其他配置文件扫描缓存所在的包,请添加该扫描 -->
<!-- <context:component-scan base-package="com.scm.base.cache" /> -->
<!-- 基于注解的驱动,默认使用基于接口的动态代理,如果要基于类则修改proxy-target-class="true",这样缓存就支持对象内部的调用 -->
<cache:annotation-driven/>
<!-- 定义cache管理,简单使用java的concurrentMap来作为简单缓存的实现,请注意缓存中使用的缓存名字必须和这里配置的名字匹配 -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" >
<property name="name" value="default"></property>
</bean>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
<property name="name" value="allDic"></property>
</bean>
</set>
</property>
</bean>
</beans>
3、java类中的使用
/** * 系统字典服务类 */ @Service(value="sysDictionaryService") public class SysDictionarySerivceImpl implements SysDictionarySerivce { private final static Logger logger = LoggerFactory.getLogger(SysDictionarySerivceImpl.class); @Resource private SysDictionaryDao sysDictionaryDao; @Override @Cacheable(value="allDic") public Map<String, List<SysDictionaryItem>> getAllDictionaries() { logger.info(" 开始调用缓存字典 "); Map<String, List<SysDictionaryItem>> resultMap = new LinkedHashMap<String, List<SysDictionaryItem>>(); return resultMap; } }
4、如果从外部调用该方法,第一次会打印"开始调用缓存字典",第二次则会直接返回结果,不进入方法。
5、如果需要对所有的注解和其他更复杂的使用,可以参考spring官方文档中关于cache部分。
6、本例子是基于spring4.0.6如果其他版本有些写法可能不一样。
相关推荐
jar包-spring-modules-cache.jarjar包-spring-modules-cache.jar
spring-modules-cache-0.8.jar
spring-modules-cache.jar spring-modules-cache.jar
spring-cache-4.1.xsd用于spring开发代码提示。用于Java开发。
里面有两个xsd文件,springmodules-ehcache和springmodules-cache.xsd。需求:因为有时候在你在xml中用某个标签时,提示错误,有时候就是少了xsd的引入
通过spring-cache对方法进行缓存,同时验证如何通过key更新指定的缓存!
springboot缓存的一个小项目
java运行依赖jar包
spring-cache-3.5.0工具包,Java开发一般能用上,缓存工具
点智数码--注释驱动的-Spring-cache-缓存的介绍.doc
Spring Boot 整合 Spring-cache:让你的网站速度飞起来
springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot ...
配置applicationContext.xml的xsd文件里面.........................................................................
spring-boot-mybatis-cache-thymeleaf框架组合练习学习做的一个完整的精简的小demo项目源码
Springboot中Spring-cache与redis整合。这也是一个不错的框架,与spring的事务使用类似,只要添加一些注解方法,就可以动态的去操作缓存了,减少代码的操作。
ssm3-mybatis2-memcached 使用了 simple-...ssm3-springcache-mybatis3-memcached 通过 Spring Cache(Spring 3.1+) 实现 simple-spring-memcached 和 MyBatis3 整合。 simple-spring-memcached 使用了 JSON 序列化。
spring-cache-mongodb:基于MongoDB的Spring Cache实现
spring官方完整jar包-4.1.6版
org\springframework\cache org\springframework\context org\springframework\core org\springframework\dao org\springframework\ejb org\springframework\instrument org\springframework\jms org\spring...