Created
January 15, 2023 17:38
-
-
Save oofnivek/24e41b189db44fbab27772f6998a25bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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