Package org.alfresco.repo.cache.lookup
Class EntityLookupCache<K extends java.io.Serializable,V,VK extends java.io.Serializable>
- java.lang.Object
-
- org.alfresco.repo.cache.lookup.EntityLookupCache<K,V,VK>
-
public class EntityLookupCache<K extends java.io.Serializable,V,VK extends java.io.Serializable> extends java.lang.ObjectA 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
equalsandhashCodeimplementations 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 Classes Modifier and Type Class Description static interfaceEntityLookupCache.EntityLookupCallbackDAO<K1 extends java.io.Serializable,V1,VK1 extends java.io.Serializable>Interface to support lookups of the entities using keys and values.static classEntityLookupCache.EntityLookupCallbackDAOAdaptor<K2 extends java.io.Serializable,V2,VK2 extends java.io.Serializable>Adaptor for implementations that support immutable entities.
-
Constructor Summary
Constructors Constructor Description EntityLookupCache(EntityLookupCache.EntityLookupCallbackDAO<K,V,VK> entityLookup)Construct the lookup cache without any cache.EntityLookupCache(org.alfresco.repo.cache.SimpleCache cache, java.lang.String cacheRegion, EntityLookupCache.EntityLookupCallbackDAO<K,V,VK> entityLookup)Construct the lookup cache, using the given cache region.EntityLookupCache(org.alfresco.repo.cache.SimpleCache cache, EntityLookupCache.EntityLookupCallbackDAO<K,V,VK> entityLookup)Construct the lookup cache, using thedefault cache region.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Cache-only operation: Remove all cache entriesorg.alfresco.util.Pair<K,V>createOrGetByValue(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..org.alfresco.util.Pair<K,V>getByKey(K key)Find the entity associated with the given key.org.alfresco.util.Pair<K,V>getByValue(V value)Find the entity associated with the given value.KgetKey(VK valueKey)Cache-only operation: Get the key for a given value key (note: not 'value' but 'value key').org.alfresco.util.Pair<K,V>getOrCreateByValue(V value)Find the entity associated with the given value and create it if it doesn't exist.VgetValue(K key)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 valuevoidsetValue(K key, V value)Cache-only operation: Update the cache's valueintupdateValue(K key, V value)Update the entity associated with the given key.
-
-
-
Constructor Detail
-
EntityLookupCache
public EntityLookupCache(EntityLookupCache.EntityLookupCallbackDAO<K,V,VK> 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<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(org.alfresco.repo.cache.SimpleCache cache, java.lang.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 Detail
-
getByKey
public org.alfresco.util.Pair<K,V> getByKey(K key)
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
public org.alfresco.util.Pair<K,V> getByValue(V value)
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
public org.alfresco.util.Pair<K,V> createOrGetByValue(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. 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
public org.alfresco.util.Pair<K,V> getOrCreateByValue(V value)
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
public int updateValue(K key, V value)
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.
-
getKey
public K getKey(VK valueKey)
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
public V getValue(K 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(K key, V 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(K key)
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
public int deleteByValue(V value)
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
public void removeByKey(K key)
Cache-only operation: Remove all cache values associated with the given key.
-
removeByValue
public void removeByValue(V 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.
-
-