Class FastCache<K,V>

java.lang.Object
java.util.AbstractMap<K,V>
org.glassfish.pfl.dynamic.copyobject.impl.FastCache<K,V>
All Implemented Interfaces:
Map<K,V>

public class FastCache<K,V> extends 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.
  • Field Details

    • TABLE_SIZE

      public static final int TABLE_SIZE
      See Also:
    • map

      private Map<K,V> map
    • keys

      private Object[] keys
    • values

      private Object[] values
    • cacheCount

      private long cacheCount
    • totalCount

      private long totalCount
  • Constructor Details

    • FastCache

      public FastCache(Map<K,V> map)
  • Method Details

    • getCacheCount

      public long getCacheCount()
    • getTotalCount

      public long getTotalCount()
    • entrySet

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

      private int hash(Object key)
    • get

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
      Overrides:
      get in class 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 Map<K,V>
      Overrides:
      put in class AbstractMap<K,V>