Class Cache
- All Implemented Interfaces:
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:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Caches the parsed version of the specified CSS snippet.boolean
cacheIfPossible
(WebRequest request, WebResponse response, Object toCache) Caches the specified object, if the corresponding request and response objects indicate that it is cacheable.void
clear()
Clears the cache.void
Removes outdated entries from the cache.protected void
Truncates the cache to the maximal number of entries.getCachedObject
(WebRequest request) Returns the cached object corresponding to the specified request.getCachedResponse
(WebRequest request) Returns the cached response corresponding to the specified request.org.htmlunit.cssparser.dom.CSSStyleSheetImpl
Returns the cached parsed version of the specified CSS snippet.protected long
Gets the current time stamp.int
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 Date
parseDateHeader
(WebResponse response, String headerName) Parses and returns the specified date header of the specified response.void
setMaxSize
(int maxSize) Sets the cache's maximum size.
-
Constructor Details
-
Cache
public Cache()
-
-
Method Details
-
cacheIfPossible
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 scriptresponse
- the response corresponding to the specified compiled scripttoCache
- 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
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 (seeisCacheableContent(WebResponse)
), and headers often aren't set up correctly, disallowing caching when in fact it should be allowed.- Parameters:
css
- the CSS snippet from whichstyleSheet
is derivedstyleSheet
- the parsed version ofcss
-
deleteOverflow
protected void deleteOverflow()Truncates the cache to the maximal number of entries. -
isCacheable
Determines if the specified response can be cached.- Parameters:
request
- the performed requestresponse
- the received response- Returns:
true
if the response can be cached
-
isCacheableContent
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 anExpires
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:
-
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
Parses and returns the specified date header of the specified response. This method returnsnull
if the specified header cannot be found or cannot be parsed as a date.- Parameters:
response
- the responseheaderName
- the header name- Returns:
- the specified date header of the specified response
-
getCachedResponse
Returns the cached response corresponding to the specified request. If there is no corresponding cached object, this method returnsnull
.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:
-
getCachedObject
Returns the cached object corresponding to the specified request. If there is no corresponding cached object, this method returnsnull
.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:
-
getCachedStyleSheet
Returns the cached parsed version of the specified CSS snippet. If there is no corresponding cached stylesheet, this method returnsnull
.- 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 is25
.- 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 is25
.- 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.
-