Implementation
Introduction
You can implement the Cache Manager functionality in your own functionality by implementing the CacheBuilder and CacheManagerI interfaces in any functionality you need.
CacheBuilder & CacheManagerI Interface
Any functionality that would need to cache any kind of information should do it using the CacheBuilder standard interface. In other words, it should implement the CacheBuilder by implementing the doLoad method.
Additionally, the resulting class should also implement the CacheManagerI interface, which is a Wattyo interface to enable the CacheManager functionality.
This interface defines three different methods:
Map<String, String> register()
It should return a Map with the <objectType, cachePartion> pairs that the CacheBuilder manages. Usually it will be only one key-value pair.
For instance:
- <‘Product_Version__c’, ‘CatalogCache’>
- <‘ObjectTree__mdt’, ‘Core’>
- <‘ruleTree’, ‘RulesCache’>
Note that the key doesn’t have to be a SObject, it’s whatever cache record class you want to manage.
String getDefaultPartition(String objectType)
It’s not used by the CacheManager right now. You will usually use it to not hardcore the Partition Name for this CacheBuilder outside the CacheBuilder implementation.
String getPartitionForObject(String objectType)
It’s not used by the CacheManager right now, but it’s there to force to have this utility method for future needs.
Clear Cache
The clearCache
methods can be called to clear the cache in different ways.
The clearCache(List
It’s important to note, that Wattyo uses the CacheBuilder
interface to manage the cache access from different functionalities. This means that the Clear Cache methods must know which CacheBuilder
implementation was used to add the specified record into the cache. This is way the register method from the CacheManagerI is required.
CacheManager.clearCache(List recordIds)
Method:
void clearCache(List<Id> recordIds)
Params:
- recordsIds: List of record IDs to be removed from the Cache.
Description:
It identifies the SObjectType for the Id and get the corresponding CacheBuilder implementation and clear the records from it.
CacheManager.clearCache(String cacheObjectType) — Currently deactivated
Method:
void clearCache(String cacheObjectType)
Params:
- cacheObjectType: Cache Object Type name to clear from the cache.
Description:
It identifies the associated cache builder and removes ALL the cached values for this type.
CacheManager.clearCache(String cacheObjectType, List cacheKeys) — Currently deactivated
Method:
void CacheManager.clearCache(String cacheObjectType, List<String> cacheKeys)
Params:
- cacheObjectType: Cache Object Type name to clear from the cache..
- recordsIds: List of record IDs to be removed from the Cache.
Description:
It gets the CacheBuilder associated to the cacheObjectType and removes all the cacheKeys from it.
Reset Cache Manager
The CacheManager has its own cache to keep the different pairs of Object-CacheBuilder and Object-Partition that were loaded through the register() methods from the CacheBuilderI implementation.
This way, the CacheManager doesn’t have to find and process all the CacheBuilder implementation each time that is used.
The internal cache will be cleared once the expiration date is reached or when you call the CacheManager.reset() method.