Class LocalCache

    • Field Detail

      • DEFAULT_UNITS

        public static final int DEFAULT_UNITS
        By default, the cache size (in units) is infinite.
        See Also:
        Constant Field Values
      • DEFAULT_EXPIRE

        public static final int DEFAULT_EXPIRE
        By default, the cache entries never expire.
        See Also:
        Constant Field Values
      • DEFAULT_KEY_MASK

        protected final LocalCache.KeyMask DEFAULT_KEY_MASK
        The default key mask that ignores nothing.
    • Constructor Detail

      • LocalCache

        public LocalCache()
        Construct the cache manager.
      • LocalCache

        public LocalCache​(int cUnits)
        Construct the cache manager.
        Parameters:
        cUnits - the number of units that the cache manager will cache before pruning the cache
      • LocalCache

        public LocalCache​(int cUnits,
                          int cExpiryMillis)
        Construct the cache manager.
        Parameters:
        cUnits - the number of units that the cache manager will cache before pruning the cache
        cExpiryMillis - the number of milliseconds that each cache entry lives before being automatically expired
      • LocalCache

        public LocalCache​(int cUnits,
                          int cExpiryMillis,
                          CacheLoader loader)
        Construct the cache manager.
        Parameters:
        cUnits - the number of units that the cache manager will cache before pruning the cache
        cExpiryMillis - the number of milliseconds that each cache entry lives before being automatically expired
        loader - the CacheLoader or CacheStore to use
    • Method Detail

      • clear

        public void clear()
        Removes all mappings from this map.
        Specified by:
        clear in interface Map
        Overrides:
        clear in class OldCache
      • remove

        public Object remove​(Object oKey)
        Removes the mapping for this key from this map if present.
        Specified by:
        remove in interface Map
        Overrides:
        remove in class OldCache
        Parameters:
        oKey - key whose mapping is to be removed from the map
        Returns:
        previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key, if the implementation supports null values
      • getCacheLoader

        public CacheLoader getCacheLoader()
        Determine the loader used by this LocalCache, if any.
        Returns:
        the loader used by this LocalCache, or null if none
      • setCacheLoader

        public void setCacheLoader​(CacheLoader loader)
        Specify the loader used by this LocalCache.
        Parameters:
        loader - loader to use, or null
      • get

        public Object get​(Object oKey)
        If the specified item is in the cache, return it. Otherwise, load the value for the specified key and return it.
        Specified by:
        get in interface Map
        Overrides:
        get in class OldCache
        Parameters:
        oKey - the key to the desired cached item
        Returns:
        the value corresponding to the specified key, otherwise null
      • getEntry

        public SafeHashMap.Entry getEntry​(Object oKey)
        Locate an Entry in the hash map based on its key. If the Entry is not in the cache, load the Entry for the specified key and return it.
        Overrides:
        getEntry in class OldCache
        Parameters:
        oKey - the key to the desired cached Entry
        Returns:
        the Entry corresponding to the specified key, otherwise null
      • getAll

        public Map getAll​(Collection colKeys)
        Get all the specified keys, if they are in the cache. For each key that is in the cache, that key and its corresponding value will be placed in the map that is returned by this method. The absence of a key in the returned map indicates that it was not in the cache, which may imply (for caches that can load behind the scenes) that the requested data could not be loaded.

        The result of this method is defined to be semantically the same as the following implementation, without regards to threading issues:

        
         Map map = new AnyMap(); // could be hash map or ...
         for (Iterator iter = col.iterator(); iter.hasNext(); )
             {
             Object oKey = iter.next();
             Object oVal = get(oKey);
             if (oVal != null || containsKey(oKey))
                 {
                 map.put(oKey, oVal);
                 }
             }
         return map;
         
        Specified by:
        getAll in interface CacheMap
        Overrides:
        getAll in class OldCache
        Parameters:
        colKeys - a collection of keys that may be in the named cache
        Returns:
        a Map of keys to values for the specified keys passed in col
      • load

        public void load​(Object oKey)
        Indicates to the cache that the specified key should be loaded into the cache, if it is not already in the cache. This provides a means to "pre-load" a single entry into the cache using the cache's loader.

        If a valid entry with the specified key already exists in the cache, or if the cache does not have a loader, then this method has no effect.

        An implementation may perform the load operation asynchronously.

        Parameters:
        oKey - the key to request to be loaded
      • loadAll

        public void loadAll()
        Indicates to the cache that it should load data from its loader to fill the cache; this is sometimes referred to as "pre-loading" or "warming" a cache.

        The specific set of data that will be loaded is unspecified. The implementation may choose to load all data, some specific subset of the data, or no data. An implementation may require that the loader implement the IterableCacheLoader interface in order for this method to load any data.

        An implementation may perform the load operation asynchronously.

      • loadAll

        public void loadAll​(Collection colKeys)
        Indicates to the cache that the specified keys should be loaded into the cache, if they are not already in the cache. This provides a means to "pre-load" entries into the cache using the cache's loader.

        The result of this method is defined to be semantically the same as the following implementation:

        
         CacheLoader loader = getCacheLoader();
         if (loader != null && !colKeys.isEmpty())
             {
             Set setRequest = new HashSet(colKeys);
             setRequest.removeAll(peekAll(colKeys).keySet());
             if (!setRequest.isEmpty())
                 {
                 Map map = loader.loadAll(colKeys);
                 if (!map.isEmpty())
                     {
                     putAll(map);
                     }
                 }
             }
         
        Parameters:
        colKeys - a collection of keys to request to be loaded
      • peek

        public Object peek​(Object oKey)
        Checks for a valid entry corresponding to the specified key in the cache, and returns the corresponding value if it is. If it is not in the cache, returns null, and does not attempt to load the value using its cache loader.
        Parameters:
        oKey - the key to "peek" into the cache for
        Returns:
        the value corresponding to the specified key
      • peekAll

        public Map peekAll​(Collection colKeys)
        Checks for a valid entry corresponding to each specified key in the cache, and places the corresponding value in the returned map if it is. For each key that is not in the cache, no entry is placed into the returned map. The cache does not attempt to load any values using its cache loader.

        The result of this method is defined to be semantically the same as the following implementation, without regards to threading issues:

        
         Map map = new HashMap();
         for (Iterator iter = colKeys.iterator(); iter.hasNext(); )
             {
             Object oKey   = iter.next();
             Object oValue = peek(oKey);
             if (oValue != null || containsKey(oKey))
                 {
                 map.put(oKey, oValue);
                 }
             }
         return map;
         
        Parameters:
        colKeys - a collection of keys to "peek" into the cache for
        Returns:
        a Map of keys that were found in the cache and their values
      • getCacheStore

        protected CacheStore getCacheStore()
        Determine the store used by this LocalCache, if any.
        Returns:
        the CacheStore used by this LocalCache, or null if none
      • getKeyMask

        protected LocalCache.KeyMask getKeyMask()
        ThreadLocal: Get the current key mask for the current thread.
        Returns:
        the current key mask
      • setKeyMask

        protected void setKeyMask​(LocalCache.KeyMask mask)
        ThreadLocal: Set the key mask for the current thread.
        Parameters:
        mask - the new key mask, or null to clear the mask
      • removeEvicted

        protected boolean removeEvicted​(OldCache.Entry entry)
        Remove an entry (if it is eligible for eviction) because it has expired.
        Overrides:
        removeEvicted in class OldCache
        Parameters:
        entry - the expired cache entry
        Returns:
        true iff the entry was removed
      • instantiateMapEvent

        protected MapEvent instantiateMapEvent​(int nId,
                                               Object oKey,
                                               Object oValueOld,
                                               Object oValueNew)
        Factory pattern: instantiate a new CacheEvent corresponding to the specified parameters.
        Overrides:
        instantiateMapEvent in class OldCache
        Parameters:
        nId - the event Id
        oKey - the key
        oValueOld - the old value
        oValueNew - the new value
        Returns:
        a new instance of the CacheEvent class (or a subclass thereof)
      • instantiateInternalListener

        protected MapListener instantiateInternalListener()
        Factory pattern: Instantiate an internal MapListener to listen to this cache and report changes to the CacheStore.
        Returns:
        a new MapListener instance
      • instantiateEntry

        protected SafeHashMap.Entry instantiateEntry()
        Factory method. This method exists to allow the OldCache class to be easily inherited from by allowing the Entry class to be easily sub-classed.
        Overrides:
        instantiateEntry in class OldCache
        Returns:
        an instance of Entry that holds the passed cache value