import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
public class DatabaseCache {
private static final Cache<String, String> cache = Caffeine.newBuilder()
.maximumSize(1000)
.build();
public static String getDataFromCache(String key) {
String value = cache.getIfPresent(key);
if (value == null) {
value = getFromDatabase(key);
cache.put(key, value);
}
return value;
}
private static String getFromDatabase(String key) {
// ...
return "data from database";
}
}