一、前言
关于jena源码包中定义com.hp.hpl.jena.util.cache.CacheControl缓存控制接口、com.hp.hpl.jena.util.cache.Cache缓存接口,并通过com.hp.hpl.jena.util.cache.RandCache随机缓存实现类,实现类随机位置/存储容器大小不确定等随机缓存特性,详情参见源码示例。
二、源码示例
1.Cache、CacheControl接口
package com.hp.hpl.jena.util.cache;@b@@b@public abstract interface CacheControl@b@{@b@ public abstract boolean getEnabled();@b@@b@ public abstract boolean setEnabled(boolean paramBoolean);@b@@b@ public abstract void clear();@b@@b@ public abstract long getGets();@b@@b@ public abstract long getPuts();@b@@b@ public abstract long getHits();@b@}
package com.hp.hpl.jena.util.cache;@b@@b@public abstract interface Cache extends CacheControl@b@{@b@ public abstract Object get(Object paramObject);@b@@b@ public abstract void put(Object paramObject1, Object paramObject2);@b@}
2.RandCache实现类
package com.hp.hpl.jena.util.cache;@b@@b@import java.util.Collection;@b@import java.util.HashMap;@b@import java.util.Iterator;@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@@b@public class RandCache@b@ implements Cache, CacheControl@b@{@b@ int size;@b@ int threshhold;@b@ boolean enabled = true;@b@ String name;@b@ HashMap<java.lang.Object, java.lang.Object> map;@b@ Collection<java.lang.Object> collection;@b@ protected static Logger logger = LoggerFactory.getLogger(RandCache.class);@b@ long gets = 0L;@b@ long puts = 0L;@b@ long hits = 0L;@b@@b@ RandCache(String name, int size)@b@ {@b@ this.size = size;@b@ try {@b@ this.map = new HashMap(size * 100 / 75);@b@ } catch (IllegalArgumentException e) {@b@ if ("Illegal load factor: NaN".equals(e.getMessage()))@b@ {@b@ logger.warn("Detected a NaN anomaly believed to be due to use of JDK 1.4.1");@b@ this.map = new HashMap(size * 100 / 75, 0.75F);@b@ } else {@b@ throw e;@b@ }@b@ }@b@ this.threshhold = size;@b@ if (this.threshhold < 2)@b@ throw new Error("Cache size too small: " + size);@b@@b@ this.collection = this.map.values();@b@ }@b@@b@ public synchronized java.lang.Object get(java.lang.Object key) {@b@ if (this.enabled) {@b@ if (this.gets == 9223372036854775807L)@b@ forgetStats();@b@@b@ this.gets += 1L;@b@ java.lang.Object result = this.map.get(key);@b@ if (result != null)@b@ this.hits += 1L;@b@@b@ return result;@b@ }@b@ return null;@b@ }@b@@b@ public synchronized void put(java.lang.Object key, java.lang.Object value)@b@ {@b@ if (value == null) {@b@ throw new NullPointerException();@b@ }@b@@b@ if (this.enabled) {@b@ if (this.puts == 9223372036854775807L)@b@ forgetStats();@b@@b@ this.puts += 1L;@b@ if (this.map.size() >= this.threshhold)@b@ makeSpace();@b@@b@ this.map.put(key, value);@b@ }@b@ }@b@@b@ protected void makeSpace() {@b@ Iterator iter = this.collection.iterator();@b@@b@ int size = this.map.size();@b@ int i = 3;@b@ while (i < size) {@b@ iter.next();@b@ iter.remove();@b@ iter.next();@b@ iter.next();@b@ i += 3;@b@ }@b@ }@b@@b@ public synchronized boolean getEnabled() {@b@ return this.enabled;@b@ }@b@@b@ public synchronized boolean setEnabled(boolean enabled) {@b@ boolean result = enabled;@b@ this.enabled = enabled;@b@ return result;@b@ }@b@@b@ public synchronized void clear() {@b@ this.map.clear();@b@ }@b@@b@ public synchronized long getHits() {@b@ return this.hits;@b@ }@b@@b@ public synchronized long getGets() {@b@ return this.gets;@b@ }@b@@b@ public synchronized long getPuts() {@b@ return this.puts;@b@ }@b@@b@ protected void forgetStats() {@b@ this.gets /= 2L;@b@ this.puts /= 2L;@b@ this.hits /= 2L;@b@ }@b@}