下面介绍一下简单使用的配置过程:ehcache.jar及spring相关jar就不说了,加到项目中就是了。

简单的使用真的很简单。但只能做为入门级了。

1.ehcache.xml,可放classpath根目录下,

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect"

         dynamicConfig="true">

<      diskStore path="java.io.tmpdir" />

<defaultCache

            maxElementsInMemory="10000"

            eternal="false"

            timeToIdleSeconds="120"

            timeToLiveSeconds="120"

            overflowToDisk="true"

            diskSpoolBufferSizeMB="30"

            maxElementsOnDisk="10000000"

            diskPersistent="false"

            diskExpiryThreadIntervalSeconds="120"

            memoryStoreEvictionPolicy="LRU"

          

            />

    <cache name="DEFAULT_CACHE"

            maxElementsInMemory="10000"

            eternal="false"

            timeToIdleSeconds="120"

            timeToLiveSeconds="120"

            overflowToDisk="true"

            diskSpoolBufferSizeMB="30"

            maxElementsOnDisk="10000000"

            diskPersistent="false"

            diskExpiryThreadIntervalSeconds="120"

            memoryStoreEvictionPolicy="LRU"

           

            />    

</ehcache>

<!--  

1.必须要有的属性:  

name: cache的名字,用来识别不同的cache,必须惟一。  

maxElementsInMemory: 内存管理的缓存元素数量最大限值。  

maxElementsOnDisk: 硬盘管理的缓存元素数量最大限值。默认值为0,就是没有限制。  

eternal: 设定元素是否持久话。若设为true,则缓存元素不会过期。  

overflowToDisk: 设定是否在内存填满的时候把数据转到磁盘上。  

2.下面是一些可选属性:  

timeToIdleSeconds: 设定元素在过期前空闲状态的时间,只对非持久性缓存对象有效。默认值为0,值为0意味着元素可以闲置至无限长时间。  

timeToLiveSeconds: 设定元素从创建到过期的时间。其他与timeToIdleSeconds类似。  

diskPersistent: 设定在虚拟机重启时是否进行磁盘存储,默认为false.(我的直觉,对于安全小型应用,宜设为true)。  

diskExpiryThreadIntervalSeconds: 访问磁盘线程活动时间。  

diskSpoolBufferSizeMB: 存入磁盘时的缓冲区大小,默认30MB,每个缓存都有自己的缓冲区。  

memoryStoreEvictionPolicy: 元素逐出缓存规则。共有三种,Recently Used
(LRU)最近最少使用,为默认。 First In First Out (FIFO),先进先出。Less Frequently
Used(specified as LFU)最少使用 

-->

2.第二步,配置applicationContext-ehcache.xml,与spring整合文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" 

     xmlns:context="http://www.springframework.org/schema/context" 

     xmlns:p="http://www.springframework.org/schema/p" 

     xmlns:mvc="http://www.springframework.org/schema/mvc" 

     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

     xmlns:aop="http://www.springframework.org/schema/aop" 

     xmlns:tx="http://www.springframework.org/schema/tx" 

     xsi:schemaLocation="http://www.springframework.org/schema/beans 

          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 

          http://www.springframework.org/schema/context 

          http://www.springframework.org/schema/context/spring-context.xsd 

          http://www.springframework.org/schema/tx  

          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 

          http://www.springframework.org/schema/aop 

          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 

          http://www.springframework.org/schema/mvc 

          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"

          default-autowire="byName" default-lazy-init="false"> 

        <!-- 引用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>           

    </beans>

实际上这样就把两者结合起来了。当然集群的话还得另外配置,这里只讲最简单的。

下面使用:

3.  添加数据到缓存:

net.sf.ehcache.Cache ehCache=ApplicationContextUtils.getBean("ehCache");

net.sf.ehcache.Element lgEle=new net.sf.ehcache.Element("loginName", users.getLoginName());

net.sf.ehcache.Element pwdEle=new net.sf.ehcache.Element("password", users.getPassword());

ehCache.put(lgEle);

ehCache.put(pwdEle);

这样使用就可。

