Cache系列:spring-cache简单三步快速应用ehcache3.x-jcache缓存(spring4.x)
前言:本项目基于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的注解比较简单就不再赘述了。
Cache系列:spring-cache简单三步快速应用ehcache3.x-jcache缓存(spring4.x)的更多相关文章
- spring-data详解之spring-data-jpa:简单三步快速上手spring-data-jpa开发
前言: 基于spring framework 4.x或spring boot 1.x开发环境 务必注意以下版本问题:Spring framework4.x(Spring boot1.x)对应sprin ...
- 简单三步快速学会使用Mybatis-Generator自动生成entity实体、dao接口以及mapper映射文件(postgre使用实例)
前言: mybatis-generator是根据配置文件中我们配置的数据库连接参数自动连接到数据库并根据对应的数据库表自动的生成与之对应mapper映射(比如增删改查,选择性增删改查等等简单语句)文件 ...
- Hexo + Serverless Framework,简单三步搭建你的个人博客
很多人都想拥有自己的个人博客,还得看起来漂亮.酷酷的.尤其对开发者来说,不仅可以分享技术(装)心得(逼),面试的时候还能成为加分.这里介绍两款好用的神器,不用忙前(前端)忙后(后端),简单3min即可 ...
- 简单三步同步你的 VSCode 用户配置
https://www.cnblogs.com/knight-errant/p/10444777.html 设备重装,换设备,VSCode 又要重新配置了?不不不,简单三步,让你的 VSCode 配置 ...
- 10分钟系列:NetCore3.1+EFCore三步快速完成数据库交互
前言 做程序开发,不管是什么语言什么数据库,其中的ORM(对象关系映射)是必不可少的,但是不管选择哪一种ORM,都需要了解其中的运行机制,配置帮助类等等. 所以很多ORM都开始进行升级封装,我们只需要 ...
- 只需三步 快速完善网站Sitemap
越来越多的SEOer把优化的重点放在了站内优化上,细心的朋友应该查看一些前辈的robots.txt的时候不难发现,他们的robots中都加 入了一句Sitemap: http://www.dewang ...
- 简单三步-实现dede站内搜索功能
第一步:找到对应的搜索模板的代码 我们都知道,dede有自带的搜索功能,我们只要找到对应的模板,然后把我们想要的代码拿出来就行了.具体如下: 首先进入templets-->default--&g ...
- Hibernate实体映射配置(XML)简单三步完美配置
我们在使用Hibernate框架的时候,非常纠结的地方就是实体和表之间的映射,今天借助汤老师的思路写了小教程,以后配置不用纠结了! 第一步:写注释 格式为:?属性,表达的是本对象与?的?关系. 例:“ ...
- 简单两步快速实现shiro的配置和使用,包含登录验证、角色验证、权限验证以及shiro登录注销流程(基于spring的方式,使用maven构建)
前言: shiro因为其简单.可靠.实现方便而成为现在最常用的安全框架,那么这篇文章除了会用简洁明了的方式讲一下基于spring的shiro详细配置和登录注销功能使用之外,也会根据惯例在文章最后总结一 ...
随机推荐
- spring源码解析之IOC容器(一)
学习优秀框架的源码,是提升个人技术水平必不可少的一个环节.如果只是停留在知道怎么用,但是不懂其中的来龙去脉,在技术的道路上注定走不长远.最近,学习了一段时间的spring源码,现在整理出来,以便日后温 ...
- linux SPI驱动——spidev之deive(五)
1.定义board设备 1: struct spi_board_info { 2: /* the device name and module name are coupled, like platf ...
- pycharm 5 注册码
BIG3CLIK6F-eyJsaWNlbnNlSWQiOiJCSUczQ0xJSzZGIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiI ...
- 1930: [Shoi2003]pacman 吃豆豆
1930: [Shoi2003]pacman 吃豆豆 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1969 Solved: 461[Submit][ ...
- EasyPlayer windows RTSP播放器OCX插件使用说明
鉴于大家对于EasyPlayer插件的使用还不太熟悉,特此写一篇插件的使用文档,供大家参考:EasyPlayer插件有两种,一种是基于IE的ActiveX控件,一种是基于FireFox(也支持多浏览器 ...
- MLGBZ
April cloudy, boss rainy, told me he want to kick But coming so,Formosa Heart sad , Down,down,down W ...
- 用c++后缀自动机实现最大公共字符串算法,并封装成Python库
后缀自动机的C++代码转自https://e-maxx.ru/algo/suffix_automata,其余封装为自写. 在C++文件同级目录建立setup.py文件,代码如下: # !/usr/bi ...
- debug x86 汇编程序指南
--------------------------------------------------------------------------------------------------- ...
- Java for LeetCode 130 Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- Allegro PCB中添加汉字
注明出处:http://www.cnblogs.com/einstein-2014731/p/5650943.html Cadence用起来比AltiumDesigner要麻烦些,但是也更开放,更灵活 ...