Ehcache 初次体验

本文介绍如何配置EHCache实现内存与磁盘缓存功能,并通过示例演示其基本用法,包括缓存设置、元素存储及检索过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

听说ehcache可以做内存缓存,也可以做磁盘缓存。于是就拿来研究研究。

commons-logging-1.1.jar(www.apache.org 可以下载)

ehcache-1.2.3.jar(http://ehcache.sourceforge.net 可以下载)

ehcache.xml

<ehcache xmlns:xsi="&amp;lt;a href=" www.w3.org=""></ehcache>[ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"]
    [diskStore path="java.io.tmpdir"/]
    [cacheManagerEventListenerFactory class="" properties=""/]
     [defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            /]
    [cache name="personalHomeCache"
           maxElementsInMemory="5000"
           eternal="false"
           overflowToDisk="true"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           diskPersistent="true"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LFU"
           /]
[/ehcache]

 

TestEhache.java

 

package photoblog.cache;

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

public class TestEhcache {

 /**
  * @param args
  */
 public static void main(String[] args) {
  String fileName="E:\\project\\photoblog\\src\\photoblog\\cache\\ehcache.xml";
  CacheManager manager = new CacheManager(fileName);
  String names[]=manager.getCacheNames();
  Cache cache=manager.getCache(names[0]);
  cache.put(new Element("key1","values1"));
  Element element = cache.get("key1");
  Object obj=element.getObjectValue();
  System.out.println(obj.toString());
  manager.shutdown();
  

 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值