public class Cache extends Object implements 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.
Constructor and Description |
---|
Cache() |
Modifier and Type | Method and Description |
---|---|
void |
cache(String css,
org.htmlunit.cssparser.dom.CSSStyleSheetImpl styleSheet)
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 |
clearOutdated()
Removes outdated entries from the cache.
|
protected void |
deleteOverflow()
Truncates the cache to the maximal number of entries.
|
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(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 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.
|
public boolean cacheIfPossible(WebRequest request, WebResponse response, Object toCache)
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)public void cache(String css, org.htmlunit.cssparser.dom.CSSStyleSheetImpl styleSheet)
isCacheableContent(WebResponse)
), and headers often
aren't set up correctly, disallowing caching when in fact it should be allowed.css
- the CSS snippet from which styleSheet
is derivedstyleSheet
- the parsed version of css
protected void deleteOverflow()
protected boolean isCacheable(WebRequest request, WebResponse response)
request
- the performed requestresponse
- the received responsetrue
if the response can be cachedprotected 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.
protected long getCurrentTimestamp()
protected static Date parseDateHeader(WebResponse response, String headerName)
null
if the specified header cannot be found or cannot be parsed as a date.response
- the responseheaderName
- the header namepublic WebResponse getCachedResponse(WebRequest request)
null
.
Calculates and check if object still fresh(RFC 7234) otherwise returns null
.
request
- the request whose corresponding response is soughtpublic Object getCachedObject(WebRequest request)
null
.
Calculates and check if object still fresh(RFC 7234) otherwise returns null
.
request
- the request whose corresponding cached compiled script is soughtpublic org.htmlunit.cssparser.dom.CSSStyleSheetImpl getCachedStyleSheet(String css)
null
.css
- the CSS snippet whose cached stylesheet is soughtpublic int getMaxSize()
25
.public void setMaxSize(int maxSize)
25
.maxSize
- the cache's maximum size (must be >= 0)public int getSize()
public void clear()
public void clearOutdated()
Copyright © 2002–2024 Gargoyle Software Inc.. All rights reserved.