com.fasterxml.jackson.databind.util.LRUMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of redisson-all Show documentation
Show all versions of redisson-all Show documentation
Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC
package com.fasterxml.jackson.databind.util;
import java.util.Map;
import java.util.function.BiConsumer;
import com.fasterxml.jackson.databind.util.internal.PrivateMaxEntriesMap;
/**
* Helper for simple bounded maps used for reusing lookup values.
*
* Note that serialization behavior is such that contents are NOT serialized,
* on assumption that all use cases are for caching where persistence
* does not make sense. The only thing serialized is the cache size of Map.
*
* Since Jackson 2.12, there has been pluggable {@link LookupCache} interface which
* allows users, frameworks, provide their own cache implementations.
*
* Since Jackson 2.14, the implementation
*
*- Evicts the least recently used entry when max size is reached
*
*- Is thread-safe and does NOT require external synchronization
*
*
*/
public class LRUMap
implements LookupCache, // since 2.12
java.io.Serializable
{
private static final long serialVersionUID = 2L;
protected final int _initialEntries;
protected final int _maxEntries;
protected final transient PrivateMaxEntriesMap _map;
public LRUMap(int initialEntries, int maxEntries)
{
_initialEntries = initialEntries;
_maxEntries = maxEntries;
// We'll use concurrency level of 4, seems reasonable
_map = new PrivateMaxEntriesMap.Builder()
.initialCapacity(initialEntries)
.maximumCapacity(maxEntries)
.concurrencyLevel(4)
.build();
}
// @since 2.16
@Override
public LookupCache emptyCopy() {
return new LRUMap(_initialEntries, _maxEntries);
}
@Override
public V put(K key, V value) {
return _map.put(key, value);
}
/**
* @since 2.5
*/
@Override
public V putIfAbsent(K key, V value) {
return _map.putIfAbsent(key, value);
}
// NOTE: key is of type Object only to retain binary backwards-compatibility
@Override
public V get(Object key) { return _map.get(key); }
@Override
public void clear() { _map.clear(); }
@Override
public int size() { return _map.size(); }
@Override
public void contents(BiConsumer consumer) {
for (Map.Entry entry : _map.entrySet()) {
consumer.accept(entry.getKey(), entry.getValue());
}
}
/*
/**********************************************************************
/* Serializable overrides
/**********************************************************************
*/
protected Object readResolve() {
return new LRUMap(_initialEntries, _maxEntries);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy