org.alfresco.repo.cache.lookup
Class EntityLookupCache

java.lang.Object
  extended by org.alfresco.repo.cache.lookup.EntityLookupCache

public class EntityLookupCache
extends java.lang.Object

A cache for two-way lookups of database entities. These are characterized by having a unique key (perhaps a database ID) and a separate unique key that identifies the object. If no cache is given, then all calls are passed through to the backing DAO.

The keys must have good equals and hashCode implementations and must respect the case-sensitivity of the use-case.

All keys will be unique to the given cache region, allowing the cache to be shared between instances of this class.

Generics:

Since:
3.2

Nested Class Summary
static interface EntityLookupCache.EntityLookupCallbackDAO
          Interface to support lookups of the entities using keys and values.
static class EntityLookupCache.EntityLookupCallbackDAOAdaptor
          Adaptor for implementations that support immutable entities.
 
Constructor Summary
EntityLookupCache(EntityLookupCache.EntityLookupCallbackDAO entityLookup)
          Construct the lookup cache without any cache.
EntityLookupCache(org.alfresco.repo.cache.SimpleCache cache, EntityLookupCache.EntityLookupCallbackDAO entityLookup)
          Construct the lookup cache, using the default cache region.
EntityLookupCache(org.alfresco.repo.cache.SimpleCache cache, java.lang.String cacheRegion, EntityLookupCache.EntityLookupCallbackDAO entityLookup)
          Construct the lookup cache, using the given cache region.
 
Method Summary
 void clear()
          Cache-only operation: Remove all cache entries

NOTE: This operation removes ALL entries for ALL cache regions.

 int deleteByKey(java.io.Serializable key)
          Delete the entity associated with the given key.
 int deleteByValue(java.lang.Object value)
          Delete the entity having the given value..
 org.alfresco.util.Pair getByKey(java.io.Serializable key)
          Find the entity associated with the given key.
 org.alfresco.util.Pair getByValue(java.lang.Object value)
          Find the entity associated with the given value.
 java.io.Serializable getKey(java.io.Serializable valueKey)
          Cache-only operation: Get the key for a given value key (note: not 'value' but 'value key').
 org.alfresco.util.Pair getOrCreateByValue(java.lang.Object value)
          Find the entity associated with the given value and create it if it doesn't exist.
 java.lang.Object getValue(java.io.Serializable key)
          Cache-only operation: Get the value for a given key
 void removeByKey(java.io.Serializable key)
          Cache-only operation: Remove all cache values associated with the given key.
 void removeByValue(java.lang.Object value)
          Cache-only operation: Remove all cache values associated with the given value
 void setValue(java.io.Serializable key, java.lang.Object value)
          Cache-only operation: Update the cache's value
 int updateValue(java.io.Serializable key, java.lang.Object value)
          Update the entity associated with the given key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EntityLookupCache

public EntityLookupCache(EntityLookupCache.EntityLookupCallbackDAO entityLookup)
Construct the lookup cache without any cache. All calls are passed directly to the underlying DAO entity lookup.

Parameters:
entityLookup - the instance that is able to find and persist entities

EntityLookupCache

public EntityLookupCache(org.alfresco.repo.cache.SimpleCache cache,
                         EntityLookupCache.EntityLookupCallbackDAO entityLookup)
Construct the lookup cache, using the default cache region.

Parameters:
cache - the cache that will back the two-way lookups
entityLookup - the instance that is able to find and persist entities

EntityLookupCache

public EntityLookupCache(org.alfresco.repo.cache.SimpleCache cache,
                         java.lang.String cacheRegion,
                         EntityLookupCache.EntityLookupCallbackDAO entityLookup)
Construct the lookup cache, using the given cache region.

All keys will be unique to the given cache region, allowing the cache to be shared between instances of this class.

Parameters:
cache - the cache that will back the two-way lookups; null to have no backing in a cache.
cacheRegion - the region within the cache to use.
entityLookup - the instance that is able to find and persist entities
Method Detail

getByKey

public org.alfresco.util.Pair getByKey(java.io.Serializable key)
Find the entity associated with the given key. The entity callback will be used if necessary.

