# com.bea.content.manager.servlets
NodeCache Class

public class NodeCache
extends `Object`
implements `HttpSessionBindingListener`, `Serializable`

An object to help with caching a Node or a array of Nodes.

This object can placed anywhere for use as a cache. For convience, the `getNodeCache``` method can be used with a JSP PageContext to create and utilize ContentCaches.

Additionally, this is what the `SearchTag``` and `GetNodeTag``` tags use when useCache="true".

Hierarchy
  `Object`
  `NodeCache`

All Implemented Interfaces
  `EventListener`, `HttpSessionBindingListener`, `Serializable`

## Field Summary

`public static final `String`` | `DEF_CACHE_NAME` The default name of the NodeCache.
`protected `Map`` | `nodeCache` The cache to NodeHolder.

## Constructor Summary

NodeCache() Constructor.

## Method Summary

`public static void` | `bindToSession`(`NodeCache` cache, `HttpSession` session) Bind a NodeCache to an HttpSession.
`public synchronized void` | `clear`() Clear the entire cache.
`public static `Object`` | `getAttribute`(`PageContext` ctx, `String` name, int scope) Get a scoped attribute correctly from the JSP page context.
`public synchronized long` | `getCachedTime`(`String` id) Get the UTC time (in ms.) of when the nodes at id were cached.
`public synchronized `List`` | `getCacheIds`() Get an unordered list of all the cache ids.
`public synchronized long` | `getCacheSize`() Get the total number of nodes objects cached (across all ids).
`public static `NodeCache`` | `getFromSession`(`HttpSession` session, boolean create) Get a NodeCache from an HttpSession.
`public synchronized `Node`` | `getNode`(`String` id, long timeout) Check the cache for the node.
`public static `NodeCache`` | `getNodeCache`(`PageContext` ctx, int scope, boolean create) Get a NodeCache from the specified JSP context.
`public static `NodeCache`` | `getNodeCache`(`PageContext` ctx, `String` scopeStr, boolean create) Get a NodeCache from the specified JSP context.
`public synchronized `Node`[]` | `getNodes`(`String` id, long timeout) Check the cache for the nodes.
`public synchronized int` | `getNumCacheIds`() Get the number of cache ids in the cache.
`public static `String`` | `getScopeName`(int scope) Get the scope name based upon the scope id.
`public synchronized `Node`[]` | `remove`(`String` id) Clear a particular entry from the cache.
`public static void` | `removeAttribute`(`PageContext` ctx, `String` name, int scope) Remove a scoped attribute correctly from the JSP page context.
`public static void` | `setAttribute`(`PageContext` ctx, `String` name, `Object` val, int scope) Set a scoped attribute correctly in the JSP page context.
`public synchronized void` | `update`(`String` id, `Node`[] nodes) Update the cache with the nodes.
`public synchronized void` | `update`(`String` id, `Node` node) Update the cache with the Node.
`public void` | `valueBound`(`HttpSessionBindingEvent` evt) Called when we're bound into an HttpSession.
`public void` | `valueUnbound`(`HttpSessionBindingEvent` evt) Called when we're unbound from an HttpSession, and clears the cache.

Methods from class `java.lang.`Object``
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Methods from interface `javax.servlet.http.`HttpSessionBindingListener``
`valueBound, valueUnbound`

## Field Detail

### DEF_CACHE_NAME

    public static final String DEF_CACHE_NAME

  The default name of the NodeCache.

  When the `NodeCache.getNodeCache(PageContext, int, boolean)``` method is used, this is the name which will used.

### nodeCache

    protected Map nodeCache

  The cache to NodeHolder.

## Constructor Detail

### NodeCache

    public NodeCache()

  Constructor.

## Method Detail

### bindToSession(NodeCache, HttpSession) Method

    public static void bindToSession(NodeCache cache,
                                     HttpSession session)

Bind a NodeCache to an HttpSession.

This will use `DEF_CACHE_NAME``` as the session attribute name for the cache.

#### Parameters

cache
  the nodes cache object.

session
  the http session.

### clear() Method

    public synchronized void clear()

Clear the entire cache.

### getAttribute(PageContext, String, int) Method

    public static Object getAttribute(PageContext ctx,
                                      String name,
                                      int scope)

Get a scoped attribute correctly from the JSP page context.

#### Parameters

ctx
  the JSP page context.

name
  the attribute name.

scope
  the attribute scope (PageContext.APPLICATION_SCOPE, PageContext.PAGE_SCOPE, PageContext.REQUEST_SCOPE, PageContext.SESSION_SCOPE).

### getCachedTime(String) Method

    public synchronized long getCachedTime(String id)

Get the UTC time (in ms.) of when the nodes at id were cached.

#### Parameters

id
  the cache id of the nodes.

#### Returns

the time, or -1 if id is not a valid cache id.

### getCacheIds() Method

    public synchronized List getCacheIds()

Get an unordered list of all the cache ids.

Since the keys will all be Strings, this can be sorted with `Collections.sort(List)```.

### getCacheSize() Method

    public synchronized long getCacheSize()

Get the total number of nodes objects cached (across all ids).

### getFromSession(HttpSession, boolean) Method

    public static NodeCache getFromSession(HttpSession session,
                                           boolean create)

