Project Wonder 5.0.0.8795

er.extensions.eof
Class ERXEnterpriseObjectCache<T extends EOEnterpriseObject>

java.lang.Object
  extended by er.extensions.eof.ERXEnterpriseObjectCache<T>
Type Parameters:
T -

public class ERXEnterpriseObjectCache<T extends EOEnterpriseObject>
extends Object

Caches one entity by a given key. Listens to EOEditingContextDidSaveChanges notifications to track changes. Typically you'd have an "identifier" property and you'd fetch values by:

 ERXEnterpriseObjectCache<HelpText> helpTextCache = new ERXEnterpriseObjectCache<HelpText>("HelpText", "pageConfiguration");
 ...
 HelpText helpText = helpTextCache.objectForKey(ec, "ListHelpText");
 
You can supply a timeout after which the cache is to get cleared and all the objects refetched. Note that this implementation only caches the global IDs, not the actual data.

Author:
ak inspired by a class from Dominik Westner

Field Summary
static String ClearCachesNotification
           
protected static EOGlobalID NO_GID_MARKER
           
 
Constructor Summary
ERXEnterpriseObjectCache(Class c, String keyPath)
          Creates the cache for the given entity name and the given keypath.
ERXEnterpriseObjectCache(String entityName, String keyPath)
          Creates the cache for the given entity name and the given keypath.
ERXEnterpriseObjectCache(String entityName, String keyPath, EOQualifier qualifier, long timeout)
          Creates the cache for the given entity, keypath and timeout value in milliseconds.
ERXEnterpriseObjectCache(String entityName, String keyPath, long timeout)
          Creates the cache for the given entity, keypath and timeout value in milliseconds.
 
Method Summary
 void addObject(T eo)
          Add an object to the cache.
 void addObjectForKey(T eo, Object key)
          Add an object to the cache with the given key.
protected  ERXExpiringCache<Object,er.extensions.eof.ERXEnterpriseObjectCache.EORecord<T>> cache()
          Returns the backing cache.
 void clearCaches(NSNotification n)
          Handler for the clearCaches notification.
protected  er.extensions.eof.ERXEnterpriseObjectCache.EORecord<T> createRecord(EOGlobalID gid, T eo)
           
protected  ERXEC editingContext()
           
 void editingContextDidSaveChanges(NSNotification n)
          Handler for the editingContextDidSaveChanges notification.
protected  String entityName()
           
protected  NSArray<T> fetchObjectsForKey(EOEditingContext editingContext, Object key)
          Actually performs a fetch for the given key.
protected  EOQualifier fetchObjectsQualifier(Object key)
          Returns the qualifier to use during for fetching.
protected  void handleUnsuccessfullQueryForKey(Object key)
          Called when a query hasn't found an entry in the cache.
protected  NSArray<T> initialObjects(EOEditingContext ec)
          Returns the objects to cache initially.
protected  String keyPath()
          The key path which should get used for the key of the cache.
 T objectForKey(EOEditingContext ec, Object key)
          Retrieves an EO that matches the given key or null if no match is in the cache.
 T objectForKey(EOEditingContext ec, Object key, boolean handleUnsuccessfulQueryForKey)
          Retrieves an EO that matches the given key or null if no match is in the cache.
 EOQualifier qualifier()
          Returns the additional qualifier for this cache.
 void removeObject(T eo)
           
 void removeObjectForKey(T eo, Object key)
           
 void reset()
          Resets the cache by clearing the internal map.
 void setFetchInitialValues(boolean fetchInitialValues)
          Sets whether or not the initial values should be fetched into this cache or whether or should lazy load.
 void setResetOnChange(boolean resetOnChange)
          Sets whether or not the cache is cleared when any change occurs.
 void setRetainObjects(boolean retainObjects)
          Sets whether or not the cached EO's themselves are retained versus just their GID's.
 void setReuseEditingContext(boolean reuseEditingContext)
          Sets whether or not the editing context for this cache is reused for multiple requests.
 void start()
           
 void stop()
           
 void updateObject(T eo)
           
 void updateObjectForKey(T eo, Object key)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ClearCachesNotification

public static String ClearCachesNotification

NO_GID_MARKER

protected static final EOGlobalID NO_GID_MARKER
Constructor Detail

ERXEnterpriseObjectCache

public ERXEnterpriseObjectCache(String entityName,
                                String keyPath)
Creates the cache for the given entity name and the given keypath. No timeout value is used.

Parameters:
entityName -
keyPath -

ERXEnterpriseObjectCache

public ERXEnterpriseObjectCache(Class c,
                                String keyPath)
Creates the cache for the given entity name and the given keypath. No timeout value is used.

Parameters:
c -
keyPath -

ERXEnterpriseObjectCache

public ERXEnterpriseObjectCache(String entityName,
                                String keyPath,
                                long timeout)
Creates the cache for the given entity, keypath and timeout value in milliseconds.

Parameters:
entityName -
keyPath -
timeout -

