Jackson LookupCache tutorial with examples

PreviousNext

An interface describing the required API for the Jackson-databind Type cache.

Introduction

An interface describing the required API for the Jackson-databind Type cache.

Note that while interface itself does not specify synchronization requirements for implementations, specific use cases do. Typically implementations are expected to be thread-safe, that is, to handle synchronization.

Example

The following code shows how to use LookupCache from com.fasterxml.jackson.databind.util.

Example 1

import java.util.concurrent.ConcurrentHashMap;

/**//  w    w  w  . d e  m   o  2  s   .  co    m
 * A LookupCache implementation that has no synchronization (like LRUMap does)
 * but that has the downside of not limiting the size of the cache.
 */
public class UnlimitedLookupCache<K, V> implements LookupCache<K, V> {

    private final transient ConcurrentHashMap<K, V> _map;

    public UnlimitedLookupCache(int initialEntries) {
        // We'll use concurrency level of 4, seems reasonable
        _map = new ConcurrentHashMap<K, V>(initialEntries, 0.8f, 4);
    }

    @Override
    public int size() {
        return _map.size();
    }

    @Override
    public V get(Object key) {
        return _map.get(key);
    }

    @Override
    public V put(K key, V value) {
        return _map.put(key, value);
    }

    @Override
    public V putIfAbsent(K key, V value) {
        return _map.putIfAbsent(key, value);
    }

    @Override
    public void clear() {
        _map.clear();
    }
}
PreviousNext

Related