ehcache java实例_ehcache实例

代码结构

0b1469b9aa34d9ae47d9f8fa7c4275ac.png

ColorDatabase.java

/*

* All content copyright (c) Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All

* rights reserved.

*/

package org.terracotta;

import java.awt.Color;

import java.util.Map;

import java.util.HashMap;

/**

* This class simulates an external color database that is used to populate the

* ColorCache. The primary, named AWT colors are stored in a map. Anytime a

* color is requested and found, the calling thread is put to sleep for 3 seconds

* to simulate a slow or overloaded database.

*/

public class ColorDatabase {

private static final Map colorMap = new HashMap();

static {

colorMap.put("red", Color.red);

colorMap.put("blue", Color.blue);

colorMap.put("green", Color.green);

colorMap.put("white", Color.white);

colorMap.put("black", Color.black);

colorMap.put("lightGray", Color.lightGray);

colorMap.put("gray", Color.gray);

colorMap.put("darkGray", Color.darkGray);

colorMap.put("pink", Color.pink);

colorMap.put("orange", Color.orange);

colorMap.put("yellow", Color.yellow);

colorMap.put("magenta", Color.magenta);

colorMap.put("cyan", Color.cyan);

}

public ColorDatabase() {

}

/**

* Simulates retrieving expensive object from SOR.

*/

public Color getColor(String name) {

Color color = colorMap.get(name);

if(color == null) {

return null;

}

try {

Thread.sleep(3000);

} catch(Exception e) {}

return color;

}

}

ColorCache.java

/*

* All content copyright (c) Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All

* rights reserved.

*/

package org.terracotta;

import net.sf.ehcache.*;

import java.awt.Color;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

public class ColorCache {

private static final CacheManager cacheManager = new CacheManager();

private static final ColorDatabase colorDatabase = new ColorDatabase();

public ColorCache() {

/**/

}

public Color getColor(String name) {

Element elem = getCache().get(name);

if (elem == null) {

Color color = colorDatabase.getColor(name);

if (color == null) { return null; }

getCache().put(elem = new Element(name, color));

}

return (Color) elem.getValue();

}

private Color getCachedColor(String name) {

Element elem = getCache().get(name);

return elem != null ? (Color) elem.getValue() : null;

}

public String[] getColorNames() {

@SuppressWarnings("unchecked")

Iterator keys = ((List) getCache().getKeys()).iterator();

List list = new ArrayList();

while (keys.hasNext()) {

String name = keys.next();

if (getCachedColor(name) != null) {

list.add(name);

}

}

return list.toArray(new String[list.size()]);

}

public long getTTL() {

return getCache().getCacheConfiguration().getTimeToLiveSeconds();

}

public long getTTI() {

return getCache().getCacheConfiguration().getTimeToIdleSeconds();

}

public int getSize() {

return getCache().getSize();

}

private Ehcache getCache() {

return cacheManager.getEhcache("colors");

}

} ehcache.xml 放在类加载路径上:

maxElementsInMemory="10000"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

overflowToDisk="true"

/>

maxElementsInMemory="100"

maxElementsOnDisk="0"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="0">

maxElementsInMemory="10000"

eternal="false"

timeToIdleSeconds="300"

timeToLiveSeconds="600"

overflowToDisk="true"

/>

maxElementsInMemory="1000"

eternal="true"

timeToIdleSeconds="0"

timeToLiveSeconds="0"

overflowToDisk="false"

/>

Test.java

package org.terracotta;

import java.awt.Color;

public class Test {

public static void main(String[] args) {

// TODO Auto-generated method stub

ColorCache cache = new ColorCache();

long now = System.currentTimeMillis();

Color red=cache.getColor("red");

long elapsed = System.currentTimeMillis() - now;

//第一次获取数据时间为:

System.out.println("第一次获取数据时间为:"+elapsed+"---"+red);

long now2 = System.currentTimeMillis();

Color red2=cache.getColor("red");

long elapsed2 = System.currentTimeMillis() - now2;

//第2次获取数据时间为:

System.out.println("第2次获取数据时间为:"+elapsed2+"---"+red2);

}

} 测试结果:

第一次获取数据时间为:3003---java.awt.Color[r=255,g=0,b=0]

第2次获取数据时间为:0---java.awt.Color[r=255,g=0,b=0]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值