Ehcache学习总结(2)--Ehcache整合spring配置
首先需要的maven依赖为:
- <!--ehcache-->
- <dependency>
- <groupId>com.googlecode.ehcache-spring-annotations</groupId>
- <artifactId>ehcache-spring-annotations</artifactId>
- <version>${ehcache-spring.version}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>net.sf.ehcache</groupId>
- <artifactId>ehcache-core</artifactId>
- <version>${ehcache-core.version}</version>
- </dependency>
spring的maven依赖就不在展示出来。
ehcache需要一个配置文件,为ehcache.xml,内容为:
- <?xml version="1.0" encoding="UTF-8"?>
- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
- <diskStore path="java.io.tmpdir" /> <!-- 缓存存放目录(此目录为放入系统默认缓存目录),也可以是”D:/cache“ java.io.tmpdir -->
- <defaultCache
- maxElementsInMemory="10000"
- eternal="false"
- timeToIdleSeconds="120"
- timeToLiveSeconds="120"
- overflowToDisk="true"
- maxElementsOnDisk="10000000"
- diskPersistent="false"
- diskExpiryThreadIntervalSeconds="120"
- memoryStoreEvictionPolicy="LRU"
- />
- <!--
- name:Cache的唯一标识
- maxElementsInMemory:内存中最大缓存对象数
- maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大
- eternal:Element是否永久有效,一但设置了,timeout将不起作用
- overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中
- timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大
- timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大
- diskPersistent:是否缓存虚拟机重启期数据
- diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒
- diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区
- memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)
- -->
- </ehcache>
在spring的applicationContext.xml的配置文件的编写为:
- <!-- 引用ehCache的配置 -->
- <bean id="defaultCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
- <property name="configLocation">
- <value>classpath:/ehcache.xml</value>
- </property>
- </bean>
- <!-- 定义ehCache的工厂,并设置所使用的Cache name -->
- <bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
- <property name="cacheManager">
- <ref local="defaultCacheManager"/>
- </property>
- <property name="cacheName">
- <value>DEFAULT_CACHE</value>
- </property>
- </bean>
如此配置,基本spring整合ehcache就完成了,但还是要单独测试一下,编写一个测试程序:
- package cn.com.ecache;
- import cn.com.container.ServiceProvinder;
- import net.sf.ehcache.Cache;
- import net.sf.ehcache.Element;
- import org.junit.Test;
- /**
- * Created by Administrator on 2016/1/24.
- */
- public class TestEcache {
- @Test
- public void Test() {
- Cache cache = (Cache) ServiceProvinder.getService("ehCache");
- Element lgElement = new Element("loginName", "xiaxuan");
- Element pwElement = new Element("password", "xiaxuan");
- cache.put(lgElement);
- cache.put(pwElement);
- System.out.println(cache.get("loginName"));
- }
- }
测试结果为:
既可以存放数据到缓存中,又可以从缓存中拿到数据,spring和ehcache的整合基本成功。
如上,就是spring和ehcache整合。
Ehcache学习总结(2)--Ehcache整合spring配置的更多相关文章
- Ehcache学习总结(3)--Ehcache 整合Spring 使用页面、对象缓存
Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式 ...
- Ehcache整合spring配置,配置springMVC缓存
为了提高系统的运行效率,引入缓存机制,减少数据库访问和磁盘IO.下面说明一下ehcache和spring整合配置. 1. 需要的jar包 slf4j-api-1.6.1.jar ehcache-c ...
- Ehcache整合spring配置
为了提高系统的运行效率,引入缓存机制,减少数据库访问和磁盘IO.下面说明一下ehcache和spring整合配置. 1. 需要的jar包 slf4j-api-1.6.1.jar ehcache-c ...
- 终极版:Mybatis整合Spring配置
第一部分:配置 Spring 框架 配置 SpringMVC 的步骤: 配置流程图: SpringMVC 配置 1. 导入包(那些包,基本包 5 个,1 个日志依赖包,2 个 webmvc 支持包) ...
- Mybatis学习笔记(八) —— Mybatis整合spring
一.整合思路 1.SqlSessionFactory对象应该放到spring容器中作为单例存在. 2.传统dao的开发方式中,应该从spring容器中获得sqlsession对象. 3.Mapper代 ...
- Ehcache学习总结(1)--Ehcache入门介绍
Ehcache是现在最流行的纯Java开源缓存框架,配置简单.结构清晰.功能强大,最初知道它,是从hibernate的缓存开始的.网上中文的EhCache材料以简单介绍和配置方法居多,如果你有这方面的 ...
- ssm整合——Spring配置(2)
配置Spring 1. 环境准备 使用之前搭建Mabatis的环境 1.1 新建目录 新建spring的service业务逻辑包 在resources目录下新建spring的配置文件:applicat ...
- Ehcache 整合Spring 使用页面、对象缓存
Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一 般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布 ...
- (转)Ehcache 整合Spring 使用页面、对象缓存
Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...
随机推荐
- shell编程笔记1
参考文章:1 http://blog.csdn.net/wuwenxiang91322/article/details/9259877 通过chmod改变文件权限 补充知识: 1Linux文件的三 ...
- SQL中一次插入多条数据
SQL中insert一次可以插入一条数据,我们有三种方法可以一次性插入多条数据. 1. 语法:select 字段列表 into 新表 from 源表 注意事项:此种方法新表是系统自动创建,语句执行前不 ...
- class-metaclass-Class vs. type
In some languages, classes are only a compile-time feature (new classes cannot be declared at runtim ...
- 优动漫 PAINT 导航窗口面板
导航窗口面板用于管理画布的视图.本节将介绍如何在其中缩放.旋转.翻转画布. 如何使用导航窗口面板 使用[导航窗口]面板便于把握整个画布,以下将为您详细介绍. 什么是[导航窗口]面板 [导航窗口]面板用 ...
- SAI / PS绘画一个卡通女孩详解
本教程介绍使用SAI / PS绘画一个卡通女孩的教程 ,教程很详细,动起你的小手一起来试试吧! 软件下载:http://www.dongmansoft.com/xiazai.html 想要Get到更多 ...
- matplotlib bar函数重新封装
参考: https://blog.csdn.net/jenyzhang/article/details/52047557 https://blog.csdn.net/liangzuojiayi/art ...
- CentOS7 部署SVN服务器
服务器端:svnserver 安装主要步骤 yum install subversion rpm -ql subversion mkdir /application/svndata mkdir /ap ...
- Springboot+swagger2的接口文档开发
一.创建一个SpringBoot项目 1. 2. 3. 4. 把web里的web选中,SQL里选择自己需要的,点击next 二.创建各项所需的controller,configure等 1. 项目布局 ...
- (QT)在命令行编译ui文件和程序
1.新建helloworld_2文件夹,将helloworld里的main.cpp和hellodialog.cpp两个文件复制过来. 2.打开控制台.此时不能用cmd,否则不能出最后的结果(lz在运行 ...
- dos2unix和unix2dos命令使用【转】
dos2unix, unix2dos 用来实现 DOS <=> UNIX text file 转换 aptitude install sysutils 行末: DOS 格式 0d 0a U ...
