I have a platform cache partition that is packaged as part of a managed package. Once it is installed in a subscribers org it defaults to zero capacity.
Using getPartition()
and OrgCacheException
I can detect if it is present or not - as per Clean way for code to work with or without Platform Cache?.
private static Cache.OrgPartition FooOrgPartition {
get {
Cache.OrgPartition orgPart = null;
string cachePartitionName = 'PackageNamespace.FooPartitionName';
try {
orgPart = Cache.Org.getPartition(cachePartitionName);
} catch (cache.Org.OrgCacheException ex) {
System.debug(LoggingLevel.Error, 'FooOrgPartition - failed to getPartition with name ' + cachePartitionName);
}
return orgPart;
}
}
However, once I have the partition how can I tell if it has any capacity at all?
Looking at the Partition methods the obvious candidate appears to be getCapacity()
.
Returns the percentage of cache used of the total capacity for this partition.
However, that method comes with a catch. From Platform Cache Best Practices:
Minimize Expensive Operations
* UseCache.Org.getKeys()
andCache.Org.getCapacity()
sparingly. Both methods are expensive, because they traverse all partition-related information looking for or making calculations for a given partition.
I don't want to waste time checking or attempting to populate a cache partition with no capacity at all. Nor do I want to slow down the actual usage of the cache when it is needed by calling getCapacity()
. The whole point of the cache was to speed that scenario up.
Am I missing an inexpensive way to detect cache partitions with no capacity?
Some additional thoughts:
- It doesn't appear to be possible to delete the packaged cache partition from the subscribers org. As a packaged component there is no indication that it can be deleted from the subscribers org without uninstalling the package.
- I could use a custom hierarchy setting or feature parameter to toggle the functionality, but I'd prefer to directly detect the cache status based on its actual configuration.
getCapacity
or a cache miss? The answer to this question would dictate your approach. A partition without capacity makes no sense..getCapacity
is negligible. However, if the cache is larger and is under heavy use then the cost becomes significant, hence the reference to not calling it in the linked best practices. The whole point of using the cache is to improve performance when it is needed. I just have no safe mechanism to detect if it should be used or not.