日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jodd-cache集锦

發布時間:2025/4/5 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jodd-cache集锦 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Jodd cache提供了一組cache的實現,其層次如下:

其中,

AbstractCacheMap是一個具有計時和大小的緩存map的默認實現,它的實現類必須:

  創建一個新的緩存map。

  實現自己的刪除(prune)策略。

內部使用ReentranReadWriteLock來同步。因為從一個讀鎖升級到一個寫鎖是不可能的,因此在get(Object)方法內要注意。

FIFOCach:先進先出緩存。優點是簡單高效。缺點是不靈活,沒有在內存中保存常用的緩存對象。

/*** Creates a new LRU cache.*/public FIFOCache(int cacheSize, long timeout) {this.cacheSize = cacheSize;this.timeout = timeout;cacheMap = new LinkedHashMap<K,CacheObject<K,V>>(cacheSize + 1, 1.0f, false);}// ---------------------------------------------------------------- prune/*** Prune expired objects and, if cache is still full, the first one.*/@Overrideprotected int pruneCache() {int count = 0;CacheObject<K,V> first = null;Iterator<CacheObject<K,V>> values = cacheMap.values().iterator();while (values.hasNext()) {CacheObject<K,V> co = values.next();if (co.isExpired() == true) {values.remove();count++;}if (first == null) {first = co;}}if (isFull()) {if (first != null) {cacheMap.remove(first.key);count++;}}return count;}

LFUCache:最少訪問次數緩存。優點是常用緩存保留在內存中,偶然會使掃描算法失效。缺點是大的獲取消耗即這個算法不能快速適應變化的使用模式,特別是集群的臨時獲取是無效的。

public LFUCache(int maxSize, long timeout) {this.cacheSize = maxSize;this.timeout = timeout;cacheMap = new HashMap<K, CacheObject<K,V>>(maxSize + 1);}// ---------------------------------------------------------------- prune/*** Prunes expired and, if cache is still full, the LFU element(s) from the cache.* On LFU removal, access count is normalized to value which had removed object.* Returns the number of removed objects.*/@Overrideprotected int pruneCache() {int count = 0;CacheObject<K,V> comin = null;// remove expired items and find cached object with minimal access countIterator<CacheObject<K,V>> values = cacheMap.values().iterator();while (values.hasNext()) {CacheObject<K,V> co = values.next();if (co.isExpired() == true) {values.remove();onRemove(co.key, co.cachedObject);count++;continue;}if (comin == null) {comin = co;} else {if (co.accessCount < comin.accessCount) {comin = co;}}}if (isFull() == false) {return count;}// decrease access count to all cached objectsif (comin != null) {long minAccessCount = comin.accessCount;values = cacheMap.values().iterator();while (values.hasNext()) {CacheObject<K, V> co = values.next();co.accessCount -= minAccessCount;if (co.accessCount <= 0) {values.remove();onRemove(co.key, co.cachedObject);count++; }}}return count;}

LRUCache:最近未訪問緩存。緩存對象的消耗是一個常量。簡單高效,比FIFO更適應一個變化的場景。缺點是可能會被不會重新訪問的緩存占滿空間,特別是在面對獲取類型掃描時則完全不起作用。然后它是目前最常用的緩存算法。

/*** Creates a new LRU cache.*/public LRUCache(int cacheSize, long timeout) {this.cacheSize = cacheSize;this.timeout = timeout;cacheMap = new LinkedHashMap<K, CacheObject<K,V>>(cacheSize + 1, 1.0f, true) {@Overrideprotected boolean removeEldestEntry(Map.Entry eldest) {return LRUCache.this.removeEldestEntry(size());}};}/*** Removes the eldest entry if current cache size exceed cache size.*/protected boolean removeEldestEntry(int currentSize) {if (cacheSize == 0) {return false;}return currentSize > cacheSize;}// ---------------------------------------------------------------- prune/*** Prune only expired objects, <code>LinkedHashMap</code> will take care of LRU if needed.*/@Overrideprotected int pruneCache() {if (isPruneExpiredActive() == false) {return 0;}int count = 0;Iterator<CacheObject<K,V>> values = cacheMap.values().iterator();while (values.hasNext()) {CacheObject<K,V> co = values.next();if (co.isExpired() == true) {values.remove();count++;}}return count;}