It is up to the client code to decide if a null return value indicates a concurrency violation or not; the former would normally result in a concurrency-related exception such as ConcurrencyFailureException.

Parameters:
key - The entity key, which may be valid or invalid (null not allowed)
Returns:
Returns the key-value pair or null if the key doesn't reference an entity

getByValue

public org.alfresco.util.Pair getByValue(java.lang.Object value)
Find the entity associated with the given value. The entity callback will be used if no entry exists in the cache.

It is up to the client code to decide if a null return value indicates a concurrency violation or not; the former would normally result in a concurrency-related exception such as ConcurrencyFailureException.

Parameters:
value - The entity value, which may be valid or invalid (null is allowed)
Returns:
Returns the key-value pair or null if the value doesn't reference an entity

getOrCreateByValue

public org.alfresco.util.Pair getOrCreateByValue(java.lang.Object value)
Find the entity associated with the given value and create it if it doesn't exist. The EntityLookupCache.EntityLookupCallbackDAO.findByValue(Object) and EntityLookupCache.EntityLookupCallbackDAO.createValue(Object) will be used if necessary.

Parameters:
value - The entity value (null is allowed)
Returns:
Returns the key-value pair (new or existing and never null)

updateValue

public int updateValue(java.io.Serializable key,
                       java.lang.Object value)
Update the entity associated with the given key. The EntityLookupCache.EntityLookupCallbackDAO.updateValue(Serializable, Object) callback will be used if necessary.

It is up to the client code to decide if a 0 return value indicates a concurrency violation or not; usually the former will generate ConcurrencyFailureException or something recognised by the RetryingTransactionHelper.

Parameters:
key - The entity key, which may be valid or invalid (null not allowed)
value - The new entity value (may be null)
Returns:
Returns the row update count.

getKey

public java.io.Serializable getKey(java.io.Serializable valueKey)
Cache-only operation: Get the key for a given value key (note: not 'value' but 'value key').

Parameters:
value - The entity value key, which must be valid (null not allowed)
Returns:
The entity key (may be null)

getValue

public java.lang.Object getValue(java.io.Serializable key)
Cache-only operation: Get the value for a given key

Parameters:
key - The entity key, which may be valid or invalid (null not allowed)
Returns:
The entity value (may be null)

setValue

public void setValue(java.io.Serializable key,
                     java.lang.Object value)
Cache-only operation: Update the cache's value

Parameters:
key - The entity key, which may be valid or invalid (null not allowed)
value - The new entity value (may be null)

deleteByKey

public int deleteByKey(java.io.Serializable key)
Delete the entity associated with the given key. The EntityLookupCache.EntityLookupCallbackDAO.deleteByKey(Serializable) callback will be used if necessary.

It is up to the client code to decide if a 0 return value indicates a concurrency violation or not; usually the former will generate ConcurrencyFailureException or something recognised by the RetryingTransactionHelper.

Parameters:
key - the entity key, which may be valid or invalid (null not allowed)
Returns:
Returns the row deletion count

deleteByValue

public int deleteByValue(java.lang.Object value)
Delete the entity having the given value.. The EntityLookupCache.EntityLookupCallbackDAO.deleteByValue(Object) callback will be used if necessary.

It is up to the client code to decide if a 0 return value indicates a concurrency violation or not; usually the former will generate ConcurrencyFailureException or something recognised by the RetryingTransactionHelper.

Parameters:
key - the entity value, which may be valid or invalid (null allowed)
Returns:
Returns the row deletion count

removeByKey

public void removeByKey(java.io.Serializable key)
Cache-only operation: Remove all cache values associated with the given key.


removeByValue

public void removeByValue(java.lang.Object value)
Cache-only operation: Remove all cache values associated with the given value

Parameters:
value - The entity value (null is allowed)

clear

public void clear()
Cache-only operation: Remove all cache entries

NOTE: This operation removes ALL entries for ALL cache regions.



Copyright © 2005 - 2010 Alfresco Software, Inc. All Rights Reserved.