The following ATGMobileCommon
classes are used by the various Managers that need caching. For example, ATGProductManager
uses ATGRepositoryCoreDataCache
because it is caching products and SKUs that are repository items and ATGProfileManager
uses ATGMemoryCache
for its order caching.
ATGCache
is the protocol that each Oracle Commerce caches implement, enabling you to get the cache to follow the protocol without having to know if it is in core data or in-memory backed.ATGCacheManagedDocument
is the shared core data storage document, a wrapper around the Apple SQL data storage.You can instantiate the
ATGCoreDataCache
class when you want aCoreData
backed cache. It implements theATGCache
protocol.You can instantiate the
ATGMemoryCache
class when you want an in-memory cache. It implements theATGCache
protocol.ATGRepositoryCoreDataCache
is a subclass ofATGCoreDataCache
that you can use when you are caching repository items. It can help you manage items that have arepositoryId
property.
Implementation
A singleton object of ATGMemoryCache
is used so any object that needs to interact with the in-memory cache can, for example, pass ATGMemoryCache sharedInstance
for access to the centralized cache.
This class broadcasts notifications for other objects, for example, if some view controllers might be interested in knowing when certain objects are removed from the cache so they can reset their state. This is implemented only for when the user’s profile is deleted, but this can be easily extended by customers to include other events. Below is the current implementation.
ATGMemoryCache.m
- (void) broadcastClearCacheNotifications:(NSString *)pID cache:(NSCache *) cache { // Broadcast a notification if we are about to delete the user's profile. If pID is nil this means we are clearing // everything in the cache, so broadcast a notification in this case too. if (pID == nil || [pID isEqualToString:ATG_USER_PROFILE_CACHE_ID ]) { ATGMemoryCacheItem *userProfile = [cache objectForKey:pID]; if (userProfile) { [[NSNotificationCenter defaultCenter] postNotificationName:ATGMemoryCacheWillClearUserProfileNotification object:nil]; } }}
You can add other “if” statements to include the clear cache events they want to broadcast. Also, if they need to broadcast notifications for when items added to the cache they can just follow this clear cache example.