Session Cache Bug When in @future Context

We built a little framework around managing Session Cache. It wasn’t until today we referenced some of the caching methods from a @future method. I understand that in this case it might be better to use Platform Cache instead, or don’t use cache at all, but that’s irrelevant here. What’s interesting is that there is isAvailable() method which based on the documentation should return false in this scenario:

isAvailable() – Returns true if the session cache is available for use.
The session cache isn’t available when an active session isn’t
present, such as in asynchronous Apex or code called by asynchronous
Apex. For example, if batch Apex causes an Apex trigger to execute,
the session cache isn’t available in the trigger because the trigger
runs in asynchronous context.

Instead, the code breaks and a system error is thrown:

|FATAL_ERROR|cache.Org.OrgCacheException: Failed Cache.Session.getDefault():Session not found - Session Cache not supported

Class.cache.PlatformCacheDelegateWrapper.getSessionDefault: line 16, column 1
Class.cache.Session.getDefault: line 99, column 1
Class.cache.Session.isAvailable: line 91, column 1
Class.CacheManager: line 14, column 1

Line 14 in CacheManager is:

if (Cache.Session.isAvailable())

Anyone else experienced this? Should I file a bug with SFDC or is there something else that I’m missing here?


Yes, you’ll want to file a bug; this is something happening within the system code (and it shouldn’t be). For now, do not attempt to call the cache methods without first checking for a session:

if(UserInfo.getSessionId() != null) {
    // It should be safe to use Cache.Session now.

Source : Link , Question Author : Boris Bachovski , Answer Author : sfdcfox

Leave a Comment