com.atlassian.jira.util.cache
Class ManagedCache<K,V>

java.lang.Object
  extended by com.atlassian.jira.util.cache.ManagedCache<K,V>
Type Parameters:
K - The key of the cache.
V - The values of the cache.

public final class ManagedCache<K,V>
extends java.lang.Object

A concurrent, thread-safe cache suitible for use with cached Store/Persister or Manager implementations.

The cache is filled by calls to get(Object) by the use of a factory. The cache for a given key can be invalidated with remove(Object), or the whole invalidated with clear().

Important features:

Typical usage:
 public class CachedFooStore implements FooStore {
     private final ManagedCache<FooKey, Foo> cache = ManagedCache.newManagedCache(
         new Function<FooKey, Foo>()
         {
             public Foo apply(@Nullable FooKey key)
             {
                 return delegate.get(key);
             }
         }
     );
     ...
     public Foo getFoo(FooKey key)
     {
         return cache.get(key);
     }

     public void deleteFoo(FooKey key)
     {
         try
         {
             delegate.deleteFoo(key);
         }
         finally
         {
             cache.remove(key);
         }
      }
     public void updateFoo(FooKey key, Foo foo)
     {
         try
         {
             delegate.updateFoo(key, foo);
         }
         finally
         {
             cache.remove(key);
         }
      }
     public Foo createFoo(FooKey key, Foo foo)
     {
         try
         {
             return delegate.createFoo(key, foo);
         }
         finally
         {
             cache.remove(key);
         }
      }
 }
 

Since:
v4.2

Method Summary
 void clear()
          Remove all items from the cache.
 V get(K arg)
          Retrieve an item from the cache, or create it if not already in the cache.
static
<K,V> ManagedCache<K,V>
newManagedCache(com.google.common.base.Function<K,V> factory)
          Create a new cache.
static
<K,V,T> ManagedCache<K,V>
newManagedCache(com.google.common.base.Function<K,V> factory, com.google.common.base.Function<K,T> keyFactory)
          Create a new cache, where get/remove should be in terms of K, but the internal map should be keyed in terms of a value T (derived from K via keyFactory).
 void remove(K a)
          Remove an item from the cache.
 int size()
          Indicitive size of cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

newManagedCache

public static <K,V,T> ManagedCache<K,V> newManagedCache(com.google.common.base.Function<K,V> factory,
                                                        com.google.common.base.Function<K,T> keyFactory)
Create a new cache, where get/remove should be in terms of K, but the internal map should be keyed in terms of a value T (derived from K via keyFactory).

Type Parameters:
K - Keys of the cache.
V - Values of the cache.
T - Type of the keys in the internal map.
Parameters:
factory - factory for creating a new V when it is missing from the cache.
keyFactory - function for deriving the map key V from K

newManagedCache

public static <K,V> ManagedCache<K,V> newManagedCache(com.google.common.base.Function<K,V> factory)
Create a new cache.

Type Parameters:
K - Keys of the cache.
V - Values of the cache.
Parameters:
factory - factory for creating a new V when it is missing from the cache.

get

public V get(K arg)
Retrieve an item from the cache, or create it if not already in the cache.


remove

public void remove(K a)
Remove an item from the cache.


clear

public void clear()
Remove all items from the cache.


size

public int size()
Indicitive size of cache. If calls to get(Object) are in progress, and they call the factory and it returns null, then those nulls may be included in this count even though they will not be in the cache when the get() eventually returns.



Copyright © 2002-2010 Atlassian. All Rights Reserved.