📄 cache-manager.vm
字号:
#### Copyright(C) 2002 Javanovic Software (http://www.javanovic.com)#### This library is free software; you can redistribute it and/or## modify it under the terms of the GNU Lesser General Public## License as published by the Free Software Foundation; either## version 2.1 of the License, or (at your option) any later version.#### This library is distributed in the hope that it will be useful,## but WITHOUT ANY WARRANTY; without even the implied warranty of## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU## Lesser General Public License for more details.#### You should have received a copy of the GNU Lesser General Public## License along with this library; if not, write to the Free Software## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA##//---------------------------------------------------------// Application: $property.Name// Author : $property.Author// File : CacheManager.java//// Copyright $year $property.Company// Generated at $date.Time// using Karapan Sapi Struts Generator// Visit http://www.javanovic.com//---------------------------------------------------------package ${build.Package}.util;import java.util.*;class CostComparator implements Comparator { public int compare(Object o1, Object o2) { return ((Double) o1).compareTo((Double) o2); } public boolean equals(Object o) { return super.equals(o); }}public class CacheManager { public final static int LRU = 0; public final static int LFU = 1; public final static int MIX = 2; private HashMap cacheHashMap = new HashMap(); private int CACHE_CAPACITY = 100; private int TRESHOLD = 90; private int purgeAlgorithm; private long hitCount = 0; private long missCount = 0; public CacheManager(int purgeAlgorithm, int cacheCapacity, int treshold) { this.purgeAlgorithm = purgeAlgorithm; CACHE_CAPACITY = cacheCapacity; TRESHOLD = treshold; } public CacheManager(int purgeAlgorithm) { this.purgeAlgorithm = purgeAlgorithm; } public synchronized Object getCache(String identifier) { CachedObject cachedObject = (CachedObject) cacheHashMap.get(identifier); Object obj = null; if (cachedObject == null) { missCount++; } else if (cachedObject.isExpired()) { cacheHashMap.remove(identifier); missCount++; } else { cachedObject.incNumAccess(); cachedObject.setLastAccessTime(new Date()); hitCount++; obj = cachedObject.getObject(); } return obj; } public synchronized void invalidate(String identifier) { cacheHashMap.remove(identifier); } public long getHitCount() { return hitCount; } public long getMissCount() { return missCount; } public long getCurrentCacheSize() { return cacheHashMap.size(); } public synchronized void putCache(Object object, String id, int minutesToLive) { CachedObject cachedObject = new CachedObject(object, id, minutesToLive); if (cacheHashMap.size() == CACHE_CAPACITY) { sweep(); } cacheHashMap.put(id, cachedObject); } public synchronized void sweep() { TreeMap costTreeMap = new TreeMap(new CostComparator()); for (Iterator i = cacheHashMap.entrySet().iterator(); i.hasNext(); ) { Map.Entry entry = (Map.Entry) i.next(); CachedObject cachedObject = (CachedObject) entry.getValue(); if (cachedObject.isExpired()) { cacheHashMap.remove(entry.getKey()); } else { double cost = 0.0; switch (purgeAlgorithm) { case LFU: cost = cachedObject.getLFUCost(); break; case LRU: cost = cachedObject.getLRUCost(); break; default: cost = cachedObject.getMixCost(); } costTreeMap.put(new Double(cost), entry.getKey()); } } // delete to treshold for (int i = cacheHashMap.size(); i > TRESHOLD; i--) { Object kk = costTreeMap.firstKey(); Object k = costTreeMap.get(kk); cacheHashMap.remove(k); costTreeMap.remove(kk); } } public void clearCache() { hitCount = 0; missCount = 0; cacheHashMap.clear(); } public static String createKey(String[] keys) { StringBuffer newKey = new StringBuffer(""); for(int i =0;i<keys.length;i++) newKey.append(keys[i]).append("/"); return newKey.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -