com.atlassian.cache.impl
Class AbstractCacheManager

java.lang.Object
  extended by com.atlassian.cache.impl.AbstractCacheManager
All Implemented Interfaces:
CacheFactory, CacheManager
Direct Known Subclasses:
EhCacheManager, HazelcastCacheManager, MemoryCacheManager

public abstract class AbstractCacheManager
extends Object
implements CacheManager

A partial implementation of CacheManager.

Since:
2.0.6

Field Summary
protected  com.atlassian.util.concurrent.Function<String,com.atlassian.util.concurrent.ManagedLock> cacheCreationLocks
          Used to synchronize the creation of caches.
protected  ConcurrentMap<String,com.atlassian.util.concurrent.Supplier<ManagedCache>> caches
          Map of all the caches.
protected  CacheSettingsDefaultsProvider cacheSettingsDefaultsProvider
           
 
Constructor Summary
protected AbstractCacheManager(CacheSettingsDefaultsProvider cacheSettingsDefaultsProvider)
           
 
Method Summary
protected abstract
<K,V> ManagedCache
createComputingCache(String name, CacheSettings settings, CacheLoader<K,V> loader)
          Creates a cache that upon a miss is able to populate itself using the loader.
protected abstract  ManagedCache createSimpleCache(String name, CacheSettings settings)
          Creates a cache with no loader, i.e.
 void flushCaches()
          Flush the contents of all flushable caches registered with the cache manager.
<K,V> Cache<K,V>
getCache(Class<?> owningClass, String name)
          Returns the cache with the given name, creates it if necessary.
<K,V> Cache<K,V>
getCache(String name)
          Returns the cache with the given name, creates it if necessary.
<K,V> Cache<K,V>
getCache(String name, CacheLoader<K,V> loader)
          Returns the cache with the given name and loader, creates it if necessary.
<K,V> Cache<K,V>
getCache(String name, CacheLoader<K,V> loader, CacheSettings settings)
          Returns the cache with the given name, loader and required cache settings, creates it if necessary.
<K,V> Cache<K,V>
getCache(String name, Class<K> keyType, Class<V> valueType)
          Returns a cache with the given name, and the types specified.
<V> CachedReference<V>
getCachedReference(Class<?> owningClass, String name, Supplier<V> supplier)
          Returns a Cached Reference, creating it if necessary.
<V> CachedReference<V>
getCachedReference(Class<?> owningClass, String name, Supplier<V> supplier, CacheSettings settings)
          Returns a Cached Reference, creating it if necessary.
<V> CachedReference<V>
getCachedReference(String name, Supplier<V> supplier)
          Returns a Cached Reference, creating it if necessary.
 Collection<Cache<?,?>> getCaches()
          Gets the collection of caches managed.
 ManagedCache getManagedCache(String name)
          Returns the managed cache for the specified name.
 Collection<ManagedCache> getManagedCaches()
          Gets the collection of caches managed.
protected  CacheSettings mergeSettings(String name, CacheSettings settings)
           
 void shutdown()
          Shuts down and clean all data of the current instance and its all caches.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.atlassian.cache.CacheFactory
getCachedReference
 

Field Detail

caches

protected final ConcurrentMap<String,com.atlassian.util.concurrent.Supplier<ManagedCache>> caches
Map of all the caches. Before creating a cache to put into the map, acquire a lock using cacheCreationLocks to ensure that a cache is created only once.


cacheCreationLocks

protected final com.atlassian.util.concurrent.Function<String,com.atlassian.util.concurrent.ManagedLock> cacheCreationLocks
Used to synchronize the creation of caches.


cacheSettingsDefaultsProvider

protected final CacheSettingsDefaultsProvider cacheSettingsDefaultsProvider
Constructor Detail

AbstractCacheManager

protected AbstractCacheManager(CacheSettingsDefaultsProvider cacheSettingsDefaultsProvider)
Method Detail

getCaches

@Nonnull
public Collection<Cache<?,?>> getCaches()
Description copied from interface: CacheManager
Gets the collection of caches managed. This collection is not a "live" collection. It is a copy.

Specified by:
getCaches in interface CacheManager
Returns:
a collection of Caches. The returned collection will only include caches created via the getCache(...) methods. Caches created via the getCachedReference(...) methods are not included.

getManagedCaches

@Nonnull
public Collection<ManagedCache> getManagedCaches()
Description copied from interface: CacheManager
Gets the collection of caches managed. This collection is not a "live" collection. It is a copy. The returned collection will include caches created via both the getCache(...) and getCachedReference(...) methods.

Specified by:
getManagedCaches in interface CacheManager
Returns:
a collection of ManagedCaches.

getManagedCache

@Nullable
public ManagedCache getManagedCache(@Nonnull
                                             String name)
Description copied from interface: CacheManager
Returns the managed cache for the specified name.

Specified by:
getManagedCache in interface CacheManager
Parameters:
name - the name of the managed cache to retrieve
Returns:
the ManagedCache specified by name or null if no cache exists.

flushCaches

public void flushCaches()
Description copied from interface: CacheManager
Flush the contents of all flushable caches registered with the cache manager.

Specified by:
flushCaches in interface CacheManager
See Also:
CacheSettings.getFlushable()

getCache

@Nonnull
public <K,V> Cache<K,V> getCache(@Nonnull
                                         String name)
Description copied from interface: CacheFactory
Returns the cache with the given name, creates it if necessary. This is equivalent to calling CacheFactory.getCache(String, CacheLoader) with name and null.

