Be the first to like this
For Web Applications, caching is a common strategy to improve performance and reliability. Yet, it’s a lesser known feature that there is “Platform Cache” as a built-in mechanism for Salesforce developers to add to their app design. Leveraging the Platform Cache can reduce your SOQL queries for certain types of data and can cut the retrieval time for data to just a few Milliseconds. 10 MB of Platform Cache are free for all orgs starting from Enterprise Edition, so you can start right now in your sandbox without the need for extra licenses.
Use cases for the Platform cache is everything that's either expensive to retrieve from the database or expensive to hold in the memory. This could be all kind of data that isn't changing too often, but crucial for Apex code (and especially code run from the frontend or in Triggers, while users are waiting and seeing a spinner): Schedules, mapping tables, taxonomies, pricing logic - at this point, you will have read at least one keyword for some Apex heavy lifting that is implemented in your org as well.
The starting point are just a few lines of Apex: The Cache.CacheBuilder interface is easy to implement and wraps just a SOQL query to execute to handle cache misses. So within minutes, your implementation can be up and running and handle one of the biggest risks for caches, which is a ""miss"" - data, that should be there but is not. Platform Cache has a strict policy for Time to Live (TTL) and eviction strategies if you run out of memory (LRU), so it's very likely that one particular value is just not there when you need it. Another threat is stale data in your cache, and some basic invalidation is also already implemented by the platform.
In the github repo for the session (github.com/dstdia/PlatformCache), you can find a sample implementation for Platform Cache, including an example for the CacheBuilder interface, an example for a custom Cache Manager class, and some benchmarking scripts, as well as the presentation and the essential reads for this topic. If you want to connect with Daniel, reach out via Twitter (@stangomat), LinkedIn (https://www.linkedin.com/in/danielstange/) or Email (firstname.lastname@example.org)