Platform Cache In Salesforce

03:29 0 Comments A+ a-

Platform Cache In Salesforce

Hello All,

Thanks to all of you to give a great response to my last blog Release Notes Spring 18.
Today we are going to discuss Platform Cache in salesforce.So let’s start.

Why use cache:

To store data statically, that is being used frequently in our application.

Do not need to query data again and again. Or do not need to make API call, If we fetch data from a Third party API.

What is Platform Cache: Platform cache is a layer in which you store data to be accessed later in our application. Use of platform cache, make our application much faster. Before using platform cache, we need to create a partition and allocate space to it.

Type Of Platform Cache: There is two type of cache in salesforce.
1. Org Cache: It stores data org-wide. Anyone can access data in Org. Following classes are used to retrieve and put data if we use Org Cache.

2. Session Cache: Session cache stores data for an individual user and is tied to that user’s session. The maximum life of a session is 8 hours. Following classes are used to retrieve and put data, if we use Session Cache.

Where We can use Platform Cache: Platform Cache can be used everywhere in your code, where you use same data over and over. In the following case, Platform cache can be useful.

  • Want to hold temporary data, do not want to save it in the database but want to use on various pages.
  • Making expensive calculation (same calculation on same data) on every page.
  • User’s Shopping cart in the shopping site.

Cache Allocation By Editions:

Developer Edition (0 MB by Default), But can get 10 MB as a trial.

Enterprise Edition (10 MB by default)

Unlimited Edition (30 MB by default)

Performance Edition (30 MB by default)

Limitation of Platform Cache Salesforce :

  • Data is not persisted, So There is No guarantee of data loss.
  • Data is not encrypted in the cache.
  • Org Cache support concurrent read and write. So data can be overwritten. For example if a user put data in Org cache with key name and value is ‘Arpit’ and start using in application. But during this time period another user come and put value ‘Navin’ for key name. Then this value will be changed for both user.
  • Session cache can store value upto 8 Hours (or until session is lost)
  • Org cache can store value upto 48 Hours.
  • Maximum size of a single cached item (for put() methods) 100 KB

Create Partition :

Before starting to work on platform cache, we need to make a Partition of Platform Cache. Partitions allow us to distribute cache space, so we can better use it for our application. Caching data in specific partition ensures that data is not overwritten by another application.

Each partition has one session cache and one org cache segment and you can allocate separate capacity to each segment. Session cache can be used to store data for individual user sessions, and org cache is for data that any users in an org can access.

To create partitions Search Platform Cache in quick find box.

In developer edition, We have 0MB space. But it can be increased by using Request Trial Capacity button.

Once you get a confirmation mail. You are ready to create Partition. Click on New Platform Cache Partition.

Click on save to save the partitions.Values on Session Cache allocation and Org Cache allocation can be changed later also.

Now Let’s Put and retrieve data using platform cache.

To call Partition in apex code we use namespace.partitionName . ‘local’ as namespace can be used in both scenario if you have namespace enabled or not enable in your org.

In my case I have iarpit as namespace , In my code i am using local.ibirds and iarpit.ibirds both. (Here ibirds is name of partition we created)

I am doing a very simple example (Just like a Wizard page)  just to implement Platform cache.

I created 2 Visualforce page , and 2 different controller for them.

On the first page we take input in wrapper list and on another page we show that data. We do not insert data in database.

On CacheExample page we fill the data in wrapper list.

On next button click, we will display this data on CacheExampleNextPage.

To clear cache click on remove button. And reload page . On remove button I used following code.
Cache.SessionPartition orgPart = Cache.Session.getPartition('local.ibirds); orgPart.remove('myList')

CacheExample class:

/* Name : CacheExample Date : 1/02/2018 Author : Description : We are doing this as an example of Cache memor */ public class CacheExample{ public List<CacheWrapper> listOfCacheWrap{get;set;} public CacheExample(){ listOfCacheWrap = new List<CacheWrapper>(); listOfCacheWrap.add(new CacheWrapper(1,'',TRUE)); } public void addRow(){ listOfCacheWrap.add(new CacheWrapper(1,'',false)); } public PageReference nextPage(){ Cache.Session.put('local.ibirds.myList', listOfCacheWrap); return Page.CacheExamplePage1; } public class CacheWrapper{ public Integer index{get;set;} public String name{get;set;} public Boolean isQualified{get;set;} public CacheWrapper(Integer index, String name, Boolean isQualified){ this.index = index; = name; this.isQualified = isQualified; } } }

CacheExample Page:

<apex:page controller="CacheExample"> <apex:form > <apex:pageBlock > <apex:pageBlockButtons > <apex:commandButton value="Next Page" action="{!nextPage}"/> </apex:pageBlockButtons> <apex:pageBlockTable value="{!listOfCacheWrap}" var="objWrap"> <apex:column headerValue="Action"> <apex:commandButton value="Add" action="{!addRow}"/> </apex:column> <apex:column headerValue="Reg No."> <apex:inputText value="{!objWrap.index}"/> </apex:column> <apex:column headerValue="Name"> <apex:inputText value="{!}"/> </apex:column> <apex:column headerValue="Qualified"> <apex:inputCheckbox value="{!objWrap.isQualified}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>

CacheExampleNext class:

/* Name : CacheExampleNext Date : 1/02/2018 Author : Arpit vijayvergiya Descriptio : In this class we will get the data from cache memory and display on another page. */ public class CacheExampleNext{ public List<CacheExample.CacheWrapper> newList{get;set;} public CacheExampleNext(){ newList = new List<CacheExample.CacheWrapper>(); // Get partition Cache.SessionPartition orgPart = Cache.Session.getPartition('iarpit.ibirds'); system.debug(orgPart); newList = (List<CacheExample.CacheWrapper>)orgPart.get('myList'); System.debug('newList '+newList); } public void remove(){ Cache.SessionPartition orgPart = Cache.Session.getPartition('local.ibirds); orgPart.remove('myList'); } }


<apex:page controller="CacheExampleNext"> <apex:form > <apex:pageBlock > <apex:pageBlockButtons > <apex:commandButton value="Remove" action="{!remove}"/> </apex:pageBlockButtons> <apex:pageBlockTable value="{!newList}" var="objWrap"> <apex:column headerValue="Reg No."> <apex:outputText value="{!objWrap.index}"/> </apex:column> <apex:column headerValue="Name"> <apex:outputText value="{!}"/> </apex:column> <apex:column headerValue="Qualified"> <apex:outputText value="{!objWrap.isQualified}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>

In above Example I used Cache Session. If you want to use Cache Org then use Cache.Org class instead of Cache.Session and Cache.OrgPartition class instead of Cache.SessionPartition.
$Cache.Session Global Variable : Any value stored in Cache Session can directly be accessed on the Visualforce page by using $Cache.Session global Variable.

Here is Snippet of code:

<apex:outputText value="{!$Cache.Session.namespace.partitionName.key}"/>

If key value is List, then we can find size also to use it further on our page. (myList is key)

<apex:outputText value="{!$Cache.Session.namespace.partitionName.myList.size}"/>

Want to delete A Partition you created:  You can see that there is no delete button or link for
partition we created.

So then How we can delete partition if we found that it has no longer use for us.
Let’s click on edit button,Uncheck default checkbox button and save.

Thanks to all.