一、前言
关于jena源码包中定义com.hp.hpl.jena.db.impl.ICache接口、com.hp.hpl.jena.db.impl.LRUCache最近最久未使用实现算法(LRU是Least Recently Used的缩写,即最近最久未使用,常用于页面置换算法,是为虚拟页式存储管理服务),详情参见源码示例
二、源码示例
1.ICache接口
package com.hp.hpl.jena.db.impl;@b@@b@public abstract interface ICache<K, V>@b@{@b@ public abstract void put(K paramK, V paramV);@b@@b@ public abstract V get(K paramK);@b@@b@ public abstract void setLimit(int paramInt);@b@@b@ public abstract int getLimit();@b@}
2.LRUCache实现类
package com.hp.hpl.jena.db.impl;@b@@b@import com.hp.hpl.jena.shared.JenaException;@b@import com.hp.hpl.jena.util.CollectionFactory;@b@import java.util.Map;@b@import java.util.Random;@b@@b@public class LRUCache<K, T>@b@ implements ICache<K, T>@b@{@b@ protected Map<K, T> keyCache;@b@ protected Map<T, K> valCache;@b@ protected K[] Keys;@b@ protected Random rand;@b@ protected int maxCount;@b@ protected int count;@b@@b@ public LRUCache(int max)@b@ {@b@ this.rand = new Random();@b@ resize(max);@b@ }@b@@b@ protected void resize(int max)@b@ {@b@ this.maxCount = max;@b@ this.keyCache = CollectionFactory.createHashedMap(max);@b@ this.valCache = CollectionFactory.createHashedMap(max);@b@ this.Keys = ((Object[])new Object[max]);@b@ this.count = 0;@b@ }@b@@b@ public T get(K id)@b@ {@b@ return this.keyCache.get(id);@b@ }@b@@b@ public Object getByValue(String val) {@b@ return this.valCache.get(val);@b@ }@b@@b@ public void put(K id, T val)@b@ {@b@ synchronized (this) {@b@ int curSize = this.keyCache.size();@b@ this.keyCache.put(id, val);@b@ this.valCache.put(val, id);@b@ if (this.keyCache.size() > curSize) {@b@ int ix = this.count++;@b@ if (this.count > this.maxCount)@b@ {@b@ ix = this.rand.nextInt(this.maxCount);@b@ Object keyval = this.keyCache.get(this.Keys[ix]);@b@ if ((keyval == null) || (this.keyCache.remove(this.Keys[ix]) == null))@b@ throw new JenaException("LRUCache keyCache corrupted");@b@ if (this.valCache.remove(keyval) == null)@b@ throw new JenaException("LRUCache valCache corrupted");@b@ this.count -= 1;@b@ this.Keys[ix] = id;@b@ if (this.keyCache.size() > this.maxCount)@b@ throw new JenaException("LRUCache exceeds threshold");@b@ }@b@ this.Keys[ix] = id;@b@ }@b@ }@b@ }@b@@b@ public void clear() {@b@ this.keyCache.clear();@b@ this.valCache.clear();@b@ this.count = 0;@b@ }@b@@b@ public void setLimit(int max) {@b@ resize(max);@b@ }@b@@b@ public int getLimit() {@b@ return this.maxCount;@b@ }@b@}
3.集合CollectionFactory类
package com.hp.hpl.jena.util;@b@@b@import java.util.Collection;@b@import java.util.HashMap;@b@import java.util.HashSet;@b@import java.util.Map;@b@import java.util.Set;@b@@b@public class CollectionFactory@b@{@b@ public static <K, V> Map<K, V> createHashedMap()@b@ {@b@ return new HashMap();@b@ }@b@@b@ public static <K, V> Map<K, V> createHashedMap(int size)@b@ {@b@ return new HashMap(size);@b@ }@b@@b@ public static <K, V> Map<K, V> createHashedMap(Map<K, V> toCopy)@b@ {@b@ return new HashMap(toCopy);@b@ }@b@@b@ public static <T> Set<T> createHashedSet()@b@ {@b@ return new HashSet();@b@ }@b@@b@ public static <T> Set<T> createHashedSet(Collection<T> toCopy)@b@ {@b@ return new HashSet(toCopy);@b@ }@b@}