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.w3c.dom.css.CSSStyleSheet 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.
|
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.w3c.dom.css.CSSStyleSheet |
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)
Tries to guess if the content is dynamic or not.
|
protected 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.w3c.dom.css.CSSStyleSheet 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 cssprotected 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)
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.
response
- the response to examinetrue
if the response should be considered as cacheableprotected long getCurrentTimestamp()
protected 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
.request
- the request whose corresponding response is soughtpublic Object getCachedObject(WebRequest request)
null
.request
- the request whose corresponding cached compiled script is soughtpublic org.w3c.dom.css.CSSStyleSheet getCachedStyleSheet(String css)
null
.css
- the CSS snippet whose cached stylesheet is soughtpublic int getMaxSize()
public void setMaxSize(int maxSize)
maxSize
- the cache's maximum size (must be >= 0)public int getSize()
public void clear()
Copyright © 2002–2016 Gargoyle Software Inc.. All rights reserved.