org.jboss.cache.util
Class Caches

java.lang.Object
  extended by org.jboss.cache.util.Caches

public class Caches
extends Object

Adaptors for Cache classes, such as Node. This is useful for integration of JBoss Cache into existing applications.

Example use:

 Cache c = ...;
 Map m = Caches.asMap(c);
 m.put("a", "b"); // null
 m.containsKey("a"); // true
 m.remove("a"); // "b"
 


Nested Class Summary
static class Caches.HashKeySelector<T>
          Class that returns a child name to use based on the hash code of a key.
static interface Caches.ChildSelector<T>
          Returns a segment (child node name) to use based on the characteristics of a key.
 
Method Summary
static
<K,V> Map<K,V>
asMap(Cache<K,V> cache)
          Returns a Map from the root node.
static
<K,V> Map<K,V>
asMap(Node<K,V> node)
          Returns a Map, where map keys are named children of the given Node, and values are kept under a single key for this node.
static
<K,V> Map<K,V>
asPartitionedMap(Cache<K,V> cache)
          Returns a Map, where map entries are partitioned into child nodes, within the cache root, by key hash code.
static
<K,V> Map<K,V>
asPartitionedMap(Node<K,V> node)
          Returns a Map, where map entries are partitioned into children nodes, within a cache node.
static
<K,V> Map<K,V>
asPartitionedMap(Node<K,V> node, Caches.ChildSelector<K> ss)
          Returns a Map, where map entries are partitioned into children, within a cache node, by key hash code.
static
<K,V> Map<K,V>
asSimpleMap(Node<K,V> node)
          Returns a Map, where map data is put and returned directly from a single Node.
static
<K,V> Set<V>
asSimpleSet(Node<K,V> node)
          Returns a Set, where set entries are data entries of the given Node.
protected static int hashCode(int i)
          Computes an improved hash code from an object's hash code.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

asMap

public static <K,V> Map<K,V> asMap(Cache<K,V> cache)
Returns a Map from the root node.

Parameters:
cache - cache to wrap as a map
Returns:
a map representation of the cache
See Also:
asMap(Node)

asMap

public static <K,V> Map<K,V> asMap(Node<K,V> node)
Returns a Map, where map keys are named children of the given Node, and values are kept under a single key for this node. The map may be safely concurrently modified through this Map or externally, and its contents reflect the cache state and existing data of the Node. This means that ConcurrentModificationException is never thrown and all methods are thread safe.

The map is not serializable.

Usage note: As a single node is used for every key, it is most efficient to store data for a single entity (e.g. Person) in a single object.

Also, when using a CacheLoader for storage, keys used must be valid as part of the Fqn used in calls. Generally speaking, simple string values are preferred.

Parameters:
node - node in a cache to wrap
Returns:
a Map representation of the cache

asSimpleMap

public static <K,V> Map<K,V> asSimpleMap(Node<K,V> node)
Returns a Map, where map data is put and returned directly from a single Node. This method is "simple" as data is kept under a single node. Note that storing all data in a single Node can be inefficient when using persistence, replication, or transactions. The map may be safely concurrently modified through this Map or externally. This means that ConcurrentModificationException is never thrown and all methods are thread safe.

The methods Map.entrySet() and Map.values() and Map.keySet() do not allow for modification of the Node. Further all these methods return a collection which is a snapshot (copy) of the data at time of calling. This may be very inefficient.

The map is not serializable.

Parameters:
node - node to wrap
Returns:
Map representation of the cache

asSimpleSet

public static <K,V> Set<V> asSimpleSet(Node<K,V> node)
Returns a Set, where set entries are data entries of the given Node. This method is "simple" as data is kept under a single node.

Note that storing all data in a single Node can be inefficient when using persistence, replication, or transactions. The set may be safely concurrently modified through this Map or externally. This means that ConcurrentModificationException is never thrown and all methods are thread safe.

The set is not serializable.

Parameters:
node - node to wrap
Returns:
a Set representation of the values in a node

asPartitionedMap

public static <K,V> Map<K,V> asPartitionedMap(Node<K,V> node)
Returns a Map, where map entries are partitioned into children nodes, within a cache node. The default child selector divides the data into 128 child nodes based on hash code. Note that for large data sets, the number of child nodes should be increased.

Parameters:
node - node to cache under
Returns:
a Map representation of the cache

asPartitionedMap

public static <K,V> Map<K,V> asPartitionedMap(Node<K,V> node,
                                              Caches.ChildSelector<K> ss)
Returns a Map, where map entries are partitioned into children, within a cache node, by key hash code.

The map is not serializable.

Usage note: This is a performance (and size) compromise between asMap(Node) and asSimpleMap(Node). For applications using a CacheLoader, asMap(Node) is a better choice.

Parameters:
node - node to cache under
ss - selector strategy that chooses a segment based on key
Returns:
a Map representation of the cache

asPartitionedMap

public static <K,V> Map<K,V> asPartitionedMap(Cache<K,V> cache)
Returns a Map, where map entries are partitioned into child nodes, within the cache root, by key hash code.

Parameters:
cache - cache to use
Returns:
a Map representation of the cache

hashCode

protected static final int hashCode(int i)
Computes an improved hash code from an object's hash code.



Copyright © 2008 JBoss, a division of Red Hat. All Rights Reserved.