Ehcache缓存配置

news/2024/7/20 14:04:19 标签: 运维, 测试, 内存管理

下面介绍一下简单使用的配置过程: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" /> // 缓存存放在内存中
  <diskStore path="G:/ehcache/temp/"/>  //缓存存放在磁盘中 <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); 

 


这样使用就可。 

当然,在spring管理的bean中,也可: 

private Cache  ehCache;  
      
    @Resource(name="ehCache")  
    public void setEhCache(Cache ehCache) {  
        this.ehCache = ehCache;  
    }  

 


4.使用。 

这个其实就不用说了,大家都会了,我相信,能过对应的key值去获取就是了。 

如: Element lgEle= ehCache.get("loginName"); 
需要修改,就先取得再修改,删除就直接删除。 

转载于:https://www.cnblogs.com/chen-lhx/p/5616335.html


http://www.niftyadmin.cn/n/757001.html

相关文章

layer 弹出php视图,layui-layer独立组件-弹出层介绍

【注意事项】一、使用时&#xff0c;请把文件夹layer整个放置在您站点的任何一个目录&#xff0c;只需引入layer.js即可&#xff0c;除jQuery外&#xff0c;其它文件无需再引入。二、如果您的js引入是通过合并处理或者您不想采用layer自动获取的绝对路径&#xff0c;您可以通过…

Java工厂构造函数参数,当具体对象采用不同的构造函数参数时,在Java中为工厂...

小编典典您有两种选择&#xff1a;RectangularShape extends ShapeRoundShape extends Shape并RectangularShapeFactory与RoundShapeFactory2)构建器(另请参见有效Java中的第2项)public Shape {private final int x;private final int y;private final double radius;private S…

Qt Creater中Clang-format的使用

起因在于习惯性的想格式化代码&#xff0c;发现Qt Creater默认居然是没有代码格式化的&#xff0c;只有一个缩进&#xff0c;搞毛线啊&#xff01;&#xff01;&#xff01; 搜索了下&#xff0c;倒是很容易就搜到了&#xff0c;Qt Creater中有个插件&#xff1a;beautifier&am…

java制作烟花源码,国庆烟花抖音源码

简单的介绍一下Java语言的特点与优势&#xff1a;1.平台无关性平台无关性是指Java能运行于不同的平台。Java引进虚拟机 原理&#xff0c;并运行于虚拟机&#xff0c;实现不同平台的Java接口之间。使用Java编写的程序能在世界范围内共享。Java的数据类型与 机器无关&#xff0c;…

SQL Server with ties 语句

With ties 语句是与top、order by 语句联合使用的语句&#xff1b;我们在实际查询过程中会遇到这样的情况&#xff0c;比如查询考试为前三名的学生信息&#xff0c;发现有并列第三的情况&#xff0c;如果我们只是top 3 发现并列第三的学生信息有的没有显示出来&#xff0c;基于…

matlab simulink fft,simulink中怎样对数据进行FFT分析

在许多仿真中都会用到FFT(快速傅里叶分析)对信号进行分析&#xff0c;以判断仿真结果的好坏&#xff0c;这里给读者介绍两种Matlab中对数据进行FFT分析(快速傅里叶分析)的方法&#xff0c;希望对你有所帮助。方法一、利用simulink工具箱中的powergui进行FFT分析&#xff1a;这种…

对JAVA RMI的认识

RMI的定义 RPC (Remote Procedure Call):远程方法调用&#xff0c;用于一个进程调用另一个进程中的过程&#xff0c;从而提供了过程的分布能力。 RMI&#xff08;Remote Method Invocation):远程方法调用&#xff0c;即在RPC的基础上有向前迈进了一步&#xff0c;提供分布式对象…

mysql 中文linux 用命令 utf8,Linux下修改MySQL数据库字符编码为UTF-8解决中文乱码

由于MySQL编码原因会导致数据库出现乱码。解决办法&#xff1a;修改MySQL数据库字符编码为UTF-8&#xff0c;UTF-8包含全世界所有国家需要用到的字符,是国际编码。具体操作&#xff1a;1、进入MySQL控制台>mysql -uroot -p #输入密码进入>status; #查看当前MySQL运行状态…