採用LinkedHashMap內建的LRU 演算法快取資料, 可檢測對象是否已被虛擬機器回收,並且重新計算當前緩衝大小,清除緩衝中無用的索引值對象(即已經被虛擬機器回收但未從緩衝清除的資料);
* 預設記憶體緩衝大小為: 4 * 1024 * 1024 可通過通過setMaxCacheSize重新設定緩衝大小,可手動清空記憶體緩衝
* <br>支援記憶體緩衝和磁碟緩衝方式, 通過 {@link cc.util.cache.NetByteWrapper} 支援HTTP緩衝 (註:詳細參考cc.util.http包); 註:使用JDK7
package cc.util.cache; import java.io.Serializable;import java.util.Objects; /** 封裝網路資料, 將資料的Etag、lastModified擷取到, 下次請求的時候提取出來到伺服器比對 * Help to wrap byte data which obtains from network, It will work with {@link cc.util.cache.NetChacheManager} * @author wangcccong * @version 1.1406 * <br> create at: Tues, 10 Jun 2014 */public class NetByteWrapper implements Serializable { private final static long serialVersionUID = 1L; /** data from network */ private byte[] data; /** data size */ int contentLength; /** latested modify time */ private long lastModified; /** ETag: look up HTTP Protocol */ private String ETag; public NetByteWrapper(byte[] data, long lastModified, String Etag) { this.data = data; this.lastModified = lastModified; this.ETag = Etag; } public byte[] getData() { return data; } public void setData(byte[] data) { this.data = data; } public long getLastModified() { return lastModified; } public void setLastModified(long lastModified) { this.lastModified = lastModified; } public String getETag() { return ETag; } public void setETag(String eTag) { this.ETag = eTag; } public int getContentLength() { return Objects.isNull(data) ? 0 : data.length; }} package cc.util.cache; import java.lang.ref.ReferenceQueue;import java.lang.ref.SoftReference; /**採用軟引用方式將資料存放起來 * enclose {@link cc.util.cache.NetByteWrapper} with {@link java.lang.ref.SoftReference}, In order to recycle the memory * @author wangcccong * @version 1.1406 * <br> create at: Tues, 10 Jun. 2014 */public class NetByteSoftReference extends SoftReference<NetByteWrapper> { private String key = ""; private long length = 0; public NetByteSoftReference(String key, NetByteWrapper arg0) { this(key, arg0, null); } public NetByteSoftReference(String key, NetByteWrapper arg0, ReferenceQueue<? super NetByteWrapper> arg1) { super(arg0, arg1); // TODO Auto-generated constructor stub this.key = key; this.length = arg0.getContentLength(); } public String getKey() { return key; } public long getLength() { return length; } } package cc.util.cache; import java.lang.ref.ReferenceQueue;import java.lang.ref.SoftReference;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Objects; /** * 採用LinkedHashMap內建的LRU 演算法快取資料, 可檢測對象是否已被虛擬機器回收,並且重新計算當前緩衝大小,清除緩衝中無用的索引值對象(即已經被虛擬機器回收但未從緩衝清除的資料); * 預設記憶體緩衝大小為: 4 * 1024 * 1024 可通過通過setMaxCacheSize重新設定緩衝大小,可手動清空記憶體緩衝,支援採用記憶體映射方式讀取緩衝 * <br>支援記憶體緩衝和磁碟緩衝方式, 通過 {@link cc.util.cache.NetByteWrapper} 支援HTTP緩衝 (註:詳細參考cc.util.http包) * @author wangcccong * @version 1.1406 * <br> create at: Tues, 10 Jun 2014 */public class NetCacheManager { /** max cache size */ private long MAX_CACHE_SIZE = 4 * 1024 * 1024; private long cacheSize = 0; private static NetCacheManager instance = null; private final ReferenceQueue<NetByteWrapper> referenceQueue; private final LinkedHashMap<String, NetByteSoftReference> cacheMap; private NetCacheManager(){ referenceQueue = new ReferenceQueue<NetByteWrapper>(); cacheMap = new LinkedHashMap<String, NetByteSoftReference>(16, 0.75f, true) { private static final long serialVersionUID = -8378285623387632829L; @Override protected boolean removeEldestEntry( java.util.Map.Entry<String, NetByteSoftReference> eldest) { // TODO Auto-generated method stub boolean shouldRemove = cacheSize > MAX_CACHE_SIZE; if (shouldRemove) { cacheSize -= eldest.getValue().getLength(); System.gc(); } return shouldRemove; } }; } /** singleton model */ public static synchronized NetCacheManager newInstance(){ if (Objects.isNull(instance)) { instance = new NetCacheManager(); } return instance; } /** * reset the memory cache size * @param cacheSize */ public void setMaxCacheSize(long cacheSize) { this.MAX_CACHE_SIZE = cacheSize; } /** * 擷取當前記憶體緩衝大小 * @return */ public long getCacheSize() { return cacheSize; } /** * 將資料緩衝至記憶體, 如果http返回的資料<b>不支援</b>緩衝則採用此方法,緩衝的key一般為請求的url * @param key * @param value */ public void cacheInMemory(String key, byte[] value) { this.cacheInMemory(key, value, 0, null); } /** * 將資料緩衝至記憶體, 如果http返回的資料<b>支援</b>緩衝則採用此方法 * @param key * @param value * @param lastModified */ public void cacheInMemory(String key, byte[] value, long lastModified) { this.cacheInMemory(key, value, lastModified, null); } /** * 將資料緩衝至記憶體, 如果http返回的資料<b>支援</b>緩衝則採用此方法 * @param key * @param value * @param Etags */ public void cacheInMemory(String key, byte[] value, String Etags) { this.cacheInMemory(key, value, 0, Etags); } /** * 將資料緩衝至記憶體, 如果http返回的資料<b>支援</b>緩衝則採用此方法 * @param key * @param value * @param lastModified * @param Etags */ private void cacheInMemory(String key, byte[] value, long lastModified, String Etags) { Objects.requireNonNull(key, "key must not be null"); clearRecycledObject(); NetByteWrapper wrapper = new NetByteWrapper(value, lastModified, Etags); NetByteSoftReference byteRef = new NetByteSoftReference(key, wrapper, referenceQueue); cacheMap.put(key, byteRef); value = null; wrapper = null; } /** * 緩衝至磁碟, 預設不首先緩衝到記憶體 * @param key * @param value * @param path */ public void cacheInDisk(String key, byte[] value, String path) { cacheInDisk(key, value, path, false); } /** * * @param key * @param value * @param path * @param cacheInMemory */ public void cacheInDisk(String key, byte[] value, String path, boolean cacheInMemory) { this.cacheInDisk(key, value, 0, null, path, cacheInMemory); } /** * * @param key * @param value * @param lastModified * @param Etags * @param path * @param cacheInMemory */ private void cacheInDisk(String key, byte[] value, long lastModified, String Etags, String path, boolean cacheInMemory) { if (cacheInMemory) cacheInMemory(key, value, lastModified, Etags); try (FileOutputStream fos = new FileOutputStream(path); ObjectOutputStream oos = new ObjectOutputStream(fos)) { NetByteWrapper wrapper = new NetByteWrapper(value, lastModified, Etags); oos.writeObject(wrapper); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } /** * get {@link cc.util.cache.NetByteWrapper} from memory according to key * @param key * @return {@link cc.util.cache.NetByteWrapper} */ public NetByteWrapper getFromMemory(String key) { SoftReference<NetByteWrapper> softReference = cacheMap.get(key); return Objects.nonNull(softReference) ? softReference.get() : null; } /** * get byte[] from memory according to key * @param context * @param key * @return */ public byte[] getByteFromMemory(String key) { NetByteWrapper wrapper = getFromMemory(key); return Objects.nonNull(wrapper) ? wrapper.getData() : null; } /** * 從磁碟擷取資料 * @param path * @return {@link cc.util.cache.NetByteWrapper} */ public NetByteWrapper getFromDisk(String path) { try (FileInputStream fis = new FileInputStream(path); ObjectInputStream ois = new ObjectInputStream(fis)) { NetByteWrapper wrapper = (NetByteWrapper) ois.readObject(); return wrapper; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); return null; } } /** * 採用記憶體映射的方式從磁碟擷取資料(加快讀取緩衝的大檔案) * @param path * @return */ public NetByteWrapper getFromDiskByMapped(String path) { try (FileInputStream fis = new FileInputStream(path); FileChannel channel= fis.getChannel(); ByteArrayOutputStream baos = new ByteArrayOutputStream()){ MappedByteBuffer mbb = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); byte[] bts = new byte[1024]; int len = (int) channel.size(); for (int offset = 0; offset < len; offset += 1024) { if (len - offset > 1024) mbb.get(bts); else mbb.get((bts = new byte[len - offset])); baos.write(bts); } ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); NetByteWrapper wrapper = (NetByteWrapper) ois.readObject(); bais.close(); ois.close(); return wrapper; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); return null; } } /** * 從磁碟擷取緩衝的byte[] 資料 * @param path * @return */ public byte[] getByteFromDisk(String path) { NetByteWrapper wrapper = getFromDisk(path); return Objects.isNull(wrapper) ? null : wrapper.getData(); } /** * 通過記憶體映射放射從磁碟擷取緩衝的byte[] 資料 * @param path * @return */ public byte[] getByteFromDiskByMapped(String path) { NetByteWrapper wrapper = getFromDiskByMapped(path); return Objects.isNull(wrapper) ? null : wrapper.getData(); } /** * calculate the size of the cache memory */ private void clearRecycledObject() { NetByteSoftReference ref = null; //檢測對象是否被回收,如果被回收則從緩衝中移除死項 while (Objects.nonNull((ref = (NetByteSoftReference) referenceQueue.poll()))) { cacheMap.remove(ref.getKey()); } cacheSize = 0; Iterator<String> keys = cacheMap.keySet().iterator(); while (keys.hasNext()) { cacheSize += cacheMap.get(keys.next()).getLength(); } } /** * clear the memory cache */ public void clearCache() { clearRecycledObject(); cacheMap.clear(); System.gc(); System.runFinalization(); } }