Nasıl Java 6 LRU önbellek uygulamak istiyorsunuz?
Lütfen EHCache veya OSCache, vb söyleme. Benim kendi kullanarak sadece SDK (yaparak öğrenme) uygulamak istediğim için bu soruyu amacıyla varsayalım. Önbellek bir çok iş parçacıklı ortamda kullanılacak göz önüne alındığında, hangi datastructures kullanırsınız? Zaten bir LinkedHashMap Collections#synchronizedMap kullanılarak uygulanan ettik ama eğer yeni eşzamanlı koleksiyonları daha iyi bir aday olurdu merak ediyorum.
GÜNCELLEME: sadece bu külçe buldum: Yegge's latest okuyordum
Sürekli zamanlı erişim ihtiyacı ve ekleme düzeni korumak istiyorsanız, daha iyi bir LinkedHashMap, gerçekten harika bir veri yapısı daha yapamazsın. Muhtemelen daha iyi olurdu tek yolu ise eşzamanlı bir versiyonu olsaydı. Ama ne yazık ki.
Neredeyse tamLinkedHashMap
Collections#synchronizedMap
Uygulama ben yukarıda gitmeden önce aynı şeyi düşünüyordum. Bilmek güzel bir şey göz ardı etmedim.
Cevaplara göre şimdiye kadar çok eşzamanlı bir LRU için en iyi yol ConcurrentHashMap LinkedHashMap
kullanan aynı mantığı kullanarak uzatmak olacak gibi geliyor.
CEVAP
Bu öneriler çok severim, ama şimdilik* Collections.synchronizedMap
*4 devam edeceğim sanırım. Eğer gelecekte tekrar ziyaret edersem, muhtemelen LinkedHashMap
HashMap
uzatır aynı şekilde ConcurrentHashMap
uzanan çalışacağım.
GÜNCELLEME:
İstek üzerine, burada benim şimdiki uygulama özeti.
private class LruCache<A, B> extends LinkedHashMap<A, B> {
private final int maxEntries;
public LruCache(final int maxEntries) {
super(maxEntries 1, 1.0f, true);
this.maxEntries = maxEntries;
}
/**
* Returns <tt>true</tt> if this <code>LruCache</code> has more entries than the maximum specified when it was
* created.
*
* <p>
* This method <em>does not</em> modify the underlying <code>Map</code>; it relies on the implementation of
* <code>LinkedHashMap</code> to do that, but that behavior is documented in the JavaDoc for
* <code>LinkedHashMap</code>.
* </p>
*
* @param eldest
* the <code>Entry</code> in question; this implementation doesn't care what it is, since the
* implementation is only dependent on the size of the cache
* @return <tt>true</tt> if the oldest
* @see java.util.LinkedHashMap#removeEldestEntry(Map.Entry)
*/
@Override
protected boolean removeEldestEntry(final Map.Entry<A, B> eldest) {
return super.size() > maxEntries;
}
}
Map<String, String> example = Collections.synchronizedMap(new LruCache<String, String>(CACHE_SIZE));
Nasıl tek örnek bir Java uygulaması uy...
Nasıl Java sonsuz uygulamak için?...
Nasıl yapılır: Windows Forms uygulamas...
Nasıl görev önceliği Java 5'te bir Exe...
Nasıl JSP dosyaları Java kod önlemek i...