Package org.htmlunit

Class Cache

  • All Implemented Interfaces:
    java.io.Serializable

    public class Cache
    extends java.lang.Object
    implements java.io.Serializable

    Simple cache implementation which caches compiled JavaScript files and parsed CSS snippets. Caching compiled JavaScript files avoids unnecessary web requests and additional compilation overhead, while caching parsed CSS snippets avoids very expensive CSS parsing.

    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      Cache()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void cache​(java.lang.String css, org.htmlunit.cssparser.dom.CSSStyleSheetImpl styleSheet)
      Caches the parsed version of the specified CSS snippet.
      boolean cacheIfPossible​(WebRequest request, WebResponse response, java.lang.Object toCache)
      Caches the specified object, if the corresponding request and response objects indicate that it is cacheable.
      void clear()
      Clears the cache.
      void clearOutdated()
      Removes outdated entries from the cache.
      protected void deleteOverflow()
      Truncates the cache to the maximal number of entries.
      java.lang.Object getCachedObject​(WebRequest request)
      Returns the cached object corresponding to the specified request.
      WebResponse getCachedResponse​(WebRequest request)
      Returns the cached response corresponding to the specified request.
      org.htmlunit.cssparser.dom.CSSStyleSheetImpl getCachedStyleSheet​(java.lang.String css)
      Returns the cached parsed version of the specified CSS snippet.
      protected long getCurrentTimestamp()
      Gets the current time stamp.
      int getMaxSize()
      Returns the cache's maximum size.
      int getSize()
      Returns the number of entries in the cache.
      protected boolean isCacheable​(WebRequest request, WebResponse response)
      Determines if the specified response can be cached.
      protected boolean isCacheableContent​(WebResponse response)
      Perform prior validation for 'no-store' directive in Cache-Control header.
      protected static java.util.Date parseDateHeader​(WebResponse response, java.lang.String headerName)
      Parses and returns the specified date header of the specified response.
      void setMaxSize​(int maxSize)
      Sets the cache's maximum size.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Cache

        public Cache()
    • Method Detail

      • cacheIfPossible

        public boolean cacheIfPossible​(WebRequest request,
                                       WebResponse response,
                                       java.lang.Object toCache)
        Caches the specified object, if the corresponding request and response objects indicate that it is cacheable.
        Parameters:
        request - the request corresponding to the specified compiled script
        response - the response corresponding to the specified compiled script
        toCache - the object that is to be cached, if possible (may be for instance a compiled script or simply a WebResponse)
        Returns:
        whether the response was cached or not
      • cache

        public void cache​(java.lang.String css,
                          org.htmlunit.cssparser.dom.CSSStyleSheetImpl styleSheet)
        Caches the parsed version of the specified CSS snippet. We key the cache based on CSS snippets (rather than requests and responses as is done above) because a) this allows us to cache inline CSS, b) CSS is extremely expensive to parse, so we want to avoid it as much as possible, c) CSS files aren't usually nearly as large as JavaScript files, so memory bloat won't be too bad, and d) caching on requests and responses requires checking dynamically (see isCacheableContent(WebResponse)), and headers often aren't set up correctly, disallowing caching when in fact it should be allowed.
        Parameters:
        css - the CSS snippet from which styleSheet is derived
        styleSheet - the parsed version of css
      • deleteOverflow

        protected void deleteOverflow()
        Truncates the cache to the maximal number of entries.
      • isCacheable

        protected boolean isCacheable​(WebRequest request,
                                      WebResponse response)
        Determines if the specified response can be cached.
        Parameters:
        request - the performed request
        response - the received response
        Returns:
        true if the response can be cached
      • isCacheableContent

        protected boolean isCacheableContent​(WebResponse response)

        Perform prior validation for 'no-store' directive in Cache-Control header.

        Tries to guess if the content is dynamic or not.

        "Since origin servers do not always provide explicit expiration times, HTTP caches typically assign heuristic expiration times, employing algorithms that use other header values (such as the Last-Modified time) to estimate a plausible expiration time".

        The current implementation considers as dynamic content everything except responses with a Last-Modified header with a date older than 10 minutes or with an Expires header specifying expiration in more than 10 minutes.

        Parameters:
        response - the response to examine
        Returns:
        true if the response should be considered as cacheable
        See Also:
        RFC 7234, RFC 2616
      • getCurrentTimestamp

        protected long getCurrentTimestamp()
        Gets the current time stamp. As method to allow overriding it, when simulating another time.
        Returns:
        the current time stamp
      • parseDateHeader

        protected static java.util.Date parseDateHeader​(WebResponse response,
                                                        java.lang.String headerName)
        Parses and returns the specified date header of the specified response. This method returns null if the specified header cannot be found or cannot be parsed as a date.
        Parameters:
        response - the response
        headerName - the header name
        Returns:
        the specified date header of the specified response
      • getCachedResponse

        public WebResponse getCachedResponse​(WebRequest request)
        Returns the cached response corresponding to the specified request. If there is no corresponding cached object, this method returns null.

        Calculates and check if object still fresh(RFC 7234) otherwise returns null.

        Parameters:
        request - the request whose corresponding response is sought
        Returns:
        the cached response corresponding to the specified request if any
        See Also:
        RFC 7234
      • getCachedObject

        public java.lang.Object getCachedObject​(WebRequest request)
        Returns the cached object corresponding to the specified request. If there is no corresponding cached object, this method returns null.

        Calculates and check if object still fresh(RFC 7234) otherwise returns null.

        Parameters:
        request - the request whose corresponding cached compiled script is sought
        Returns:
        the cached object corresponding to the specified request if any
        See Also:
        RFC 7234
      • getCachedStyleSheet

        public org.htmlunit.cssparser.dom.CSSStyleSheetImpl getCachedStyleSheet​(java.lang.String css)
        Returns the cached parsed version of the specified CSS snippet. If there is no corresponding cached stylesheet, this method returns null.
        Parameters:
        css - the CSS snippet whose cached stylesheet is sought
        Returns:
        the cached stylesheet corresponding to the specified CSS snippet
      • getMaxSize

        public int getMaxSize()
        Returns the cache's maximum size. This is the maximum number of files that will be cached. The default is 25.
        Returns:
        the cache's maximum size
      • setMaxSize

        public void setMaxSize​(int maxSize)
        Sets the cache's maximum size. This is the maximum number of files that will be cached. The default is 25.
        Parameters:
        maxSize - the cache's maximum size (must be >= 0)
      • getSize

        public int getSize()
        Returns the number of entries in the cache.
        Returns:
        the number of entries in the cache
      • clear

        public void clear()
        Clears the cache.
      • clearOutdated

        public void clearOutdated()
        Removes outdated entries from the cache.