Cache系列:spring-cache简单三步快速应用ehcache3.x-jcache缓存(spring4.x)

前言:本项目基于spring4.x构建,使用ehcache3.5.2和JCache(jsr107规范)

一、依赖

    除了ehcache和cache-api外,注意引用spring-context-support

  1.                    
  2.                     <dependency>
  3.                         <groupId>org.springframework</groupId>
  4.                         <artifactId>spring-context-support</artifactId>
  5.                         <version>4.3.16.RELEASE</version>
  6.                     </dependency>
  7.     <dependency>
  8.     <groupId>org.ehcache</groupId>
  9.     <artifactId>ehcache</artifactId>
  10.     <version>3.5.2</version>
  11. </dependency>
  12. <dependency>
  13.     <groupId>javax.cache</groupId>
  14.     <artifactId>cache-api</artifactId>
  15.     <version>1.0.0</version>
  16. </dependency>

 

二、配置

1、ehcache配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ehcache:config
  3. xmlns:ehcache="http://www.ehcache.org/v3"
  4. xmlns:jcache="http://www.ehcache.org/v3/jsr107">
  5.  
  6. <ehcache:service>
  7. <jcache:defaults>
  8. <jcache:cache name="default" template="myDefaultTemplate"/>
  9. </jcache:defaults>
  10. </ehcache:service>
  11.  
  12. <ehcache:cache alias="allCameraCache">
  13. <ehcache:key-type copier="org.ehcache.impl.copy.SerializingCopier">java.lang.String</ehcache:key-type>
  14. <ehcache:value-type copier="org.ehcache.impl.copy.SerializingCopier">java.lang.String</ehcache:value-type>
  15. <ehcache:expiry>
  16. <ehcache:tti unit="minutes">20</ehcache:tti><!-- 数据过期时间20分钟 -->
  17. </ehcache:expiry>
  18. <ehcache:heap unit="entries">200</ehcache:heap><!-- 最多缓存200个对象 -->
  19. </ehcache:cache>
  20.  
  21. <!-- 使用模板,可以覆盖模板的属性 -->
  22. <ehcache:cache alias="cameraCache" uses-template="myDefaultTemplate">
  23. <ehcache:key-type>java.lang.Object</ehcache:key-type>
  24. <ehcache:value-type>java.lang.Object</ehcache:value-type>
  25. <ehcache:expiry>
  26. <ehcache:tti unit="minutes">30</ehcache:tti><!-- 数据过期时间30分钟,覆盖模板默认属性 -->
  27. </ehcache:expiry>
  28. <ehcache:heap unit="entries">500</ehcache:heap><!-- 最多缓存500个对象 -->
  29. </ehcache:cache>
  30.  
  31. <!-- 默认模板 -->
  32. <ehcache:cache-template name="myDefaultTemplate">
  33. <ehcache:expiry>
  34. <ehcache:none/><!-- 缓存永不过期 -->
  35. </ehcache:expiry>
  36. </ehcache:cache-template>
  37.  
  38. </ehcache:config>

 

2、spring配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:cache="http://www.springframework.org/schema/cache"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  8. http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
  9. <!--eguid博客地址:http://bog.eguid.cc-->
  10. <cache:annotation-driven cache-manager="cacheManager" /><!--扫描cache注解,如果已有可以不要-->
  11. <context:component-scan base-package="cc.eguid.cache" /><!--扫描路径 -->
  12. <!-- jcache缓存 -->
  13. <bean id="jCacheManager" class="org.springframework.cache.jcache.JCacheManagerFactoryBean">
  14.     <property name="cacheManagerUri" value="classpath:config/ehcache.xml" /> <!--改成配置文件对应的路径-->
  15. </bean>
  16. <bean id="cacheManager" class="org.springframework.cache.jcache.JCacheCacheManager">
  17. <property name="cacheManager" ref="jCacheManager" />
  18. </bean>
  19. </beans>

 

三、使缓存生效

1、注解方式使用

@Cacheable(value="cameraCache",key="#userid")

public String getCameraList(String userid,Integer otherparam) {

...

}

spring-cache的注解比较简单就不再赘述了。

 

posted @ 2018-09-18 09:34  eguid  阅读(2803)  评论(0编辑  收藏  举报