function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rogerio Carvalho 9Rogerio Carvalho 9 

Cache Session does NOT retrieve data stored in cache intermittently

We have identified an issue where Cache.Session get() method does NOT return data stored in cache intermittently. We have put together a very tiny sample VisualForce page to demonstrate the problem. This sample page demonstrates that Cache.Session.getPartition(partitionName).get(KEY) returns NULL even if the data exists in cache for the given KEY. The page performs 100 Visualforce.remoting.Manager.invokeAction calls to a controller remote action. If the Apex code is not able to retrieve the data from cache it returns "Not Found" otherwise it returns "Found". Case # created with Salesforce.com 14311948.

See code and output sample below:

VisualForce Page:
<apex:page controller="CacheTestController" showChat="false" showHeader="false">

<div style="margin: 10px; font-size: 14px">
    <pre id="put">In progress ...</pre>
    <pre id="get"></pre>
</div>
 
<script>
    Visualforce.remoting.Manager.invokeAction(
        'CacheTestController.putCache', function(result, event) {
            document.getElementById('put').innerHTML = result;
        }, 
        { escape: false }
    );
 
    var tentative = 1;
 
    var timer = setInterval(function () {
 
        if (tentative == 100) clearInterval(timer);
 
        Visualforce.remoting.Manager.invokeAction(
            'CacheTestController.getCache', tentative, function (result, event) {
                document.getElementById('get').innerHTML += result + '<br/>';
            }, 
            { escape: false }
        );
 
        tentative++;
 
    }, 1000);
</script>
</apex:page>



Controller:
public class CacheTestController {
 
    private static final String PARTITION_NAME = 'FinancialServices';
    private static final String KEY = 'STAGE';
    private static final String VALUE = 'Any value';
      
    @RemoteAction
    public static String putCache() {
 
        Cache.Partition partition = Cache.Session.getPartition(PARTITION_NAME);
 
        partition.put(KEY, VALUE);
 
        return 'ok';
    }
 
    @RemoteAction
    public static String getCache(String tentative) {
 
        String found = 'Found: ' + tentative;
 
        Cache.Partition partition = Cache.Session.getPartition(PARTITION_NAME);
       
        String cacheValue = (String) partition.get(KEY);
       
        if (cacheValue == null) {
            found = 'Not Found: ' + tentative;
        }
       
        return found;
    }
}

Output:
Found: 1
Found: 2
Found: 3
Found: 4
Found: 5
Found: 6
Found: 7
Found: 8
Found: 9
Found: 10
Found: 11
Found: 12
Found: 13
Found: 14
Found: 15
Not Found: 16
Found: 17
Found: 18
Found: 19
Found: 20
Found: 21
Found: 22
Not Found: 23
Found: 24
Found: 25
Found: 26
Found: 27
Found: 28
Not Found: 29
Not Found: 30
Found: 31
Found: 32
Found: 33
Found: 34
Found: 35
Found: 36
Found: 37
Found: 38
Found: 39
Found: 40
Found: 41
Found: 42
Found: 43
Not Found: 44
Found: 45
Not Found: 46
Found: 47
Found: 48
Found: 49
Found: 50
Found: 51
Found: 52
Found: 53
Found: 54
Not Found: 55
Found: 56
Found: 57
Found: 58
Found: 59
Found: 60
Found: 61
Found: 62
Found: 63
Not Found: 64
Found: 65
Found: 66
Found: 67
Found: 68
Found: 69
Found: 70
Found: 71
Found: 72
Not Found: 73
Found: 74
Found: 75
Found: 76
Found: 77
Not Found: 78
Found: 79
Found: 80
Found: 81
Found: 82
Found: 83
Not Found: 84
Found: 85
Found: 86
Found: 87
Found: 88
Found: 89
Found: 90
Found: 91
Found: 92
Found: 93
Found: 94
Found: 95
Not Found: 96
Found: 97
Found: 98
Found: 99
Found: 100

 
Kayla Borg 14Kayla Borg 14
Did you reach a resolution with the case you submitted?
Henrique Oliveira 9Henrique Oliveira 9
A resolution, yes. An anwser, no. After 4 - 5 days of case opened, the issue was gone. Then salesforce support started saying they could not reproduce the issue and the case got closed. So far, we could no longer reproduce the error and everything is working fine. But we never received any information about what happened or whether something was fixed or not.
Thomas GagnéThomas Gagné

Here's an even simpler example--a unit test. It works in all our environments except our staging org.

static testMethod void cacheTest() {
  	Cache.Session.put('key', 'value');
  	system.assertEquals('value', Cache.Session.get('key'));
}
Why on earth would that fail?
Matt Brown 71Matt Brown 71
I could imagine that would fail if there was no default partition defined in your Platform Cache setup.  Or another reason could be that if you have Session cache capacity set to 0, nothing would throw an exception - but your cache wouldn't be written.