Salesforce Developer Platform Cache Using


Cache.OrgPartition orgPart = Cache.Org.getPartition('local.CurrencyCache');
String cachedRate = (String)orgPart.get('DollarToEuroRate');
// Check the cache value that the get() call returned.
if (cachedRate != null) {
    // Display this exchange rate   
} else {
    // We have a cache miss, so fetch the value from the source.
    // Call an API to get the exchange rate.

Cache.Org.put('local.CurrencyCache.DollarToEuroRate', '0.91');
Cache.Org.put('DollarToEuroRate', '0.91');

With session cache, you can manage cache values in Apex and read cached values 
with a Visualforce global variable.

When using Apex, managing the session cache is similar to the way you manage the 
org cache, except the class names are different. Use the Cache.Session and 
Cache.SessionPartition classes to access values stored in the session cache. 
To manage values in any partition, use the methods in the Cache.Session class. 
If you’re managing cache values in only one partition, use the 
Cache.SessionPartition methods instead. The Cache.SessionPartition methods are 
easier to use than the Cache.Session methods because you specify the namespace 
and partition prefix only once when you create the partition object.

// Get partition
Cache.SessionPartition sessionPart = Cache.Session.getPartition('local.CurrencyCache');

// Add cache value to the partition
sessionPart.put('FavoriteCurrency', 'JPY');

// Retrieve cache value from the partition
String cachedRate = (String)sessionPart.get('FavoriteCurrency');

Session cache doesn’t support Anonymous Apex blocks. For example, if you execute 
the previous snippet of session cache methods in the Developer Console, you’ll 
get an error. However, you can execute org cache methods through Anonymous Apex.

// Get partition
Cache.OrgPartition orgPart = Cache.Org.getPartition('local.CurrencyCache');

// Add cache value to the partition. Usually, the value is obtained from a 
// callout, but hardcoding it in this example for simplicity.
orgPart.put('DollarToEuroRate', '0.91');

// Retrieve cache value from the partition
String cachedRate = (String)orgPart.get('DollarToEuroRate');

If we are managing cache values in just one partition, use the 
Cache.OrgPartition methods. The Cache.OrgPartition methods are easier to use 
than the Cache.Org methods because you specify the namespace and partition 
prefix only once when you create the partition object.

Access cached values stored in the session cache from a Visualforce page by 
using the $Cache.Session global variable. By using this global variable, you can 
read cached values that were stored with Apex directly from your Visualforce 

When using the $Cache.Session global variable, fully qualify the key name with 
the namespace and partition name. This example is an output text component that 
retrieves a cached value from the namespace ExPro, partition CurrencyCache, and 
key FavoriteCurrencyRate.


The Visualforce global variable is available only for session cache and not for 
org cache.

Unlike with Apex methods, you can’t omit the namespace.partition prefix to 
reference the default partition in the org. If a namespace isn’t defined for the 
org, use local to refer to the namespace of the current org where the code is 

<apex:outputText value="{!$Cache.Session.local.MyPartition.Key}"/>

If the cached value is a data structure that has properties or methods, like an 
Apex List or a custom class, access those properties in the $Cache.Session 
expression using dot notation. For example, this markup invokes the List.size() 
Apex method if the value of numbersList is declared as a List.

<apex:outputText value="{!$Cache.Session.local.MyPartition.numbersList.size}"/>

<apex:outputText value="{!$Cache.Session.local.MyPartition.myData.value}"/>

By using platform cache, ISV apps run faster and have better performance. If 
you’re an ISV developer, you can guarantee cache space for your apps by 
purchasing cache space for your own namespace. That way, when your app is 
installed in a subscriber org, the space for your app’s cache is not affected by 
the usage of the cache in the subscriber’s org. Only Apex code running from your 
app’s namespace can access and use your namespace’s cache. No other code in the 
subscriber org can use this cache. You can test your app against your 
namespace’s cache and be assured that the cache allocation is going to be 
protected in every subscriber org.

Cache partitions are distributed to subscribers as part of the app’s package. 
Add one or more cache partitions for your namespace to your package as 
components in the same way as you add other components. Cache partitions aren’t 
automatically added as dependent components.

1. The partitions you add to the package must be nondefault partitions.

2. When a subscriber installs your package, the cache partitions get installed 
   in their org enabling your app to access the installed cache partitions for 
   your namespace.

3. Subscribers must have an Enterprise Edition or Unlimited Edition org, or must 
   have purchased Platform Cache.

The subscriber’s total cache capacity of 30 MB is divided into three partitions 
consisting of combinations of session and org cache. The packaged cache capacity 
originates from the installed package in the subscriber’s org and contains two 
partitions. The packaged cache consists of 20 MB of cache that the ISV 
purchased. Only Apex code from the package can access the packaged cache and 
code in the subscriber’s org can’t use this cache.

Purchase your namespace cache from the Channel Order App. You can purchase cache 
in 10-MB blocks. To determine how much cache space your apps need, test your 
apps with your cache partitions. Use trial cache to increase the capacity of 
cache in your org. As we mentioned in the first unit, certain editions of 
subscriber orgs get cache allocation by default. For your Developer Edition org 
in which you develop apps, you can request 10 MB of trial cache. You can request 
to increase the amount of trial cache given to your org by contacting 
Salesforce. By experimenting with different cache sizes, you’ll have a better 
idea of how much cache to purchase for your own namespace.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License