## com.gargoylesoftware.htmlunit
Class Cache

    java.lang.Object
      com.gargoylesoftware.htmlunit.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.

Version:
  $Revision: 4883 $
Author:
  Marc Guillemot, Daniel Gredler
See Also:
  Serialized Form

Constructor Summary
`Cache()`

Method Summary
`void` `cache(java.lang.String css, org.w3c.dom.css.CSSStyleSheet styleSheet)` Caches the parsed version of the specified CSS snippet.
`void` `cacheIfPossible(WebRequestSettings 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.
`protected void` `deleteOverflow()` Truncates the cache to the maximal number of entries.
`java.lang.Object` `getCachedObject(WebRequestSettings request)` Returns the cached object corresponding to the specified request.
`org.w3c.dom.css.CSSStyleSheet` `getCachedStyleSheet(java.lang.String css)` Returns the cached parsed version of the specified CSS snippet.
`int` `getMaxSize()` Returns the cache's maximum size.
`int` `getSize()` Returns the number of entries in the cache.
`protected boolean` `isCacheable(WebRequestSettings request, WebResponse response)` Determines if the specified response can be cached.
`protected boolean` `isDynamicContent(WebResponse response)` Tries to guess if the content is dynamic or not.
`protected boolean` `isJavaScript(WebResponse webResponse)` Returns `true` if the provided response is JavaScript content.
`protected 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 void cacheIfPossible(WebRequestSettings 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)

### cache

    public void cache(java.lang.String css,
                      org.w3c.dom.css.CSSStyleSheet 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 dynamicity (see `isDynamicContent(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(WebRequestSettings 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

### isDynamicContent

    protected boolean isDynamicContent(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.

  Parameters:
    `response` - the response to examine
  Returns:
    `true` if the response should be considered as dynamic and therefore uncacheable
  See Also:
    RFC 2616

### parseDateHeader

    protected 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

### isJavaScript

    protected boolean isJavaScript(WebResponse webResponse)

  Returns `true` if the provided response is JavaScript content. This method checks file extensions in addition to content types, because many web applications are badly configured and have incorrect headers.

  Parameters:
    `webResponse` - the response to analyze
  Returns:
    `true` if it can be considered as JavaScript

### getCachedObject

    public java.lang.Object getCachedObject(WebRequestSettings request)

  Returns the cached object corresponding to the specified request. If there is no corresponding cached object, this method returns `null`.

  Parameters:
    `request` - the request whose corresponding cached compiled script is sought
  Returns:
    the cached object corresponding to the specified request if any

### getCachedStyleSheet

    public org.w3c.dom.css.CSSStyleSheet 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.

Copyright © 2002-2011 Gargoyle Software Inc.. All Rights Reserved.