TimedCache 不限制大小,只有當對象過期時才會刪除。標準的chache方法不會顯式的調用刪除(prune),而是根據定義好的延遲進行定時刪除。

public TimedCache(long timeout) {this.cacheSize = 0;this.timeout = timeout;cacheMap = new HashMap<K, CacheObject<K,V>>();}// ---------------------------------------------------------------- prune/*** Prunes expired elements from the cache. Returns the number of removed objects.*/@Overrideprotected int pruneCache() {int count = 0;Iterator<CacheObject<K,V>> values = cacheMap.values().iterator();while (values.hasNext()) {CacheObject co = values.next();if (co.isExpired() == true) {values.remove();count++;}}return count;}// ---------------------------------------------------------------- auto pruneprotected Timer pruneTimer;/*** Schedules prune.*/public void schedulePrune(long delay) {if (pruneTimer != null) {pruneTimer.cancel();}pruneTimer = new Timer();pruneTimer.schedule(new TimerTask() {@Overridepublic void run() {prune();}}, delay, delay);}/*** Cancels prune schedules.*/public void cancelPruneSchedule() {if (pruneTimer != null) {pruneTimer.cancel();pruneTimer = null;}}

注意,還提供了一個FileLFUCache,沒有繼承AbstractCacheMap.用LFU將文件緩存到內存,極大加快訪問常用文件的性能。

protected final LFUCache<File, byte[]> cache;protected final int maxSize;protected final int maxFileSize;protected int usedSize;/*** Creates file LFU cache with specified size. Sets* {@link #maxFileSize max available file size} to half of this value.*/public FileLFUCache(int maxSize) {this(maxSize, maxSize / 2, 0);}public FileLFUCache(int maxSize, int maxFileSize) {this(maxSize, maxFileSize, 0);}/*** Creates new File LFU cache.* @param maxSize total cache size in bytes* @param maxFileSize max available file size in bytes, may be 0* @param timeout timeout, may be 0*/public FileLFUCache(int maxSize, int maxFileSize, long timeout) {this.cache = new LFUCache<File, byte[]>(0, timeout) {@Overridepublic boolean isFull() {return usedSize > FileLFUCache.this.maxSize;}@Overrideprotected void onRemove(File key, byte[] cachedObject) {usedSize -= cachedObject.length;}};this.maxSize = maxSize;this.maxFileSize = maxFileSize;}// ---------------------------------------------------------------- get/*** Returns max cache size in bytes.*/public int getMaxSize() {return maxSize;}/*** Returns actually used size in bytes.*/public int getUsedSize() {return usedSize;}/*** Returns maximum allowed file size that can be added to the cache.* Files larger than this value will be not added, even if there is* enough room.*/public int getMaxFileSize() {return maxFileSize;}/*** Returns number of cached files.*/public int getCachedFilesCount() {return cache.size();}/*** Returns timeout.*/public long getCacheTimeout() {return cache.getCacheTimeout();}/*** Clears the cache.*/public void clear() {cache.clear();usedSize = 0;}// ---------------------------------------------------------------- getpublic byte[] getFileBytes(String fileName) throws IOException {return getFileBytes(new File(fileName));}/*** Returns cached file bytes.*/public byte[] getFileBytes(File file) throws IOException {byte[] bytes = cache.get(file);if (bytes != null) {return bytes;}// add filebytes = FileUtil.readBytes(file);if ((maxFileSize != 0) && (file.length() > maxFileSize)) {// don't cache files that size exceed max allowed file sizereturn bytes;}usedSize += bytes.length;// put file into cache// if used size > total, purge() will be invoked cache.put(file, bytes);return bytes;}

?

轉載于:https://www.cnblogs.com/davidwang456/p/4650755.html

總結

以上是生活随笔為你收集整理的jodd-cache集锦的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。