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.springframeworkgroupId>
  4.                          <artifactId>spring-context-supportartifactId>
  5.                          <version>4.3.16.RELEASEversion>
  6.                      dependency>
  7.      <dependency>
  8.      <groupId>org.ehcachegroupId>
  9.      <artifactId>ehcacheartifactId>
  10.      <version>3.5.2version>
  11. dependency>
  12. <dependency>
  13.      <groupId>javax.cachegroupId>
  14.      <artifactId>cache-apiartifactId>
  15.      <version>1.0.0version>
  16. dependency>

 

二、配置

1、ehcache配置

  1. "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.Stringehcache:key-type>
  14. <ehcache:value-type copier="org.ehcache.impl.copy.SerializingCopier">java.lang.Stringehcache:value-type>
  15. <ehcache:expiry>
  16. <ehcache:tti unit="minutes">20ehcache:tti>
  17. ehcache:expiry>
  18. <ehcache:heap unit="entries">200ehcache:heap>
  19. ehcache:cache>
  20.  
  21. <ehcache:cache alias="cameraCache" uses-template="myDefaultTemplate">
  22. <ehcache:key-type>java.lang.Objectehcache:key-type>
  23. <ehcache:value-type>java.lang.Objectehcache:value-type>
  24. <ehcache:expiry>
  25. <ehcache:tti unit="minutes">30ehcache:tti>
  26. ehcache:expiry>
  27. <ehcache:heap unit="entries">500ehcache:heap>
  28. ehcache:cache>
  29.  
  30. <ehcache:cache-template name="myDefaultTemplate">
  31. <ehcache:expiry>
  32. <ehcache:none/>
  33. ehcache:expiry>
  34. ehcache:cache-template>
  35.  
  36. ehcache:config>

 

2、spring配置

  1. "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. <cache:annotation-driven cache-manager="cacheManager" />
  10. <context:component-scan base-package="cc.eguid.cache" />
  11. <bean id="jCacheManager" class="org.springframework.cache.jcache.JCacheManagerFactoryBean">
  12.      <property name="cacheManagerUri" value="classpath:config/ehcache.xml" />
  13. bean>
  14. <bean id="cacheManager" class="org.springframework.cache.jcache.JCacheCacheManager">
  15. <property name="cacheManager" ref="jCacheManager" />
  16. bean>
  17. beans>

 

三、使缓存生效

1、注解方式使用

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

public String getCameraList(String userid,Integer otherparam) {

...

}

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

 

你可能感兴趣的:(Cache系列:spring-cache简单三步快速应用ehcache3.x-jcache缓存(spring4.x))