Platform Cache can increase performance,replace Custom settings
and Custom Meta data Types by storing data temporarily without
any need of extra storage,and eliminates the need to use web callouts.
Note : session cache doesn't support synchronous Apex.
For example, you can't use future methods or batch Apex with
session cache.
Platform cache to improve the API callouts.
ex : access token for external service
Cache.OrgPartition orgPart = Cache.Org.getPartition('local.SomePartitionName');
orgPart.put('IntegrationAccessToken','123w12e12wzws1jnds3rbhh3');
orgPart.put('IntegrationTokenExpiry','10/03/2018');
system.debug((string)orgPart.get('IntegrationAccessToken'));
1.Org cache
Org cache can store Org-wide data such that anyone in the org can access
it.Org cache is accessible across sessions,requests, and org users and profiles.
For Org cache,data can live upto 48 hours in the cache.
By default, the time-to-live (TTL) value for org cache is 24 hours.
global values for all users
exists for upto 48 hours
2.Session Cache
specific to user sessions
Expires in 8 hours or if session ends
enterprise - 10MB (Mega bytes)
unlimited and performance - 30 MB (Mega bytes)
All others -0MB (mega bytes)
Maximum partition size - 5 MB (mega bytes)
Use Case :
============
1. Want to get top 10 by annual Revenue
2. Want to get number of VIP Accounts in each region (7 regions)
ex :
List<Account> accounts = [select Name,Region__c,BillingAddress,Description,
Industry,Status__c,Type,Opportunity_count__c,AnnualRevenue
from Account order by AnnualRevenue DESC NULLS LAST LIMIT 10];
// Add to Cache
Cache.Org.put('local.AcctPartition.TopAccounts',accounts);
// Get the cache Partition
cache.OrgPartition orgPart = cache.Org.getpartition('local.AcctPartition);
// Get the data and cast it to the right datatype
List<Account> accuntsCache = (List<Account>) orgPart.get('TopAccounts);
if(accuntsCache !=null){
return accountsCache;
}
return accountscache;
ex :
Map<String,Integer> accountsByRegion = new Map<String,Integer>();
List<Schema.PicklistEntry> regions = Account.Region__c.getDescribe().getPicklistValues();
for(Schema.PicklistEntry pe : regions){
accountsByRegion.put(pe.value,0);
}
for(String regionName : accountsByRegion.keyset()){
Integer count =[ select count() from account where Region__c =: regionName and Type ='VIP'];
accountsByRegion.put(regionName,count);
}
// put any data structure into the cache
Cache.Org.put('local.AcctPartition.VIPAccounts',accountsByRegion);
Map<String,Integer> accountsByRegion = (Map<String,Integer>)cache.Org.getPartition('local.AcctPartition').get('VIPAccounts');
if(accountsByRegion !=null){
return convertMapToDetail(accountsByRegion);
}
return getAccountsByRegion();
Note :
The Cache Diagnostics user permission allows you to see detailed
information about the platform Cache feature.
Session cache :
================
Session cache stores data that are tied to a user's session
such that other users in the Org cannot access this data.
The maximum life of a session cache is 8 hours.
use the Cache.Session and cache.SessionPartition classes
to access values stored in the session cache.
Cache.Session.put(Key,value);
CacheBuilder Interface :
========================
public class Accountcache implements Cache.CacheBuilder{
Public Object doLoad(String topTen){
List<Account> accounts = (List<Accounts>)[select Id,AnnualRevenue,Name,Region__c
FROM Account ORDER BY AnnualRevenue DESC NULLS LAST LIMIT 10];
return accounts;
}
}
CacheBuilder Interface has method with 1 parameter.
CacheBuilder use the class and key to request.
//populate the cache
List<Account> myAccounts = (List<Account>) Cache.Org.get(AccountCache.class,'TopTen');
// Retrieve from cache
List<Account> myAccounts2 = (List<Account>) Cache.Org.get(AccountCache.class,'TopTen');
Interface checks if value is cached.
If cached return value else calculate,cache and return.
Note :
Instead of storing and retrieving cache, it is better to provide
loading strategy toPlatform cache, so upon cache miss,Salesforce
automatically calls the class to load the cache for that key.
This reduces the code and handles cache miss much more gracefully.