T
- the type of EOEnterpriseObject in this cachepublic class ERXEnterpriseObjectCache<T extends EOEnterpriseObject> extends Object
ERXEnterpriseObjectCache<HelpText> helpTextCache = new ERXEnterpriseObjectCache<HelpText>("HelpText", "pageConfiguration");
...
HelpText helpText = helpTextCache.objectForKey(ec, "ListHelpText");
You can supply a timeout after which individual objects (or all objects if fetchInitialValues
is true
) get cleared and re-fetched. This implementation can cache either only the global IDs,
or the global ID and a copy of the actual object. Caching the actual object ensures that the snapshot stays around
and thus prevent additional trips to the database.
Listens to EOEditingContextDidSaveChanges notifications to track changes to objects in the cache and ClearCachesNotification
for messages to purge the cache.Modifier and Type | Field and Description |
---|---|
static String |
ClearCachesNotification
Other code can send this notification if it needs to have this cache discard all of the
objects that it has.
|
protected static EOGlobalID |
NO_GID_MARKER |
Constructor and Description |
---|
ERXEnterpriseObjectCache(Class c,
String keyPath)
Creates the cache for the entity implemented by the passed class 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,
EOQualifier qualifier,
long timeout,
boolean shouldRetainObjects,
boolean shouldFetchInitialValues)
Creates the cache for the given entity, keypath and timeout value in milliseconds.
|
ERXEnterpriseObjectCache(String entityName,
String keyPath,
EOQualifier qualifier,
long timeout,
boolean shouldRetainObjects,
boolean shouldFetchInitialValues,
boolean shouldReturnUnsavedObjects)
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.
|
Modifier and Type | Method and Description |
---|---|
void |
addObject(T eo)
Add an object to the cache using
eo.valueForKeyPath(keyPath()) as the key. |
void |
addObjectForKey(T eo,
Object key)
Add an object to the cache with the given key if it matches the qualifier, or
if there is no qualifier.
|
NSArray<T> |
allObjects(EOEditingContext ec)
Returns a list of all the objects currently in the cache and not yet expired.
|
NSArray<T> |
allObjects(EOEditingContext ec,
EOQualifier additionalQualifier)
Returns a list of all the objects currently in the cache and not yet expired which match additionalQualifier.
|
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)
Created an EORecord instance representing eo using its EOGlobalID.
|
protected ERXEC |
editingContext()
Returns the editing context that holds object that are in this cache.
|
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: the value for keyPath matches key
AND qualifier() (if not null).
|
protected void |
handleUnsuccessfullQueryForKey(Object key)
Called when a query hasn't found an entry in the cache.
|
protected String |
keyPath() |
T |
objectForKey(EOEditingContext ec,
Object key)
Retrieves an EO that matches the given key.
|
T |
objectForKey(EOEditingContext ec,
Object key,
boolean handleUnsuccessfulQueryForKey)
Retrieves an EO that matches the given key.
|
protected void |
preLoadCacheIfNeeded()
Loads all relevant objects into the cache if set to fetch initial values.
|
EOQualifier |
qualifier()
Returns the additional qualifier for this cache.
|
void |
removeObject(T eo)
Removes an object from the cache using
eo.valueForKeyPath(keyPath()) as the key. |
void |
removeObjectForKey(T eo,
Object key)
Removes the object associated with key from the cache.
|
void |
reset()
Resets the cache by clearing the internal map.
|
static void |
setApplicationDidFinishInitialization(boolean didFinish)
Called from
ERXExtensions.finishInitialization() to enable fetches. |
void |
setFetchInitialValues(boolean fetchInitialValues)
Sets whether or not the initial values should be fetched into
this cache or whether it 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()
Call this to re-start cache updating after stop() is called.
|
void |
stop()
Call this to stop cache updating.
|
T |
unsavedMatchingObject(EOEditingContext ec,
Object key)
Looks in ec for an newly inserted (unsaved) EO that matches the given key.
|
void |
updateObject(T eo)
Updates an object in the cache (adding if not present) using
eo.valueForKeyPath(keyPath()) as the key. |
void |
updateObjectForKey(T eo,
Object key)
Updates an object in the cache (adding if not present) with the given key if it
matches the qualifier, or if there is no qualifier.
|
public static String ClearCachesNotification
protected static final EOGlobalID NO_GID_MARKER
public ERXEnterpriseObjectCache(String entityName, String keyPath)
entityName
- name of the EOEntity to cachekeyPath
- key path to data uniquely identifying an instance of this entitypublic ERXEnterpriseObjectCache(Class c, String keyPath)
c
- Class used to identify which EOEntity this cache is forkeyPath
- key path to data uniquely identifying an instance of this entitypublic ERXEnterpriseObjectCache(String entityName, String keyPath, long timeout)
entityName
- name of the EOEntity to cachekeyPath
- key path to data uniquely identifying an instance of this entitytimeout
- time to live in milliseconds for an object in this cachepublic ERXEnterpriseObjectCache(String entityName, String keyPath, EOQualifier qualifier, long timeout)
true
after this constructor. You will almost certainly want to call
setResetOnChange(false);
.entityName
- name of the EOEntity to cachekeyPath
- key path to data uniquely identifying an instance of this entityqualifier
- EOQualifier restricting which instances are stored in this cachetimeout
- time to live in milliseconds for an object in this cachesetResetOnChange(boolean)
,
setFetchInitialValues(boolean)
public ERXEnterpriseObjectCache(String entityName, String keyPath, EOQualifier qualifier, long timeout, boolean shouldRetainObjects, boolean shouldFetchInitialValues)
entityName
- name of the EOEntity to cachekeyPath
- key path to data uniquely identifying an instance of this entityqualifier
- EOQualifier restricting which instances are stored in this cachetimeout
- time to live in milliseconds for an object in this cacheshouldRetainObjects
- true if this cache should retain the cached objects, false to keep only the GIDshouldFetchInitialValues
- true if the cache should be fully populated on first accesssetResetOnChange(boolean)
,
setFetchInitialValues(boolean)
,
setRetainObjects(boolean)
public ERXEnterpriseObjectCache(String entityName, String keyPath, EOQualifier qualifier, long timeout, boolean shouldRetainObjects, boolean shouldFetchInitialValues, boolean shouldReturnUnsavedObjects)
entityName
- name of the EOEntity to cachekeyPath
- key path to data uniquely identifying an instance of this entityqualifier
- EOQualifier restricting which instances are stored in this cachetimeout
- time to live in milliseconds for an object in this cacheshouldRetainObjects
- true if this cache should retain the cached objects, false to keep only the GIDshouldFetchInitialValues
- true if the cache should be fully populated on first accessshouldReturnUnsavedObjects
- true if unsaved matching objects should be returned, see unsavedMatchingObject(EOEditingContext, Object)
setResetOnChange(boolean)
,
setFetchInitialValues(boolean)
,
setRetainObjects(boolean)
public void start()
stop()
public void stop()
start()
public static void setApplicationDidFinishInitialization(boolean didFinish)
ERXExtensions.finishInitialization()
to enable fetches. This is to ensure that
migrations have run prior first fetch from this class.didFinish
- indicator if application did finish initialization phaseprotected ERXEC editingContext()
public void editingContextDidSaveChanges(NSNotification n)
_resetOnChange
is true
, this
calls reset() to discard the entire cache contents if an object of the given entity has been changed.
If _resetOnChange
is false
, this updates the cache to reflect the added/changed/removed
objects.n
- NSNotification with EOEditingContext as the object and a dictionary of changes in the userInfoEOEditingContext.ObjectsChangedInEditingContextNotification
,
reset()
public void clearCaches(NSNotification n)
n
- NSNotification with an entity nameClearCachesNotification
protected String entityName()
protected String keyPath()
protected ERXExpiringCache<Object,er.extensions.eof.ERXEnterpriseObjectCache.EORecord<T>> cache()
_fetchInitialValues
.protected er.extensions.eof.ERXEnterpriseObjectCache.EORecord<T> createRecord(EOGlobalID gid, T eo)
_retainObjects
,
this will also include an instance of the EO to ensure that the snapshot is retained.gid
- EOGlobalID of eoeo
- the EO to make an EORecord forprotected void preLoadCacheIfNeeded()
public void addObject(T eo)
eo.valueForKeyPath(keyPath())
as the key.eo
- the object to add to the cacheaddObjectForKey(EOEnterpriseObject, Object)
public void addObjectForKey(T eo, Object key)
eo
- eo the object to add to the cachekey
- the key to add the object underpublic void removeObject(T eo)
eo.valueForKeyPath(keyPath())
as the key.eo
- the object to remove from the cacheremoveObjectForKey(EOEnterpriseObject, Object)
public void removeObjectForKey(T eo, Object key)
eo
- eo the object to remove from the cache (ignored)key
- the key to remove the object forpublic void updateObject(T eo)
eo.valueForKeyPath(keyPath())
as the key.eo
- the object to update in the cacheupdateObjectForKey(EOEnterpriseObject, Object)
public void updateObjectForKey(T eo, Object key)
qualifier()
is not null, the object
is removed from the cache if it does not match the qualifier.eo
- eo the object to update in the cachekey
- the key of the object to updatepublic T objectForKey(EOEditingContext ec, Object key)
ec
- editing context to get the object intokey
- key value under which the object is registeredpublic T objectForKey(EOEditingContext ec, Object key, boolean handleUnsuccessfulQueryForKey)
_returnUnsavedObjects
is true
,
it attempts to find and return an unsaved object. If there is still no match
and handleUnsuccessfulQueryForKey
is true
,
it attempts to fetch the missing objects. Null is returned if
handleUnsuccessfulQueryForKey
is false
or no matching
object can be fetched.ec
- editing context to get the object intokey
- key value under which the object is registeredhandleUnsuccessfulQueryForKey
- if false, a cache miss returns null rather than fetchingpublic T unsavedMatchingObject(EOEditingContext ec, Object key)
ec
- editing context to search for unsaved, matching objectskey
- key value to identify the unsaved objectpublic NSArray<T> allObjects(EOEditingContext ec)
ec
- editing context to get the objects intopublic NSArray<T> allObjects(EOEditingContext ec, EOQualifier additionalQualifier)
ec
- editing context to get the objects intoadditionalQualifier
- qualifier to restrict which objects are returned from the cacheprotected void handleUnsuccessfullQueryForKey(Object key)
_fetchInitialValues
is false
, it will attempt to fetch the missing object and
adds it to the cache if it is found.
call addObject(EOEnterpriseObject)
on it.key
- the key of the object that was not found in the cacheprotected NSArray<T> fetchObjectsForKey(EOEditingContext editingContext, Object key)
editingContext
- the editing context to fetch inkey
- the key to fetch withpublic EOQualifier qualifier()
protected EOQualifier fetchObjectsQualifier(Object key)
key
- the key to fetchpublic void reset()
_fetchInitialValues
is true
, otherwise they are re-loaded on demand.preLoadCacheIfNeeded()
public void setFetchInitialValues(boolean fetchInitialValues)
fetchInitialValues
- if true, the initial values are fetched into the cachepublic void setReuseEditingContext(boolean reuseEditingContext)
reuseEditingContext
- whether or not the editing context for this cache is reused for multiple requestspublic void setRetainObjects(boolean retainObjects)
retainObjects
- if true, the EO's are retainedpublic void setResetOnChange(boolean resetOnChange)
resetOnChange
- if true, the cache will clear on changes; if false, the cache will update on changesCopyright © 2002 – 2024 Project Wonder.