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详细配置和登录注销功能使用之外,也会根据惯例在文章最后总结一 ...
随机推荐
- oracle中视图v$sql的用途
1.获取正在执行的sql语句.sql语句的执行时间.sql语句的等待事件: select a.sql_text,b.status,b.last_call_et,b.machine,b.event,b. ...
- (4.5.4)Android測试TestCase单元(Unit test)測试和instrumentationCase单元測试
Android单元和instrumentation单元測试 Developing Android unit and instrumentation tests Android的单元測试是基于JUnit ...
- Unix中库的使用
库有点像java中的jar包,但是使用起来要比jar包要麻烦一点. 库分为静态编程库和动态链接库两种. 库一旦设计出来就需要被可执行程序链接和调用. 可执行程序在编译时直接载入静态编程库,在运行时直接 ...
- 【Selenium + Python】路径报错之OSError: [Errno 22] Invalid argument: './t/report/2018-03-23_11:03:12_report.html'
现象: 此问题真的是太痛苦了,查了好多资料是说路径的问题,结果还是报错,后来一点点的排查才发现原来是!!!!!! 废话不多说上原来代码: if __name__ == '__main__': star ...
- android -volley-请求数据
private List<gson.DataBean>arrGson;//请求的数据 //请求数据的方法 public void initData() { RequestQueue mQu ...
- python 基础 4.3 高阶函数下和匿名函数
一 .匿名函数 顾名思议就是没有名字的函数,那为什么要设立匿名函数,他有什么作用呢?lambda 函数就是一种快速定义单行的最小函数,可以用在任何需要函数的地方. 常规版: def fun(x,y ...
- 微信URL有效性验证
1.填写配置项:填写 URL 和 Token 点击提交按钮 微信服务器会以get方式请求到所指定的URL,在此URL中进行URL的有效性验证 2.URL有效性的验证: if ("get&q ...
- process_thread_action
import psycopg2 import threading conn_fmac = psycopg2.connect(database='filter_useless_mac', user='u ...
- Linux 日志命令
当日志文件存储日志很大时,我们就不能用vi直接进去查看日志,需要Linux的命令去完成我们的查看任务 Log位置: /var/log/message 系统启动后的信息和错误日志,是Red Hat Li ...
- sql性能分析语句
SELECT creation_time N'语句编译时间' ,last_execution_time N'上次执行时间' ,total_physical_reads N'物理读取总次数' ,tota ...