JavaWeb项目中加入ehcache缓存

在简单研究了memcached和redis之后,又看了同样很流行的ehcache缓存。

在JavaWeb项目中加入ehcache缓存技术,所依赖的主要jar包: ehcache.jar   backport-util-concurrent.jar

免积分下载地址:http://download.csdn.net/detail/kkkder/9638071

Demo是从网上找的如下:

package com.ehcache;

import java.io.Serializable;

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

public class EhCache 
{
	public static void main(String[] args) {
        //创建一个缓存管理器
        CacheManager singletonManager = CacheManager.create();
        //建立一个缓存实例
        Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2);
        //在内存管理器中添加缓存实例
        singletonManager.addCache(memoryOnlyCache);
        Cache cache = singletonManager.getCache("testCache");
        //使用缓存
        Element element = new Element("key1", "value1");
        cache.put(element);
        cache.put(new Element("key1", "value2"));

        element = cache.get("key1");
        Serializable value = element.getValue();
        System.out.println("value:"+value);

        int elementsInMemory = cache.getSize();
        System.out.println("elementsInMemory:"+elementsInMemory);

        long elementsInMemory2 = cache.getMemoryStoreSize();
        System.out.println("elementsInMemory2:"+elementsInMemory2);

        Object obj = element.getObjectValue();
        cache.remove("key1");
        System.out.println("obj:"+obj);
        singletonManager.shutdown();
    }
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值