ERXEnterpriseObjectCache

public ERXEnterpriseObjectCache(String entityName,
                                String keyPath,
                                EOQualifier qualifier,
                                long timeout)
Creates the cache for the given entity, keypath and timeout value in milliseconds.

Parameters:
entityName -
keyPath -
qualifier -
timeout -
Method Detail

start

public void start()

stop

public void stop()

editingContext

protected ERXEC editingContext()

editingContextDidSaveChanges

public void editingContextDidSaveChanges(NSNotification n)
Handler for the editingContextDidSaveChanges notification. Calls reset if and object of the given entity were changed.

Parameters:
n -

clearCaches

public void clearCaches(NSNotification n)
Handler for the clearCaches notification. Calls reset if n.object is the entity name.

Parameters:
n -

entityName

protected String entityName()

keyPath

protected String keyPath()
The key path which should get used for the key of the cache.

Returns:
the cache keypath

cache

protected ERXExpiringCache<Object,er.extensions.eof.ERXEnterpriseObjectCache.EORecord<T>> cache()
Returns the backing cache. If the cache is to old, it is cleared first.

Returns:
the backing cache

createRecord

protected er.extensions.eof.ERXEnterpriseObjectCache.EORecord<T> createRecord(EOGlobalID gid,
                                                                              T eo)

initialObjects

protected NSArray<T> initialObjects(EOEditingContext ec)
Returns the objects to cache initially.

Parameters:
ec -
Returns:
the initial objects for the cache

addObject

public void addObject(T eo)
Add an object to the cache. Subclasses may want to override this if the object to reside under more than one entry (eg, title and identifier).

Parameters:
eo -

addObjectForKey

public void addObjectForKey(T eo,
                            Object key)
Add an object to the cache with the given key. The object can be null.

Parameters:
eo -
key -

removeObject

public void removeObject(T eo)

removeObjectForKey

public void removeObjectForKey(T eo,
                               Object key)

updateObject

public void updateObject(T eo)

updateObjectForKey

public void updateObjectForKey(T eo,
                               Object key)

objectForKey

public T objectForKey(EOEditingContext ec,
                      Object key)
Retrieves an EO that matches the given key or null if no match is in the cache.

Parameters:
ec - editing context to get the object into
key - key value under which the object is registered
Returns:
the matching object

objectForKey

public T objectForKey(EOEditingContext ec,
                      Object key,
                      boolean handleUnsuccessfulQueryForKey)
Retrieves an EO that matches the given key or null if no match is in the cache.

Parameters:
ec - editing context to get the object into
key - key value under which the object is registered
handleUnsuccessfulQueryForKey - if false, a cache miss returns null rather than fetching
Returns:
the matching object

handleUnsuccessfullQueryForKey

protected void handleUnsuccessfullQueryForKey(Object key)
Called when a query hasn't found an entry in the cache. This implementation puts a not-found marker in the cache so the next query will return null. You could override this method to create an EO with sensible default values and call addObject(EOEnterpriseObject) on it.

Parameters:
key -

fetchObjectsForKey

protected NSArray<T> fetchObjectsForKey(EOEditingContext editingContext,
                                        Object key)
Actually performs a fetch for the given key. Override this method to implement custom fetch rules.

Parameters:
editingContext - the editingcontext to fetch in
key - the key to fetch with
Returns:
the fetch objects

qualifier

public EOQualifier qualifier()
Returns the additional qualifier for this cache.

Returns:
the additional qualifier for this cache

fetchObjectsQualifier

protected EOQualifier fetchObjectsQualifier(Object key)
Returns the qualifier to use during for fetching.

Parameters:
key - the key to fetch
Returns:
the qualifier to use

reset

public void reset()
Resets the cache by clearing the internal map. When the next value is accessed, the objects are refetched.


setFetchInitialValues

public void setFetchInitialValues(boolean fetchInitialValues)
Sets whether or not the initial values should be fetched into this cache or whether or should lazy load. If turned off, resetOnChange will also be turned off.

Parameters:
fetchInitialValues - if true, the initial values are fetched into the cache

setReuseEditingContext

public void setReuseEditingContext(boolean reuseEditingContext)
Sets whether or not the editing context for this cache is reused for multiple requests.

Parameters:
reuseEditingContext - whether or not the editing context for this cache is reused for multiple requests

setRetainObjects

public void setRetainObjects(boolean retainObjects)
Sets whether or not the cached EO's themselves are retained versus just their GID's. If set, this implicitly sets reuseEditingContext to true.

Parameters:
retainObjects - if true, the EO's are retained

setResetOnChange

public void setResetOnChange(boolean resetOnChange)
Sets whether or not the cache is cleared when any change occurs. This requires fetching initial values (and will be turned on if you set this)

Parameters:
resetOnChange - if true, the cache will clear on changes; if false, the cache will update on changes

Last updated: Fri, Jan 9, 2009 • 04:35 AM EST

Copyright © 2002 – 2007 Project Wonder.