
com.github.pjfanning.jackson.CaffeineLookupCache Maven / Gradle / Ivy
package com.github.pjfanning.jackson;
import com.fasterxml.jackson.databind.util.LRUMap;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import java.util.concurrent.ConcurrentMap;
public class CaffeineLookupCache extends LRUMap {
private final ConcurrentMap cache;
private final int maxEntries;
public CaffeineLookupCache(int maxEntries) {
super(4, 10); //super class has its own storage (that we will not use)
this.maxEntries = maxEntries;
Cache fullCache = Caffeine.newBuilder().maximumSize(maxEntries).build();
this.cache = fullCache.asMap();
}
@Override
public V put(K key, V value) {
return cache.put(key, value);
}
@Override
public V putIfAbsent(K key, V value) {
return cache.putIfAbsent(key, value);
}
@Override
public V get(Object key) {
return cache.get(key);
}
@Override
public void clear() {
cache.clear();
}
@Override
public int size() {
return cache.size();
}
@Override
protected Object readResolve() {
return new CaffeineLookupCache(maxEntries);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy