Package org.alfresco.repo.cache.lookup
Class EntityLookupCache<K extends Serializable,V,VK extends Serializable>
java.lang.Object
org.alfresco.repo.cache.lookup.EntityLookupCache<K,V,VK>
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:
- K: The database unique identifier.
- V: The value stored against K.
- VK: The a value-derived key that will be used as a cache key when caching K for lookups by V. This can be the value itself if it is itself a good key.
- Since:
- 3.2
- Author:
- Derek Hulley
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceEntityLookupCache.EntityLookupCallbackDAO<K1 extends Serializable,V1, VK1 extends Serializable> Interface to support lookups of the entities using keys and values.static classEntityLookupCache.EntityLookupCallbackDAOAdaptor<K2 extends Serializable,V2, VK2 extends Serializable> Adaptor for implementations that support immutable entities. -
Constructor Summary
ConstructorsConstructorDescriptionEntityLookupCache(EntityLookupCache.EntityLookupCallbackDAO<K, V, VK> entityLookup) Construct the lookup cache without any cache.EntityLookupCache(SimpleCache cache, String cacheRegion, EntityLookupCache.EntityLookupCallbackDAO<K, V, VK> entityLookup) Construct the lookup cache, using the given cache region.EntityLookupCache(SimpleCache cache, EntityLookupCache.EntityLookupCallbackDAO<K, V, VK> entityLookup) Construct the lookup cache, using thedefault cache region. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Cache-only operation: Remove all cache entriescreateOrGetByValue(V value, ControlDAO controlDAO) Attempt to create the entity and, failing that, look it up.
This method takes the opposite approach togetOrCreateByValue(Object), which assumes the entity's existence: in this case the entity is assumed to NOT exist.intdeleteByKey(K key) Delete the entity associated with the given key.intdeleteByValue(V value) Delete the entity having the given value..Find the entity associated with the given key.getByValue(V value) Find the entity associated with the given value.getCachedEntityByValue(V value) Find the entity associated with the given value if its cachedCache-only operation: Get the key for a given value key (note: not 'value' but 'value key').getOrCreateByValue(V value) Find the entity associated with the given value and create it if it doesn't exist.Cache-only operation: Get the value for a given keyvoidremoveByKey(K key) Cache-only operation: Remove all cache values associated with the given key.voidremoveByValue(V value) Cache-only operation: Remove all cache values associated with the given valuevoidCache-only operation: Update the cache's valueintupdateValue(K key, V value) Update the entity associated with the given key.
-
Constructor Details
-
EntityLookupCache
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(SimpleCache cache, EntityLookupCache.EntityLookupCallbackDAO<K, V, VK> entityLookup) Construct the lookup cache, using thedefault cache region.- Parameters:
cache- the cache that will back the two-way lookupsentityLookup- the instance that is able to find and persist entities
-
EntityLookupCache
public EntityLookupCache(SimpleCache cache, String cacheRegion, EntityLookupCache.EntityLookupCallbackDAO<K, V, VK> 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 Details
-
getByKey
Find the entity associated with the given key. Theentity callbackwill 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 asConcurrencyFailureException.- 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
Find the entity associated with the given value. Theentity callbackwill 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 asConcurrencyFailureException.- 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
-
createOrGetByValue
Attempt to create the entity and, failing that, look it up.
This method takes the opposite approach togetOrCreateByValue(Object), which assumes the entity's existence: in this case the entity is assumed to NOT exist. TheEntityLookupCache.EntityLookupCallbackDAO.createValue(Object)andEntityLookupCache.EntityLookupCallbackDAO.findByValue(Object)will be used if necessary.
Use this method when the data involved is seldom reused.- Parameters:
value- The entity value (null is allowed)controlDAO- an essential DAO required in order to ensure a transactionally-safe attempt at data creation- Returns:
- Returns the key-value pair (new or existing and never null)
-
getOrCreateByValue
Find the entity associated with the given value and create it if it doesn't exist. TheEntityLookupCache.EntityLookupCallbackDAO.findByValue(Object)andEntityLookupCache.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
Update the entity associated with the given key. TheEntityLookupCache.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 generateConcurrencyFailureExceptionor something recognised by theRetryingTransactionHelper.- 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.
-
getCachedEntityByValue
Find the entity associated with the given value if its cached- Parameters:
value- The entity value (null is not allowed)- Returns:
- Returns the key-value pair (existing or null)
-
getKey
Cache-only operation: Get the key for a given value key (note: not 'value' but 'value key').- Parameters:
valueKey- The entity value key, which must be valid (null not allowed)- Returns:
- The entity key (may be null)
-
getValue
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
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
Delete the entity associated with the given key. TheEntityLookupCache.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 generateConcurrencyFailureExceptionor something recognised by theRetryingTransactionHelper.- Parameters:
key- the entity key, which may be valid or invalid (null not allowed)- Returns:
- Returns the row deletion count
-
deleteByValue
Delete the entity having the given value.. TheEntityLookupCache.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 generateConcurrencyFailureExceptionor something recognised by theRetryingTransactionHelper.- Parameters:
value- the entity value, which may be valid or invalid (null allowed)- Returns:
- Returns the row deletion count
-
removeByKey
Cache-only operation: Remove all cache values associated with the given key. -
removeByValue
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.
-