- July 22, 2024
- iBirds Software Services
- 0
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.
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.
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; this.name = 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="{!objWrap.name}"/> </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'); } }