9

I know there are limitations to platform cache though, within the limitation(capacity) is it allowed to store any type of data (Collections, Integer, DateTime, sObject etc...) in the cache?

What if we store a collection : map of String to list of object? eg;

Map<String,List<String>> ObjDevNameToFields;
...
Cache.Org.put('local.LabelStorage.labels', ObjDevNameToFields);
...

will there be a data loss?

Thanks

1 Answer 1

7

Looking at the documentation you can put an object (Map> is an object) and hence we can use complex types as well .

You can definitely put map> into the Org Cache

Apex code

public class OrgCacheController {

// Constructor of the controller for the Visualforce page. 
public OrgCacheController() {  
}

// Adds various values to the cache.
// This method is called when the Visualforce page loads.
public void init() {  
    Map <Id,List<Contact>> mapAccIdByCntlst = new Map <Id,List<Contact>> ();
    for (Contact c: [Select Id, name, AccountId from Contact limit 1000]) {
    // AccountId mapping with Contact List

    if (mapAccIdByCntlst.containsKey(c.AccountId)) {
            mapAccIdByCntlst.get(c.AccountId).add(c); //tricky part .Here map.get(key) is returning list and we are adding contacts to the list
    } else {
            List < Contact > lstcnts = new List < Contact > (); //Initialize list as no key is found before and first time we get key 
            lstcnts.add(c);
            mapAccIdByCntlst.put(c.AccountId, lstcnts);
    }

  }
    if (!Cache.Org.contains('maplist')) {
        Cache.Org.put('maplist', mapAccIdByCntlst );
    } 
}


// Return output from the cache.
 public Map<Id,List<Contact>> getOutput() {
    return (Map<Id,List<Contact>>)Cache.Org.get('maplist');
 }

}

VF class

<apex:page controller="OrgCacheController" action="{!init}">

<apex:form >
    <br/>Partition display
    <apex:repeat value="{!Output}" var="fieldKey">
            <apex:outputText value="{!fieldKey}"/>:
           {!Output[fieldKey]}<br/>
        </apex:repeat>

</apex:form>

2
  • 4
    You can't put any type you want. Normal rules still apply. For example, you can't serialize SavePoint objects, so they cannot be put into the cache. All normal data types, like maps, can be put into the cache.
    – sfdcfox
    Commented Nov 26, 2015 at 7:05
  • @sfdcfox Thanks for the correction ! Updated my answer Commented Nov 26, 2015 at 20:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .