Kolay, java LRU önbellek kullanımı kolay | Netgez.com
SORU
22 EKİM 2008, ÇARŞAMBA


Kolay, java LRU önbellek kullanımı kolay

Uygulama basit olduÄŸunu biliyorum, ama zaten var olan bir ÅŸeyi yeniden yapmak istiyorum.

Sorunu çözmek istiyorum ben yük yapılandırma (XML çok istiyorum önbellek onlar için farklı sayfaları, roller ... böylece birlikte girdi büyüyebilir oldukça fazla (ama ™ olmaz). 1%, ben bu iÅŸi önbellekte bazı öğelerin maksimum sayıda sahip olmak istiyorum

Kadar org buldum.apache.commons.koleksiyonları.göster.Apache commons LRUMap ve iyi görünüyor ama aynı zamanda başka bir şey daha sormak istiyorum. Herhangi bir öneriler?

CEVAP
22 EKİM 2008, ÇARŞAMBA


LinkedHashMap (Java 1.4) kullanabilirsiniz

exampledepot.com kod:

// Create cache
final int MAX_ENTRIES = 100;
Map cache = new LinkedHashMap(MAX_ENTRIES 1, .75F, true) {
    // This method is called just after a new entry has been added
    public boolean removeEldestEntry(Map.Entry eldest) {
        return size() > MAX_ENTRIES;
    }
};

// Add to cache
Object key = "key";
cache.put(key, object);

// Get object
Object o = cache.get(key);
if (o == null && !cache.containsKey(key)) {
    // Object not in cache. If null is not a possible value in the cache,
    // the call to cache.contains(key) is not needed
}

// If the cache is to be used by multiple threads,
// the cache must be wrapped with code to synchronize the methods
cache = (Map)Collections.synchronizedMap(cache);

Bunu PaylaÅŸ:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VÄ°DEO

Rastgele Yazarlar

  • boburnham

    boburnham

    11 Temmuz 2006
  • Elly

    Elly

    3 EKÄ°M 2005
  • USI Events

    USI Events

    6 AÄžUSTOS 2013