Skip to content

Instantly share code, notes, and snippets.

@oofnivek
Created January 15, 2023 17:38
Show Gist options
  • Save oofnivek/24e41b189db44fbab27772f6998a25bf to your computer and use it in GitHub Desktop.
Save oofnivek/24e41b189db44fbab27772f6998a25bf to your computer and use it in GitHub Desktop.
package com.example.demo;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import java.util.concurrent.TimeUnit;
public class CacheStore<T> {
private Cache<String, T> cache;
public CacheStore(int expiryDuration, TimeUnit timeUnit) {
cache = CacheBuilder.newBuilder()
.expireAfterWrite(expiryDuration, timeUnit)
.concurrencyLevel(Runtime.getRuntime().availableProcessors())
.build();
}
public T get(String key) {
return cache.getIfPresent(key);
}
public void add(String key, T value) {
if(key != null && value != null) {
cache.put(key, value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment