博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ehcache实例
阅读量:6637 次
发布时间:2019-06-25

本文共 4016 字,大约阅读时间需要 13 分钟。

hot3.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 放在类加载路径上:

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]

转载于:https://my.oschina.net/zhongwenhao/blog/224187

你可能感兴趣的文章
【linux】grep 和【perl】 脚本实现的grep功能的运行时间差异
查看>>
php strpos 字符串查找函数内部源码实现
查看>>
linux+nginx并发量大的时候出现Too many open files问题
查看>>
C++动态数组
查看>>
php 调用远程url的六种方法小结
查看>>
FTP服务器 传输性能测试之Raid 1+0篇
查看>>
Mac 终端Terminal 快捷键
查看>>
WebGIS--ArcGIS for Flex系列开发三:Tomcat部署
查看>>
PHP实现倒计时
查看>>
CAS服务端,查询数据库验证
查看>>
ThreadLocal的细节和设计模式
查看>>
CentOS6.5安装Tab增强版:bash-completion
查看>>
Maven实战读书笔记(6)
查看>>
使用Scrapy来爬取自己的CSDN文章 (2)
查看>>
线性表链接实现--双循环链表
查看>>
爱车加油记
查看>>
from selenium import selenium
查看>>
“业务比技术重要”一条企业开发中经典的谬论
查看>>
OpenCart之特价商品(Special)模块教程
查看>>
如何在 CentOS 7 中添加新磁盘而不用重启系统
查看>>