[SalesForce] Cache.Session return null value

I have create small function to store and retrieve the value inside static using session cache. but i am unable to get the data which i have stored in session cache. I am not getting any error on retrieve or inserting but Always return null value. Any one can help me.

Here is My platform Cache setting

Image1

Image2

Apex Controller :

public class SessionCacheController {
    public  static String name {get;set;}
    public  static String check {get;set;}
    public  static String dummy {get;set;}
    private static final String KEY = 'Key';
    private static final String VALUE = 'Any value';

    //adding key,value pair to cache 
    public static void  setData()
    {      
        dummy='setData';
        Cache.Session.put('MyPackagePrefix.PlatformCache.myName', 'Nithesh K');             

        Cache.Partition partition = Cache.Session.getPartition('PlatformCache');
        partition.put(KEY, VALUE);                     
    }

    //retrieve value by key 
    public static void getData()
    {
        dummy='getData';
        Name=(String)Cache.Session.get('MyPackagePrefix.PlatformCache.myName');   
        system.debug('myName *** ' + Name); // return null

        Cache.Partition partition = Cache.Session.getPartition('PlatformCache');
        check = (String) partition.get(KEY);
        system.debug('check *** ' +  check); // return null
    }
}

Visual force Page

 <apex:page controller="SessionCacheController">
    <apex:form >
    <apex:pageBlock id="pageSection">
        <apex:outputText >  My name {! Name  } </apex:outputText>   <br/>   
         <apex:outputText > Check Key {! check  } </apex:outputText>   <br/> 
         <apex:outputText > dummy {! dummy  } </apex:outputText>   <br/> 
    </apex:pageBlock>

    <apex:commandButton value="setData" action="{!setData }" reRender="pageSection"/>
    <apex:commandButton value="getData" action="{!getData }" reRender="pageSection"/>
    </apex:form>

</apex:page>

When i click setData() command-button, Value is added cache.session and getData() value is retrieve from cache.session and render in visual force page .

Best Answer

I have found answer for my question by Allocating space to Session Cache and Org Cache Allocation . I have forgot to change the value of Session Cache Allocation and Org Cache Allocation from 0 to some other value. without that it is not working..

Above session cache code only works if your created new Platform Cache. If you don't know how create a Platform Cache, Follow below link one by one

First Follow this link to activate Request Trial Capacity

Second Follow this link to create new platform cache

If your finished with above two step then click on edit on platform cache which your newly created, Change trail value 0 to 5 in Session Cache Allocation and Org Cache Allocation and save it.

enter image description here

How to use session cache

Related Topic