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

  • Adam Outler

    Adam Outler

    19 EKİM 2006
  • GavinMichaelBooth

    GavinMichael

    26 AĞUSTOS 2006
  • Titan Lee Hai

    Titan Lee Ha

    14 Temmuz 2008