Class Cache


  • public abstract class Cache
    extends Object
    A caching cache, whether shared between requests or used within the scope of a single request.

    The thread safety will be the minimum needed for the expected usage.

    1. Thread safe for caches shared between requests.
    2. Thread safe for caches used in requests that may use concurrent subrequests.
    3. Not thread safe for per-request, no-subrequest caches. (in this case it's much like request attributes).
    • Field Detail

      • VERIFY_CACHE_PARENT_CHILD_RELATIONSHIPS

        protected static final boolean VERIFY_CACHE_PARENT_CHILD_RELATIONSHIPS
        Enables the page parent-child relationships verification. This does not measurably affect performance; just leave it on.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Cache

        public Cache()
    • Method Detail

      • newMap

        public abstract <K,​V> Map<K,​V> newMap()
        Creates a new map that is suitable for the expected thread safety requirements. This map will itself be consistent with the thread safety guarantees of this cache overall.
      • newMap

        public abstract <K,​V> Map<K,​V> newMap​(int size)
        Creates a new map that is suitable for the expected thread safety requirements. This map will itself be consistent with the thread safety guarantees of this cache overall.
        Parameters:
        size - the number of elements the map can hold before internal resizing. this is the actual size, no need to consider load factor or other nonsense.
      • getAttribute

        public abstract Object getAttribute​(String key)
        Gets a cache attribute or null if not in cache.
      • getAttribute

        public <V> V getAttribute​(String key,
                                  Class<V> clazz)
        Gets a cache attribute or null if not in cache. The attribute is cast to the provided type.
      • getAttribute

        public abstract <V,​E extends Exception> V getAttribute​(String key,
                                                                     Class<V> clazz,
                                                                     Cache.Callable<? extends V,​E> callable)
                                                              throws E extends Exception
        Atomically checks the cache then calls the callable and adds. Note: It is possible the callable might be called and not used. If clean-up of unused callable is necessary, synchronize externally and use regular getAttribute/setAttribute in sequence.
        Throws:
        E extends Exception
      • removeAttribute

        public abstract void removeAttribute​(String key)
        Removes a cache attribute.