How to use platform cache - with example

Platform cache allows you to cache data in Salesforce server. Accessing platform cache is faster than getting data by querying from objects or custom settings.

Use cases

  • Storing API session token till it expires
  • Caching frequently called API response data
  • Caching results from time consuming operations like complex queries and calculations

Types of platform cache

  1. Session Cache

    Stores data in association with user’s session. Maximum life is 8 hours. Only the user who put data to this cache can retrieve it back.

  2. Org Cache

    Data is stored at the org level. Any user can access data in cache.

How to setup

  • Go to Setup -> Platform Cache
  • Click on “New Platform Cache Partition” button
  • Enter necessary details and click “Save”. You can select “Default” checkbox to make the partition your default partition to avoid specifying partition name which caching data



It is recommended to create a separate class to manage all operations with cache. You can see an example of using a service class to manage org cache below. CacheService class is used to just manage cached data and ExternalSystemAPI class leverages that class to cache access token.


2 comments: