`
huntt
  • 浏览: 24751 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

EHCache的使用随记

    博客分类:
  • J2EE
 
阅读更多
1、在src目录下(WEB-INF/classes目录下)创建一个新文件ehcache.xml,内容如下:
<?xml version="1.0" encoding="gbk"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
	<diskStore path="java.io.tmpdir"/>
	
	<defaultCache 
		maxElementsInMemory="10000" 
		eternal="false"            
		timeToIdleSeconds="120"            
		timeToLiveSeconds="120"            
		overflowToDisk="true"            
		maxElementsOnDisk="10000000"            
		diskPersistent="false"            
		diskExpiryThreadIntervalSeconds="120"            
		memoryStoreEvictionPolicy="LRU" />
	
	<!-- 监听本服务器的端口 -->
	<cacheManagerPeerListenerFactory 
		class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
		properties="hostName=192.168.1.103, port=40002,socketTimeoutMillis=2000" />
	
	<!-- 同步另外一个服务器的cache -->
	<cacheManagerPeerProviderFactory 
		class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
		properties="peerDiscovery=manual,rmiUrls=//192.168.1.103:40001/userCache" />
	
	<!--
	必须的属性:
		name:设置缓存的名称,用于标志缓存,惟一
		maxElementsInMemory:在内存中最大的对象数量
        maxElementsOnDisk:在DiskStore中的最大对象数量,如为0,则没有限制
        eternal:设置元素是否永久的,如果为永久,则timeout忽略
        overflowToDisk:是否当memory中的数量达到限制后,保存到Disk
	可选的属性:
        timeToIdleSeconds:设置元素过期前的空闲时间
        timeToLiveSeconds:设置元素过期前的活动时间
        diskPersistent:是否disk store在虚拟机启动时持久化。默认为false
   		diskExpiryThreadIntervalSeconds:运行disk终结线程的时间,默认为120秒
        memoryStoreEvictionPolicy:策略关于Eviction
	缓存子元素:
		cacheEventListenerFactory:注册相应的的缓存监听类,用于处理缓存事件,如put,remove,update,和expire
	    bootstrapCacheLoaderFactory:指定相应的BootstrapCacheLoader,用于在初始化缓存,以及自动设置。
	-->
	<cache 
		name="userCache" 
		maxElementsInMemory="10000" 
		eternal="true" 
		overflowToDisk="true" 
		timeToIdleSeconds="0" 
		timeToLiveSeconds="0" 
		diskPersistent="false" 
		diskExpiryThreadIntervalSeconds="120">
		<cacheEventListenerFactory 
			class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
	</cache>
</ehcache>


2、将ehcache-1.5.0.jar和backport-util-concurrent.jar放到lib目录下

3、测试代码
import java.util.ArrayList;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class EHCacheTest {
	
	//创建cache的管理
	private static CacheManager manager = CacheManager.create();
	//得到ehcache中的userCache(在XML中定义的那个名称)
	private static Cache cache = manager.getCache("userCache");
	
	/**
	 * 将值加入到cache里
	 * @param param
	 */
	@SuppressWarnings("unchecked")
	public static void addCache(String param){
		ArrayList<String> list = null;
		if(cache.get("list") != null){
			Element element = cache.get("list");
			if(element != null){
				list = (ArrayList<String>)element.getValue();
				list.add(param);
			}
			cache.put(element);
		}
		else{
			list = new ArrayList<String>();
			list.add(param);
			Element element = new Element("list", list);
			cache.put(element);
		}
	}
	
	/**
	 * 展示cache中内容
	 */
	@SuppressWarnings("unchecked")
	public static void showCache(){
		if(cache.get("list") != null){
			Element element = cache.get("list");
			if(element != null){
				ArrayList<String> list = (ArrayList<String>)element.getValue();
				System.out.println("cacheList : " + list.toString());
			}
		}
	}
	
	public static void main(String[] args) {
		addCache("param1");
		addCache("param2");
		showCache();
	}
}
分享到:
评论

相关推荐

    ehcache使用详解

    **Ehcache 使用详解** Ehcache 是一个广泛使用的开源Java缓存库,它提供了内存和磁盘存储的二级缓存机制,以提高...在阅读`ehcache使用文档e.doc`后,你将对Ehcache有更深入的理解,并能自如地将其应用到你的项目中。

    EhCache使用详解

    EhCache使用详解,HIBERNATE缓冲

    ehcache使用,以及集群配置

    **Ehcache 使用详解与集群配置** Ehcache 是一个广泛使用的开源Java缓存系统,它提供了内存和磁盘存储,以及对缓存数据的分布式处理能力。在Java应用程序中,Ehcache能够显著提高性能,减少数据库负载,通过缓存...

    cache/ehcache缓存使用

    本文将详细讲解"cache/ehcache缓存使用"的相关知识点,包括缓存的基本概念、Ehcache的介绍、以及如何在Java应用中使用Ehcache进行缓存操作。 首先,我们要理解什么是缓存。缓存是一种存储技术,它临时存储常用或...

    ehCache 使用例子

    默认情况下,ehCache使用LRU策略。 5. **缓存过期策略**:通过设置`&lt;cache&gt;`元素的`timeToLiveSeconds`和`timeToIdleSeconds`属性,可以控制缓存在创建后多长时间内有效或者在未被访问多长时间后过期。 6. **...

    EhCache使用

    每次需要shiro做权限控制, Realm的授权方法就会被调用, 查询数据库重新完成授权! 问题: 性能开销比较大 解决: 对用户授权,只进行一次 查询,查询后,将用户授权信息放入缓存中,以后需要授权时,直接从缓存...

    ehcache配置使用详解

    ### ehcache配置使用详解 #### 一、ehcache概述与特性 **背景介绍:** 缓存作为提升系统响应速度和降低数据库压力的关键技术,在现代软件架构中占据着重要位置。ehcache,作为一款高性能的开源Java缓存框架,旨在...

    Ehcache使用

    ### Ehcache 使用详解 #### 一、概述 Ehcache 是一款开源的、纯 Java 缓存框架,它能够提供高性能、低延迟的数据缓存功能。Ehcache 的设计目标是提高应用程序性能,通过减少对数据库或其他外部系统的依赖来达到这...

    Spring与ehcache结合使用

    ### Spring与ehcache结合使用详解 #### 一、前言 在现代软件开发中,缓存技术被广泛应用于提高应用程序的性能。其中,Spring框架因其灵活性和强大的功能,在Java领域得到了广泛应用;而ehcache作为一款高性能、...

    EHCache使用手册

    本文将深入探讨EHCache的配置及其主要元素,帮助开发者更好地理解和使用EHCache。 首先,EHCache的配置文件通常命名为`ehcache.xml`,但也可以根据需求自定义。配置文件包含了对缓存行为的详细设定,这些设定主要由...

    ehcache3-samples, 关于使用 Ehcache 3,一些示例/教程.zip

    ehcache3-samples, 关于使用 Ehcache 3,一些示例/教程 Ehcache示例这里知识库包含有关 Ehcache 3用法的示例和教程。示例'basic'演示 Ehcache 3的基本配置和用法'集群'- 演示如何在Terracotta服务器上使用分布式缓存...

    ehcache.xsd_ehcache.xml代码提示.rar

    Ehcache是一个广泛使用的开源Java缓存解决方案,它能够提高应用程序性能,通过缓存数据和对象来减少数据库查询。 【描述解析】:描述中提到“已测试有效的ehcache.xsd文件”,这指的是Ehcache的XML Schema定义文件...

    ehcache所需jar包

    ehcache所需jar包 cglib-nodep-2.2.jar ehcache-core-2.5.2.jar ehcache-spring-annotations-1.2.0.jar guava-13.0.1.jar ehcache-terracotta-2.5.2.jar slf4j-api-1.6.1.jar slf4j-log4j12-1.6.1.jar terracotta-...

    Ehcache集成Spring的使用(转载)

    这篇博客将深入探讨如何将 Ehcache 集成到 Spring 应用中,以及如何使用 Spring AOP 实现计算结果的缓存。 首先,集成 Ehcache 到 Spring 需要以下步骤: 1. **引入依赖**: 在 Maven 或 Gradle 的配置文件中添加 ...

    ehcache监控工具ehcache-monitor-kit-1.0.3

    1.解压缩到目录下,复制ehcache-monitor-kit-1.0.0\lib\ehcache-probe-1.0.0.jar包到application的web-inf/lib目录下 2.将以下配置copy的ehcache.xml文件的ehcache标签中,注:上述链接中说的配置少写了个probe包名...

    ehcache

    Ehcache 使用 XML 配置文件进行初始化设置,包括缓存的大小、过期策略、缓存策略等。例如: ```xml maxEntriesLocalHeap="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120"&gt; ``...

    ehcache-3.3.1-API文档-中文版.zip

    赠送jar包:ehcache-3.3.1.jar; 赠送原API文档:ehcache-3.3.1-javadoc.jar; 赠送源代码:ehcache-3.3.1-sources.jar; 赠送Maven依赖信息文件:ehcache-3.3.1.pom; 包含翻译后的API文档:ehcache-3.3.1-javadoc-...

    ehcache.jar及源码

    Ehcache是一个广泛使用的开源Java缓存库,它为应用程序提供了高效的内存管理和数据缓存功能。Ehcache的核心目标是提高应用性能,通过将频繁访问的数据存储在内存中,减少对数据库的依赖,从而降低系统负载。这次我们...

    EHCache的使用

    ### EHCache的使用详解 #### 一、EHCache概述与特点 EHCache 是一款非常流行的开源缓存组件,由 SourceForge 提供支持。作为一个纯 Java 实现的高性能缓存库,EHCache 在处理高并发场景下表现优异。其主要特点包括...

Global site tag (gtag.js) - Google Analytics