Ehcache整合spring的更多相关文章

  1. Ehcache 整合Spring 使用页面、对象缓存

    Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一 般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布 ...

  2. ehcache整合spring本地接口方式

    一.简介 ehcache整合spring,可以通过使用echache的本地接口,从而达到定制的目的.在方法中根据业务逻辑进行判断,从缓存中获取数据或将数据保存到缓存.这样让程序变得更加灵活. 本例子使 ...

  3. Ehcache学习总结(3)--Ehcache 整合Spring 使用页面、对象缓存

    Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式 ...

  4. (转)Ehcache 整合Spring 使用页面、对象缓存

    Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...

  5. Ehcache 整合Spring 使用页面、对象缓存(转载)

    Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...

  6. Ehcache 整合Spring 使用页面、对象缓存(转)

    Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...

  7. Ehcache 整合Spring 使用页面、对象缓存(1)

    转自:http://www.cnblogs.com/hoojo/archive/2012/07/12/2587556.html Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以 ...

  8. Ehcache整合spring配置

    为了提高系统的运行效率,引入缓存机制,减少数据库访问和磁盘IO.下面说明一下ehcache和spring整合配置. 1.   需要的jar包 slf4j-api-1.6.1.jar ehcache-c ...

  9. Ehcache学习总结(2)--Ehcache整合spring配置

    首先需要的maven依赖为: [html] view plain copy <!--ehcache--> <dependency> <groupId>com.goo ...

  10. Ehcache整合spring配置,配置springMVC缓存

    为了提高系统的运行效率,引入缓存机制,减少数据库访问和磁盘IO.下面说明一下ehcache和spring整合配置. 1.   需要的jar包 slf4j-api-1.6.1.jar ehcache-c ...

随机推荐

  1. 解决PHP下载文件时因时文件太大而报404错误

    set_time_limit(0); ini_set('memory_limit', '512M'); header('Content-Type: application/octet-stream') ...

  2. 冒泡排序——PowerShell版

    继续读啊哈磊算法有感系列.上一篇是桶排序,在结尾总结了一下简化版桶排序的缺点.这一篇来说一下冒泡排序,冒泡排序可以很好的克服桶排序的缺点.下面我们先来说说冒泡排序的过程与思想—— 冒泡排序的过程: 第 ...

  3. JavaScript高级 面向对象(9)--深拷贝代码实现

    说明(2017.4.1): 1. 深拷贝要把对象里的“方法”也复制一份出来,“方法”里的“方法和属性”再判断深浅进行拷贝. 2. 办法就是写一个函数deepCopy,里面判断深浅拷贝,然后每个对象都添 ...

  4. 传智播客《巴巴运动网视频教程(11-106)》avi格式以及兴许44集视频包括所有源码和资源

    (1)网上找巴巴运动网代码资源的时候找了非常久 基本上都是须要各种积分的 最终找到了一个不须要积分的推荐给大家.(支持迅雷下载) (2)兴许44集的jar包和项目文档等下载地址! watermark/ ...

  5. POI生成EXCEL,公式不自动执行的有关问题

    POI生成EXCEL,公式不自动执行的问题 场景:POI读取Excel模板. 当使用POI操作Excel时,发现由POI生成的公式能够在打开Excel是被执行, 而事先手工写入Excel模板文件的公式 ...

  6. signal(SIGPIPE, SIG_IGN) (转)

    signal(SIGPIPE, SIG_IGN) 当服务器close一个连接时,若client端接着发数据. 根据TCP 协议的规定,会收到一个RST响应,client再往这个服务器发送数据时,系统会 ...

  7. 常用的百度API地图操作

    常用的百度API地图操作,包括模糊搜索,放大缩小,并支持移动端 效果图如下 完整代码: http://download.csdn.net/detail/jine515073/8778167

  8. DataGridView:根据条件改变单元格的颜色

    根据条件改变DataGridView行的颜色可以使用RowPrePaint事件. 示例程序界面如下: 示例程序代码如下: using System; using System.Collections. ...

  9. MongoDB(三):MongoDB概念解析

    在上一篇文章中讲解了如何安装MongoDB,这篇文章中讲解一些有关MongoDB的概念. 不管我们要学习什么数据库,都应该学习其中的基础概念,在MongoDB中基本的概念是文档.集合.数据库,下面挨个 ...

  10. mvn 仓库地址修改

    默认仓库的存储位置 Maven缺省的本地仓库路径为${user.home}/.m2/repository具体如下图 自定义修改仓库的存储位置: 可改变默认的 .m2 目录下的默认本地存储库文件夹通过修 ...