Cache系列:spring-cache简单三步快速应用ehcache3.x-jcache缓存(spring4.x)
2024-09-08 08:03:58
前言:本项目基于spring4.x构建,使用ehcache3.5.2和JCache(jsr107规范)
一、依赖
除了ehcache和cache-api外,注意引用spring-context-support
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>4.3.16.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.ehcache</groupId>
- <artifactId>ehcache</artifactId>
- <version>3.5.2</version>
- </dependency>
- <dependency>
- <groupId>javax.cache</groupId>
- <artifactId>cache-api</artifactId>
- <version>1.0.0</version>
- </dependency>
二、配置
1、ehcache配置
- <?xml version="1.0" encoding="UTF-8"?>
- <ehcache:config
- xmlns:ehcache="http://www.ehcache.org/v3"
- xmlns:jcache="http://www.ehcache.org/v3/jsr107">
- <ehcache:service>
- <jcache:defaults>
- <jcache:cache name="default" template="myDefaultTemplate"/>
- </jcache:defaults>
- </ehcache:service>
- <ehcache:cache alias="allCameraCache">
- <ehcache:key-type copier="org.ehcache.impl.copy.SerializingCopier">java.lang.String</ehcache:key-type>
- <ehcache:value-type copier="org.ehcache.impl.copy.SerializingCopier">java.lang.String</ehcache:value-type>
- <ehcache:expiry>
- <ehcache:tti unit="minutes">20</ehcache:tti><!-- 数据过期时间20分钟 -->
- </ehcache:expiry>
- <ehcache:heap unit="entries">200</ehcache:heap><!-- 最多缓存200个对象 -->
- </ehcache:cache>
- <!-- 使用模板,可以覆盖模板的属性 -->
- <ehcache:cache alias="cameraCache" uses-template="myDefaultTemplate">
- <ehcache:key-type>java.lang.Object</ehcache:key-type>
- <ehcache:value-type>java.lang.Object</ehcache:value-type>
- <ehcache:expiry>
- <ehcache:tti unit="minutes">30</ehcache:tti><!-- 数据过期时间30分钟,覆盖模板默认属性 -->
- </ehcache:expiry>
- <ehcache:heap unit="entries">500</ehcache:heap><!-- 最多缓存500个对象 -->
- </ehcache:cache>
- <!-- 默认模板 -->
- <ehcache:cache-template name="myDefaultTemplate">
- <ehcache:expiry>
- <ehcache:none/><!-- 缓存永不过期 -->
- </ehcache:expiry>
- </ehcache:cache-template>
- </ehcache:config>
2、spring配置
- <?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:context="http://www.springframework.org/schema/context"
- xmlns:cache="http://www.springframework.org/schema/cache"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
- <!--eguid博客地址:http://bog.eguid.cc-->
- <cache:annotation-driven cache-manager="cacheManager" /><!--扫描cache注解,如果已有可以不要-->
- <context:component-scan base-package="cc.eguid.cache" /><!--扫描路径 -->
- <!-- jcache缓存 -->
- <bean id="jCacheManager" class="org.springframework.cache.jcache.JCacheManagerFactoryBean">
- <property name="cacheManagerUri" value="classpath:config/ehcache.xml" /> <!--改成配置文件对应的路径-->
- </bean>
- <bean id="cacheManager" class="org.springframework.cache.jcache.JCacheCacheManager">
- <property name="cacheManager" ref="jCacheManager" />
- </bean>
- </beans>
三、使缓存生效
1、注解方式使用
@Cacheable(value="cameraCache",key="#userid")
public String getCameraList(String userid,Integer otherparam) {
...
}
spring-cache的注解比较简单就不再赘述了。
最新文章
- jsp 中 有没有类似java if else语句
- sql 语句 事务
- PHP &; Delphi 語法
- java内存模型及GC原理
- SCU3185 Black and white(二分图最大点权独立集)
- 监控服务器JVM内存运行
- sql语句如何获得当前日期
- wpf DataGrid 双击获取当前行的控件
- C#基础总复习01
- Oracle外部表详解(转载)
- maven项目显示红叉的解决方法
- 电脑硬件扫盲--CPU 显卡
- 【转载】VC++中的图像类型转换--使用开源CxImage类库
- iOS通过URL Scheme启动app(收集了常用的app的URL Scheme)
- Js把IE COM数组列表转换成数组
- 线程&;进程&;协程
- [Go] golang互斥锁mutex
- UVA818-Cutting Chains(二进制枚举+dfs判环)
- pycharm开发工具,使用
- 第二章(java程序设计)第三章(语言基础)
热门文章
- ubuntu16.04--在标题栏显示网速
- jdbc 简单连接
- Tomcat安装与IDEA中的配置
- 【Python】selenium调用IE11浏览器,报错“找不到元素”NoSuchWindowException: Message:Unable to find element on closed window
- 深入Asyncio(三)Asyncio初体验
- redis问题接囧办法及经验
- access变转换为mysql表工具
- iOS 应用发布
- VSCode 配置python
- Python 单元测试 之setUP() 和 tearDown()