Class SimpleLruCache<K,V>

java.lang.Object
org.eclipse.jgit.util.SimpleLruCache<K,V>
Type Parameters:
K - the type of keys maintained by this cache
V - the type of mapped values

public class SimpleLruCache<K,V> extends Object
Simple limited size cache based on ConcurrentHashMap purging entries in LRU order when reaching size limit
Since:
5.1.9
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private Lock
     
     
    private int
     
    private int
     
    private long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    SimpleLruCache(int maxSize, float purgeFactor)
    Create a new cache
  • Method Summary

    Modifier and Type
    Method
    Description
    private static void
    checkPurgeFactor(float purgeFactor)
     
    void
    configure(int maxSize, float purgeFactor)
    Reconfigures the cache.
    get(Object key)
    Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
    private void
     
    private static int
    purgeSize(int maxSize, float purgeFactor)
     
    put(K key, V value)
    Maps the specified key to the specified value in this cache.
    int
    Returns the current size of this cache
    private long
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • lock

      private Lock lock
    • map

      private Map<K,SimpleLruCache.Entry<K,V>> map
    • maximumSize

      private volatile int maximumSize
    • purgeSize

      private int purgeSize
    • time

      private volatile long time
  • Constructor Details

    • SimpleLruCache

      public SimpleLruCache(int maxSize, float purgeFactor)
      Create a new cache
      Parameters:
      maxSize - maximum size of the cache, to reduce need for synchronization this is not a hard limit. The real size of the cache could be slightly above this maximum if multiple threads put new values concurrently
      purgeFactor - when the size of the map reaches maxSize the oldest entries will be purged to free up some space for new entries, purgeFactor is the fraction of maxSize to purge when this happens
  • Method Details

    • checkPurgeFactor

      private static void checkPurgeFactor(float purgeFactor)
    • purgeSize

      private static int purgeSize(int maxSize, float purgeFactor)
    • get

      public V get(Object key)
      Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

      More formally, if this cache contains a mapping from a key k to a value v such that key.equals(k), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

      Parameters:
      key - the key
      Returns:
      value mapped for this key, or null if no value is mapped
      Throws:
      NullPointerException - if the specified key is null
    • put

      public V put(@NonNull K key, @NonNull V value)
      Maps the specified key to the specified value in this cache. Neither the key nor the value can be null.

      The value can be retrieved by calling the get method with a key that is equal to the original key.

      Parameters:
      key - key with which the specified value is to be associated
      value - value to be associated with the specified key
      Returns:
      the previous value associated with key, or null if there was no mapping for key
      Throws:
      NullPointerException - if the specified key or value is null
    • tick

      private long tick()
    • size

      public int size()
      Returns the current size of this cache
      Returns:
      the number of key-value mappings in this cache
    • configure

      public void configure(int maxSize, float purgeFactor)
      Reconfigures the cache. If maxSize is reduced some entries will be purged.
      Parameters:
      maxSize - maximum size of the cache
      purgeFactor - when the size of the map reaches maxSize the oldest entries will be purged to free up some space for new entries, purgeFactor is the fraction of maxSize to purge when this happens
    • purge

      private void purge()