shorttermquerycache.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 475 行 · 第 1/2 页
JAVA
475 行
for (int i=0; i<cacheObjects.length; i++) { values[i] = ((CacheObject)cacheObjects[i]).object; } return Collections.unmodifiableList(Arrays.asList(values)); } public boolean containsKey(Object key) { // First, clear all entries that have been in cache longer than the // maximum defined age. deleteExpiredEntries(); return map.containsKey(key); } public void putAll(Map map) { for (Iterator i=map.keySet().iterator(); i.hasNext(); ) { Object key = i.next(); Object value = map.get(key); put(key, value); } } public boolean containsValue(Object value) { // First, clear all entries that have been in cache longer than the // maximum defined age. deleteExpiredEntries(); int objectSize = calculateSize(value); CacheObject cacheObject = new CacheObject(value, objectSize); return map.containsValue(cacheObject); } public Set entrySet() { // First, clear all entries that have been in cache longer than the // maximum defined age. deleteExpiredEntries(); return Collections.unmodifiableSet(map.entrySet()); } public String getName() { return name; } public Set keySet() { // First, clear all entries that have been in cache longer than the // maximum defined age. deleteExpiredEntries(); return Collections.unmodifiableSet(map.keySet()); } public long getCacheHits() { return cacheHits; } public long getCacheMisses() { return cacheMisses; } public int getCacheSize() { return cacheSize; } public int getMaxCacheSize() { return maxCacheSize; } public void setMaxCacheSize(int maxCacheSize) { this.maxCacheSize = maxCacheSize; // It's possible that the new max size is smaller than our current cache // size. If so, we need to delete infrequently used items. cullCache(); } public long getMaxLifetime() { return maxLifetime; } public void setMaxLifetime(long maxLifetime) { this.maxLifetime = maxLifetime; } /** * Returns the size of an object in bytes. Determining size by serialization * is only used as a last resort. * * @return the size of an object in bytes. */ private int calculateSize(Object object) { // If the object is Cacheable, ask it its size. if (object instanceof Cacheable) { return ((Cacheable)object).getCachedSize(); } // Coherence puts DataInputStream objects in cache. else if (object instanceof java.io.DataInputStream) { int size = 1; try { size = ((DataInputStream)object).available(); } catch (IOException ioe) { } return size; } // Check for other common types of objects put into cache. else if (object instanceof Long) { return CacheSizes.sizeOfLong(); } else if (object instanceof Integer) { return CacheSizes.sizeOfObject() + CacheSizes.sizeOfInt(); } else if (object instanceof Boolean) { return CacheSizes.sizeOfObject() + CacheSizes.sizeOfBoolean(); } else if (object instanceof long []) { long [] array = (long [])object; return CacheSizes.sizeOfObject() + array.length * CacheSizes.sizeOfLong(); } // Default behavior -- serialize the object to determine its size. else { int size = 1; try { // Default to serializing the object out to determine size. NullOutputStream out = new NullOutputStream(); ObjectOutputStream outObj = new ObjectOutputStream(out); outObj.writeObject(object); size = out.size(); } catch (IOException ioe) { ioe.printStackTrace(); } return size; } } /** * Clears all entries out of cache where the entries are older than the * maximum defined age. */ protected synchronized void deleteExpiredEntries() { // Check if expiration is turned on. if (maxLifetime <= 0) { return; } // Remove all old entries. To do this, we remove objects from the end // of the linked list until they are no longer too old. We get to avoid // any hash lookups or looking at any more objects than is strictly // neccessary. LinkedListNode node = ageList.getLast(); // If there are no entries in the age list, return. if (node == null) { return; } // Determine the expireTime, which is the moment in time that elements // should expire from cache. Then, we can do an easy to check to see // if the expire time is greater than the expire time. long expireTime = CacheFactory.currentTime - maxLifetime; while(expireTime > node.timestamp) { // Remove the object if it hasn't been read at least once. If // it has been read at least once, we know that a cache entry // update task has been created, so the object will be automatically // deleted. if (!(((CacheObject)map.get(node.object)).readCount > 0)) { remove(node.object); } // Move the node to the beginning of the list so that we don't // have to keep looking at it every time we try to expire entries. else { node.remove(); // Reset the timestamp so that we don't keep checking this node // again and again. node.timestamp = CacheFactory.currentTime; ageList.addFirst(node); } // Examine next node. node = ageList.getLast(); // If there are no more entries in the age list, return. if (node == null) { return; } } } /** * Removes objects from cache if the cache is too full. "Too full" is * defined as within 3% of the maximum cache size. Whenever the cache is * is too big, the least frequently used elements are deleted until the * cache is at least 10% empty. */ protected final void cullCache() { // See if the cache size is within 3% of being too big. If so, clean out // cache until it's 10% free. if (cacheSize >= maxCacheSize * .97) { // First, delete any old entries to see how much memory that frees. deleteExpiredEntries(); int desiredSize = (int)(maxCacheSize * .90); while (cacheSize > desiredSize) { // Get the key and invoke the remove method on it. remove(lastAccessedList.getLast().object); } } } /** * An extension of OutputStream that does nothing but calculate the number * of bytes written through it. */ private static class NullOutputStream extends OutputStream { int size = 0; public void write(int b) throws IOException { size++; } public void write(byte[] b) throws IOException { size += b.length; } public void write(byte[] b, int off, int len) { size += len; } /** * Returns the number of bytes written out through the stream. * * @return the number of bytes written to the stream. */ public int size() { return size; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?