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详细配置和登录注销功能使用之外,也会根据惯例在文章最后总结一 ...
随机推荐
- Android网络编程Socket【实例解析】
Socket 事实上和JavaWeb 里面的Socket一模一样 建立客服端,server端,server开一个port供客服端訪问 第一步创建server端:(这里把为了便于解说.把server端, ...
- Android 属性动画框架 ObjectAnimator、ValueAnimator ,这一篇就够了
前言 我们都知道 Android 自带了 Roate Scale Translate Alpha 多种框架动画,我们可以通过她们实现丰富的动画效果,但是这些宽家动画却有一个致命的弱点,它们只是改变了 ...
- linearLayout 和 relativeLayout的属性区别
LinearLayout和RelativeLayout 共有属性: java代码中通过btn1关联次控件 android:id="@+id/btn1" 控件宽度 android:l ...
- sealed,new,virtual,abstract与override关键字的区别?
1. sealed——“断子绝孙” 密封类不能被继承.密封方法可以重写基类中的方法,但其本身不能在任何派生类中进一步重写.当应用于方法或属性时,sealed修饰符必须始终与override一起使用. ...
- jQuery入门知识点
<精通ASP.NET MVC3框架>第20章 1.jQuery文件jquery-1.5.1.js:jquey核心库常规版jquery-1.5.1.min.js:jquery核心库最小化版j ...
- C# 杀掉后台进程
var p = Process.GetProcessesByName("WINWORD"); if (p.Any()) { for (int i = 0; i < p.Len ...
- Ubuntu/CentOS下编译Nginx最基本参数
Ubuntu/CentOS下编译Nginx安装基本参数,做个记录: groupadd www useradd -g www www ./configure --user=www --group=www ...
- PeekMessage究竟做了什么?
1.UI线程 2.工作线程 把Delphi里TThread的WaitFor函数转化成C++代码,就会是下面这个样子. BOOL TThread::WaitFor(HANDLE hThread) { M ...
- Git --更改远程分支名
git更新远程分支名字 git checkout old_branch git branch -m old_branch new_branch git push --delete origin old ...
- 一种流量成本节省60%以上的手机直播微信直播H5直播幼儿园直播方案
前言 近几年视频直播可以说是非常火热,EasyDarwin也非常受开发者的欢迎,不仅仅是主播火了,而且各种商业直播也火了起来:会场直播.宴会直播.讲座直播.景区直播.后厨直播.课堂直播.幼儿园直播等等 ...