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?

Default managed package allocation

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
* Use Cache.Org.getKeys() and Cache.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.
  • 1
    What's more expensive - getCapacity or a cache miss? The answer to this question would dictate your approach. A partition without capacity makes no sense.. – identigral Mar 24 at 20:51
  • @identigral I'm not sure it is that simple as that. If the cache is empty or has no capacity then the cost to call 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. – Daniel Ballinger Mar 24 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.