Class FastCache<K,​V>

  • All Implemented Interfaces:
    java.util.Map<K,​V>

    public class FastCache<K,​V>
    extends java.util.AbstractMap<K,​V>
    A cache intended to help speed up access to a Map. The idea is that some maps have a few values that are retrieved more frequently than others. So, we create a fixed size array that holds keys and values, and do a very fast hash on the key's identityHashCode. The cache is backed by a map, which can be an IdentityHashMap, or any other map (such as a WeakHashMap) where the keys satisfy k1.equals(k2) implies k1 == k2. Note that all put operations MUST go through this class, because calling put on the underlying map can result in the cache returning incorrect results for get.
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.util.AbstractMap

        java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,​V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,​V extends java.lang.Object>
      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private long cacheCount  
      private java.lang.Object[] keys  
      private java.util.Map<K,​V> map  
      static int TABLE_SIZE  
      private long totalCount  
      private java.lang.Object[] values  
    • Constructor Summary

      Constructors 
      Constructor Description
      FastCache​(java.util.Map<K,​V> map)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()  
      V get​(java.lang.Object key)  
      long getCacheCount()  
      long getTotalCount()  
      private int hash​(java.lang.Object key)  
      V put​(K key, V value)
      Put the key and value in the cache and the underlying map.
      • Methods inherited from class java.util.AbstractMap

        clear, clone, containsKey, containsValue, equals, hashCode, isEmpty, keySet, putAll, remove, size, toString, values
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Field Detail

      • map

        private java.util.Map<K,​V> map
      • keys

        private java.lang.Object[] keys
      • values

        private java.lang.Object[] values
      • cacheCount

        private long cacheCount
      • totalCount

        private long totalCount
    • Constructor Detail

      • FastCache

        public FastCache​(java.util.Map<K,​V> map)
    • Method Detail

      • getCacheCount

        public long getCacheCount()
      • getTotalCount

        public long getTotalCount()
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Specified by:
        entrySet in class java.util.AbstractMap<K,​V>
      • hash

        private int hash​(java.lang.Object key)
      • get

        public V get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map<K,​V>
        Overrides:
        get in class java.util.AbstractMap<K,​V>
      • put

        public V put​(K key,
                     V value)
        Put the key and value in the cache and the underlying map. This writes through to the map, rather than first storing a value in the cache which is only written as required, because that makes it easier to preserve the correct behavior of the map.
        Specified by:
        put in interface java.util.Map<K,​V>
        Overrides:
        put in class java.util.AbstractMap<K,​V>