org.xwiki.gwt.user.client
Class Cache

java.lang.Object
  extended by org.xwiki.gwt.user.client.Cache

public class Cache
extends Object

Generic object cache that can be used to optimize the JavaScript execution. The cached objects are stored in a map and a reference to this map is saved as a property of a DOM element. Anyone who has access to the underlying DOM element has access to the cache.


Nested Class Summary
static interface Cache.CacheCallback<T>
          Interface used to access an object when it is not found in the cache.
 
Constructor Summary
Cache(com.google.gwt.core.client.JavaScriptObject cacheHolder)
          Creates a new cache that uses the given JavaScript object to store the cached objects.
 
Method Summary
 void clear(boolean disable)
          Clears the cache and optionally disables it.
<T> T
get(String key, Cache.CacheCallback<T> callback)
          Looks up the given key in the cache and returns the associated object if the key if found.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Cache

public Cache(com.google.gwt.core.client.JavaScriptObject cacheHolder)
Creates a new cache that uses the given JavaScript object to store the cached objects.

Parameters:
cacheHolder - the JavaScript object that holds the map with the cached objects
Method Detail

get

public <T> T get(String key,
                 Cache.CacheCallback<T> callback)
Looks up the given key in the cache and returns the associated object if the key if found. Otherwise caches the object returned by the provided call-back and returns it.

Type Parameters:
T - the type of the cached object
Parameters:
key - the key that identifies the requested object in the cache
callback - the call-back used to retrieve the requested object when it is not found in the cache
Returns:
the object associated with the given key in the cache if the cache contains such a key, otherwise the object returned by the provided call-back

clear

public void clear(boolean disable)
Clears the cache and optionally disables it.

Parameters:
disable - true to disable the cache, false to enable the cache