17 Kasım 2020 Salı

PassiveExpiringMap Sınıfı

Giriş
Şu satırı dahil ederiz
import org.apache.commons.collections4.map.PassiveExpiringMap;
constructor
Şöyle yaparız
PassiveExpiringMap.ConstantTimeToLiveExpirationPolicy<String, Integer>
  expirationPolicy = new PassiveExpiringMap.ConstantTimeToLiveExpirationPolicy<>(
            5, TimeUnit.SECONDS);

PassiveExpiringMap<String, Integer> expiringMap = new PassiveExpiringMap<>(expirationPolicy,
new HashMap<>());
get metodu
Örnek
Şöyle yaparız
expiringMap.put("one", Integer.valueOf(1));
expiringMap.put("two", Integer.valueOf(2));
expiringMap.put("three", Integer.valueOf(3));
int initialCapacity = expiringMap.size(); //Size is 3
System.out.println("initialCapacity = " + initialCapacity);
Assert.assertEquals(3, initialCapacity);

System.out.println("Sleeping...");
try { Thread.sleep(10000L); } catch (InterruptedException e) { }

int updatedCapacity = expiringMap.size(); //Size is 0
System.out.println("updatedCapacity = " + updatedCapacity);
Integer one = expiringMap.get("one");
Assert.assertNull(one);
Assert.assertEquals(0, updatedCapacity);

Hiç yorum yok:

Yorum Gönder