I am attempting to store values in Cache via an Apex script. I use the Cache.Session.put() method and if I subsequently call .contains() on the next line of the code then the Key shows properly as having been stored. However, the next time I call that same script the Key no longer exists in the Cache and .contains() return false.

A few facts:

  • I have a Platform Cache called TestPlatCache in the local namespace with Allocation Session (Trial, 5), Org (Trial, 5), Total (Trial, 10)
  • The Apex script is being called from Lightning Component JavaScript
  • I have verified that my Session ID is the same between both calls of the script (shown in Log file below)

Log Files:

Run 1 - 1:26:52 PM

13:26:52:025 USER_DEBUG [36]|DEBUG|Session Id Substring: H1_sNrc2bkzbS6AO8GRdqmhCniu6NjLS4TmML7EwD98P57SyUq9NXP.Wp1dTspsJkqSW.oG45D1b

13:26:52:031 USER_DEBUG [44]|DEBUG|Contains 1 - False

13:26:52:032 USER_DEBUG [49]|DEBUG|Contains 2 - True

Run 2- 1:26:59 PM

13:26:59:020 USER_DEBUG [36]|DEBUG|Session Id Substring: H1_sNrc2bkzbS6AO8GRdqmhCniu6NjLS4TmML7EwD98P57SyUq9NXP.Wp1dTspsJkqSW.oG45D1b

13:26:59:023 USER_DEBUG [44]|DEBUG|Contains 1 - False

13:26:59:024 USER_DEBUG [49]|DEBUG|Contains 2 - True

public static void TestSessionCache()
    String nsPartKey = 'local.TestPlatCache.someKey';
    String sessionID = UserInfo.getSessionId().substring(36);
    system.debug('Session Id Substring: ' + sessionID);

    if (Cache.Session.contains(nsPartKey))
        system.debug('Contains 1 - True');
        system.debug('Contains 1 - False');            
        Cache.Session.put(nsPartKey, 'some value');     

        if (Cache.Session.contains(nsPartKey))
            system.debug('Contains 2 - True');
            system.debug('Contains 2 - False');

Can you please help me determine why my Cached Keys are not getting stored between separate calls of the same Apex script? I am expecting the log message "Contains 1 - True" but I'm not getting it. How can I retrieve a Cached Key from a previous run of the script?

Screenshot of Platform Cache

Platform Cache

  • 1
    If you call the static method from execute anonymous in the dev console twice, does it produce the expected result? (eliminating the lightning component from the problem scope) – Mark Pond Jul 3 '18 at 21:33
  • "The Apex script is being called from Lightning Component JavaScript". So your TestSessionCache() method has the @AuraEnabled annotation? What if you call it via two anonymous apex code blocks. The lightning domain sessions can be a special case. – Daniel Ballinger Jul 4 '18 at 2:43
  • Try adding a debug for Cache.Session.getCapacity(). It returns a percentage of cache capacity that has been used. isAvailable() is another good one to check. – Daniel Ballinger Jul 4 '18 at 2:53
  • @MarkPond, I went to test the code with Execute Anonymous today and it did indeed return the expected result. As Daniel asked, the AuraEnabled is not on the TestSessionCache() method, but is on another method that calls TestSessionCache(). This other method with AuraEnabled was returning an Exception which I knew about but was ignoring for the sake of just trying to isolate the test with Cache.Session. Turns out, ignoring the Exception was a bad idea because that's what was causing the Key to get removed from Cache. Once I updated code to remove the Exception, the Key gets stored correctly. – Tony Rush Jul 4 '18 at 16:25

As written, your code works just fine and produces the debug output you expect.

Is your partition marked as the default partition? Does the behavior change if you do so?

First Execution

First Execution

Second Execution

Second Execution

Cache Configuration

enter image description here

  • It was not Default Partition originally, so I set it as such but that does not fix the issue. It's very interesting that your Cache works with the same code. I'm not sure what's different. I'm executing from Lightning Component but I wouldn't think that matters. I just got my Platform Cache trial activated today. This is a Developer Salesforce system. Thanks for the research and reply, but please let me know if you have any other suggestions. I will add a screenshot of my Platform Cache to the OP. – Tony Rush Jul 3 '18 at 20:50

I was able to resolve this issue by ensuring that my Apex code does not throw any Exceptions. It appears that Salesforce does not properly store a Cache Key if an Exception occurs later in the thread.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.