Specified by:
getCache in interface CacheFactory
Parameters:
name - the name of the cache
Returns:
a Cache

getCache

@Nonnull
public <K,V> Cache<K,V> getCache(@Nonnull
                                         Class<?> owningClass,
                                         @Nonnull
                                         String name)
Description copied from interface: CacheFactory
Returns the cache with the given name, creates it if necessary.

It is equivalent to calling CacheFactory.getCache(String) with the computed name.

Specified by:
getCache in interface CacheFactory
Parameters:
owningClass - the owning class
name - the name of the cache spaced within the owningClass
Returns:
a Cache

getCache

@Nonnull
public <K,V> Cache<K,V> getCache(@Nonnull
                                         String name,
                                         @Nonnull
                                         Class<K> keyType,
                                         @Nonnull
                                         Class<V> valueType)
Description copied from interface: CacheFactory
Returns a cache with the given name, and the types specified. Creates it if necessary. If two caches of the same name are queried, with different types, the call with incorrect type arguments will throw a ClassCastException. For example with:

Cache firstCache = cacheManager.getCache("myCache", String.class, String.class);
Cache secondCache = cacheManager.getCache("myCache", String.class, Long.class);

the second call to getCache will result in a ClassCastException.

Specified by:
getCache in interface CacheFactory
Parameters:
name - the name of the cache
keyType - the type of keys in the cache. Must extend Serializable
valueType - the type of values in the cache. Must extend Serializable
Returns:
a Cache

getCache

@Nonnull
public <K,V> Cache<K,V> getCache(@Nonnull
                                         String name,
                                         CacheLoader<K,V> loader)
Description copied from interface: CacheFactory
Returns the cache with the given name and loader, creates it if necessary. This is equivalent to calling CacheFactory.getCache(String, CacheLoader, CacheSettings) with name, null and new CacheSettingsBuilder().build().

Specified by:
getCache in interface CacheFactory
Parameters:
name - the name of the cache
loader - the loader that will be used to provide values for keys that will be added to the cache. null indicates no loader
Returns:
a Cache

getCachedReference

@Nonnull
public <V> CachedReference<V> getCachedReference(@Nonnull
                                                         String name,
                                                         @Nonnull
                                                         Supplier<V> supplier)
Description copied from interface: CacheFactory
Returns a Cached Reference, creating it if necessary.

Specified by:
getCachedReference in interface CacheFactory
Parameters:
name - the name of the Cached Reference
supplier - the supplier for value to be cached, called if the value needs to be generated
Returns:
the Cached Reference

getCachedReference

@Nonnull
public <V> CachedReference<V> getCachedReference(@Nonnull
                                                         Class<?> owningClass,
                                                         @Nonnull
                                                         String name,
                                                         @Nonnull
                                                         Supplier<V> supplier)
Description copied from interface: CacheFactory
Returns a Cached Reference, creating it if necessary.

It is equivalent to calling CacheFactory.getCachedReference(String, Supplier, CacheSettings)

Specified by:
getCachedReference in interface CacheFactory
Parameters:
owningClass - the owning class
name - the name of the cache spaced within the owningClass
supplier - the supplier for value to be cached, called if the value needs to be generated
Returns:
the Cached Reference

getCachedReference

@Nonnull
public <V> CachedReference<V> getCachedReference(@Nonnull
                                                         Class<?> owningClass,
                                                         @Nonnull
                                                         String name,
                                                         @Nonnull
                                                         Supplier<V> supplier,
                                                         @Nonnull
                                                         CacheSettings settings)
Description copied from interface: CacheFactory
Returns a Cached Reference, creating it if necessary.

It is equivalent to calling CacheFactory.getCachedReference(String, Supplier, CacheSettings)

Specified by:
getCachedReference in interface CacheFactory
Parameters:
owningClass - the owning class
name - the name of the cache spaced within the owningClass
supplier - the supplier for value to be cached, called if the value needs to be generated
settings - specifies the required cache settings
Returns:
the Cached Reference

getCache

@Nonnull
public <K,V> Cache<K,V> getCache(@Nonnull
                                         String name,
                                         CacheLoader<K,V> loader,
                                         @Nonnull
                                         CacheSettings settings)
Description copied from interface: CacheFactory
Returns the cache with the given name, loader and required cache settings, creates it if necessary.

Specified by:
getCache in interface CacheFactory
Parameters:
name - the name of the cache
loader - the loader that will be used to provide values for keys that will be added to the cache. null indicates no loader
settings - the cache settings that are required to be set if the cache is created.
Returns:
a Cache

createComputingCache

protected abstract <K,V> ManagedCache createComputingCache(@Nonnull
                                                           String name,
                                                           @Nonnull
                                                           CacheSettings settings,
                                                           CacheLoader<K,V> loader)
Creates a cache that upon a miss is able to populate itself using the loader.

Returns:
a non-null cache

createSimpleCache

protected abstract ManagedCache createSimpleCache(@Nonnull
                                                  String name,
                                                  @Nonnull
                                                  CacheSettings settings)
Creates a cache with no loader, i.e. one populated via explicit puts.

Parameters:
name - the name to give the cache (required)
Returns:
a non-null cache

mergeSettings

protected CacheSettings mergeSettings(String name,
                                      CacheSettings settings)

shutdown

public void shutdown()
Description copied from interface: CacheManager
Shuts down and clean all data of the current instance and its all caches.

Specified by:
shutdown in interface CacheManager


Copyright © 2015 Atlassian. All rights reserved.