0

I have one method from which i am calling onother method which is having soql query. Every time method calls .Soql will get executed.to avoid any performance issue i have created session.catche code

public class HandlerClass () {
public static vod getdata(string recordId) {
boolean falg = getAccount(recordId)
}
public static boolean getAccount(string recordId) { 
List<Account> acc= [select id from account where id=:recordsids limit 1];
if(acc.size()>0)
return true
else
return false
}

}

// Get partition
 Cache.SessionPartition sessionPart = Cache.Session.getPartition('local.AccountCache');
// Add cache value to the partition
List<account> lst= new List<account>([select id from account]);
system.debug(lst);
sessionPart.put('accData', lst);
// Retrieve cache value from the partition
lst = new List<account>();
lst = (List<account>)sessionPart.get('accData');
system.debug(lst);

But i am not sure where to save this code .and how to refer in getdata method to avoid soql query everytime

1 Answer 1

1

you can use sessionPart.contains(key)

in your case

// Add cache value to the partition
if (!sessionPart.contains('accData')) {
    List<account> lst= new List<account>([select id from account]);
    system.debug(lst);
    sessionPart.put('accData', lst);
}
sessionPart.get(...)

and

public static Boolean getdata(String recordId) {
        // Get partition
        Cache.SessionPartition sessionPart = Cache.Session.getPartition('local.AccountCache');
        // Add cache value to the partition
        if (!sessionPart.contains('accData')) {
            Boolean falg = getAccount(recordId);
            sessionPart.put('accData', falg);
        }
        // Retrieve cache value from the partition
        return (Boolean)sessionPart.get('accData');
    }

You can read more about Best Practices https://developer.salesforce.com/docs/atlas.en-us.224.0.apexcode.meta/apexcode/apex_platform_cache_best_practices.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_cache_SessionPartition.htm

Thanks

0

You must log in to answer this question.

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