Get a NodeCache from an HttpSession.

This will retrieve the cache from the session attribute named `DEF_CACHE_NAME```. If that is set and is not a NodeCache will be removed.

#### Parameters

session
  the http session.

create
  true to create a NodeCache, if it's not there, false to not and return null.

#### Returns

the cache if it exists or create was true, null if it didn't exist and create was false.

### getNode(String, long) Method

    public synchronized Node getNode(String id,
                                     long timeout)

Check the cache for the node.

Getting a node from the cache does not update its time in terms of a timeout. Only using the `NodeCache.update(String, Node[])``` method to update the cache will do that.

#### Parameters

id
  the cache id of the node.

timeout
  the timeout in ms. for the nodes (less than 0 for no timeout).

#### Returns

the Node, or null if not found or has timed out.

### getNodeCache(PageContext, int, boolean) Method

    public static NodeCache getNodeCache(PageContext ctx,
                                         int scope,
                                         boolean create)

Get a NodeCache from the specified JSP context.

This will retrieve the NodeCache from the JSP context based upon the specified scope at the attribute named `DEF_CACHE_NAME```. If that is set and is not a NodeCache will be removed.

#### Parameters

ctx
  the JSP page context.

scope
  the cache scope (one of PageContext.APPLICATION_SCOPE, PageContext.PAGE_SCOPE, PageContext.REQUEST_SCOPE, or PageContext.SESSION_SCOPE).

create
  true to create a NodeCache it's not there, false to not and return null.

#### Returns

the cache if it exists or create was true, null if it didn't exist and create was false.

### getNodeCache(PageContext, String, boolean) Method

    public static NodeCache getNodeCache(PageContext ctx,
                                         String scopeStr,
                                         boolean create)

Get a NodeCache from the specified JSP context.

If scopeStr is "application", then PageContext.APPLICATION_SCOPE is used; if scopeStr is "page", then PageContext.PAGE_SCOPE is used; if scopeStr is "request", then PageContext.REQUEST_SCOPE is used; if scopeStr is "session", then PageContext.SESSION_SCOPE is used; otherwise, scopeStr is passed to ctx.getAttributesScope() which will determine the scope to use.

#### Parameters

ctx
  the JSP page context.

scopeStr
  the scope name.

create
  true to create a NodeCache it's not there, false to not and return null.

#### Returns

the cache if it exists or create was true, null if it didn't exist and create was false.

### getNodes(String, long) Method

    public synchronized Node[] getNodes(String id,
                                      long timeout)

Check the cache for the nodes.

Getting nodes from the cache does not update its time in terms of a timeout. Only using the `NodeCache.update(String, Node[])``` method to update the cache will do that.

#### Parameters

id
  the cache id of the nodes.

timeout
  the timeout in ms. for the nodes (less than 0 for no timeout).

#### Returns

the array of nodes, or null if not found or has timed out.

### getNumCacheIds() Method

    public synchronized int getNumCacheIds()

Get the number of cache ids in the cache.

### getScopeName(int) Method

    public static String getScopeName(int scope)

Get the scope name based upon the scope id.

### remove(String) Method

    public synchronized Node[] remove(String id)

Clear a particular entry from the cache.

#### Parameters

id
  the cache id of the nodes.

#### Returns

the cached nodes at the id, or null if none.

### removeAttribute(PageContext, String, int) Method

    public static void removeAttribute(PageContext ctx,
                                       String name,
                                       int scope)

Remove a scoped attribute correctly from the JSP page context.

#### Parameters

ctx
  the JSP page context.

name
  the attribute name.

scope
  the attribute scope (PageContext.APPLICATION_SCOPE, PageContext.PAGE_SCOPE, PageContext.REQUEST_SCOPE, PageContext.SESSION_SCOPE).

### setAttribute(PageContext, String, Object, int) Method

    public static void setAttribute(PageContext ctx,
                                    String name,
                                    Object val,
                                    int scope)

Set a scoped attribute correctly in the JSP page context.

#### Parameters

ctx
  the JSP page context.

name
  the attribute name.

val
  the attribute value.

scope
  the attribute scope (PageContext.APPLICATION_SCOPE, PageContext.PAGE_SCOPE, PageContext.REQUEST_SCOPE, PageContext.SESSION_SCOPE).

### update(String, Node[]) Method

    public synchronized void update(String id,
                                    Node[] nodes)

Update the cache with the nodes.

This can be used for new or existing nodes in the cache.

#### Parameters

id
  cache id of the nodes.

nodes
  the nodes (null to remove).

### update(String, Node) Method

    public synchronized void update(String id,
                                    Node node)

Update the cache with the Node.

This can be used for new or existing Nodes in the cache.

#### Parameters

id
  cache id of the Node.

node
  the Node (null to remove).

### valueBound(HttpSessionBindingEvent) Method

    public void valueBound(HttpSessionBindingEvent evt)

Called when we're bound into an HttpSession.

### valueUnbound(HttpSessionBindingEvent) Method

    public void valueUnbound(HttpSessionBindingEvent evt)

Called when we're unbound from an HttpSession, and clears the cache.

Related Topics

``NodeCache.clear()``