前言:本项目基于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的注解比较简单就不再赘述了。

最新文章

  1. jsp 中 有没有类似java if else语句
  2. sql 语句 事务
  3. PHP &amp; Delphi 語法
  4. java内存模型及GC原理
  5. SCU3185 Black and white(二分图最大点权独立集)
  6. 监控服务器JVM内存运行
  7. sql语句如何获得当前日期
  8. wpf DataGrid 双击获取当前行的控件
  9. C#基础总复习01
  10. Oracle外部表详解(转载)
  11. maven项目显示红叉的解决方法
  12. 电脑硬件扫盲--CPU 显卡
  13. 【转载】VC++中的图像类型转换--使用开源CxImage类库
  14. iOS通过URL Scheme启动app(收集了常用的app的URL Scheme)
  15. Js把IE COM数组列表转换成数组
  16. 线程&amp;进程&amp;协程
  17. [Go] golang互斥锁mutex
  18. UVA818-Cutting Chains(二进制枚举+dfs判环)
  19. pycharm开发工具,使用
  20. 第二章(java程序设计)第三章(语言基础)

热门文章

  1. ubuntu16.04--在标题栏显示网速
  2. jdbc 简单连接
  3. Tomcat安装与IDEA中的配置
  4. 【Python】selenium调用IE11浏览器,报错“找不到元素”NoSuchWindowException: Message:Unable to find element on closed window
  5. 深入Asyncio(三)Asyncio初体验
  6. redis问题接囧办法及经验
  7. access变转换为mysql表工具
  8. iOS 应用发布
  9. VSCode 配置python
  10. Python 单元测试 之setUP() 和 tearDown()