8 Nisan 2020 Çarşamba

Caffeine Caffeine Sınıfı

Giriş
Şu satırı dahil ederiz
import com.github.benmanes.caffeine.cache.Caffeine;
build metodu
Bu sınıf Builder örüntüsünü kullanıyor

Örnek
Şöyle yaparız
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

Cache<String, String> cache = Caffeine.newBuilder().build();
Açıklaması şöyle
Caffeine will use ConcurrentHashMap to store the data.
evictionListener metodu
Örnek
Şöyle yaparız
Cache<String, String> cache = Caffeine.newBuilder()
  .expireAfterWrite(1, TimeUnit.SECONDS)
  .evictionListener((String key, String value, RemovalCause cause) ->
    System.out.printf("Key %s was evicted (%s)%n", key, cause))
  .removalListener((String key, String graph, RemovalCause cause) ->
    System.out.printf("Key %s was removed (%s)%n", key, cause))
  .build();

// Add and remove from cache
cache.put("1", "value_1");
cache.invalidate("1");

expireAfterWrite metodu
Açıklaması şöyle
You can specify a eviction policy based thats time based, you can specify the time after which the data should be evicted from the cache. This timer can look for last access time or last write time.
Örnek
Şöyle yaparız.
Cache<String, String> cache = Caffeine.newBuilder()
  .expireAfterWrite(300, TimeUnit.SECONDS)
  .maximumSize(60)
  .removalListener((String key, String value, RemovalCause cause) -> {
    cacheListenerHandler(key, value, cause);
  })
  .build();
Örnek
Şöyle yaparız
LoadingCache<String, String> cache = Caffeine.newBuilder()
  .expireAfterWrite(3, TimeUnit.SECONDS)
  .build(key -> String.format("value_%s", UUID.randomUUID()));

// Load six items into cache
for (int i = 1; i <= 5; i++)
  cache.put(String.valueOf(i), "value_" + i);

// Print initial cache contents
for (int i = 1; i <= 5; i++)
  System.out.printf("key: %s - value: %s%n", i, cache.get(String.valueOf(i)));

// Wait for eviction to complete
Thread.sleep(5000);

// Print cache contents
for (int i = 1; i <= 5; i++)
  System.out.printf("key: %s - value: %s%n", i, cache.get(String.valueOf(i)));
Çıktı şöyle
key: 1 - value: value_1
key: 2 - value: value_2
key: 3 - value: value_3
key: 4 - value: value_4
key: 5 - value: value_5
key: 1 - value: value_e7bd2cbf-24cf-490c-9183-628746b80bf5
key: 2 - value: value_b71804b9-22e6-4a6a-9cdf-1b34588e3f18
key: 3 - value: value_18d8b606-88ff-419c-9416-3bf19af4f6a1
key: 4 - value: value_7e9d58c1-68db-4c81-885c-122bfb83ae13
key: 5 - value: value_8595c928-3d6e-4560-80ed-5f2101b482ab
maximumSize metodu
Açıklaması şöyle
Caffeine allows users to evict data from the cache based on the size of the cache. This can be useful if you have a lot of data that needs to be cached, but you don’t want to use up all of your memory. 
Örnek
Şöyle yaparız
LoadingCache<String, String> cache = Caffeine.newBuilder()
    .maximumSize(5)
    .build(key -> String.format("value_%s", UUID.randomUUID()));

// Load six items into cache
for (int i = 1; i <= 5; i++)
  cache.put(String.valueOf(i), "value_" + i);

// Add item to cache
cache.put("6", "value_6");

// Wait for eviction to complete
Thread.sleep(5000);

// Print cache contents
for (int i = 1; i <= 6; i++)
  System.out.printf("key: %s - value: %s%n", i, cache.get(String.valueOf(i)));

policy metodu
Cache nesnesi kurulurken belirtilen bazı özelliklere erişim sağlar.

refreshAfterWrite metodu
Eviction işleminden farklı olarak nesnenin eski değerini döner ancak yeni değeri de asenkron olarak yükler ve değiştirir. Aslında metod ismi refreshAfterWrite yerine sadece refreshAfter olsa bence daha mantıklıydı

Açıklaması şöyle
This feature allows you to refresh data in the cache after a certain amount of time. This can be useful if you have data that needs to be updated periodically, but you don’t want to update it every time it is requested. 
Nesne silinmez yani evict edilmez. Açıklaması şöyle
... if an entry was beyond a certain age, it would still be considered a cache hit and returned, but it would be asynchronously reloaded after being requested
Asenkron yükleme için kullanılan Executor açıklaması şöyle
Refresh operations are executed asynchronously using an Executor. The default executor is ForkJoinPool.commonPool() and can be overridden via Caffeine.executor(Executor).

Örnek
Şöyle yaparız
LoadingCache<String, String> cache = Caffeine.newBuilder()
  .refreshAfterWrite(5, TimeUnit.SECONDS)
  .build(key -> {
    System.out.println("Fetching value for key: " + key);
    return String.format("value_%s", UUID.randomUUID());
});

// Add item to cache
cache.put("1", "value_1");
System.out.println(cache.get("1"));

// Wait for 7 seconds
Thread.sleep(7000);

// Refresh should have been triggered and now we will get different value
System.out.println(cache.get("1"));

// Wait for 1 second
Thread.sleep(1000);
System.out.println(cache.get("1"));
Açıklaması şöyle
Here is a example where the data is refreshed every 5 seconds using the refreshAfterWrite method. The cache is refreshed asynchronously, when the data is requested, so the old value (if any) is still returned while the key is being refreshed, in contrast to eviction, which forces retrievals to wait until the value is loaded again.
removalListener metodu
RemovalListener arayüzü yazısına taşıdım

put metodu
Elimizde şöyle bir kod olsun.
Cache<String, String> cache = Caffeine.newBuilder()
Şöyle yaparız.
public void registerValue(String key, String value) {
  cache.put(key, value);
  LOG.info("Current estimated size of {} keys in cache", cache.estimatedSize());
}

Hiç yorum yok:

Yorum Gönder