Can't I store an object in the Cache?

I'm using the populate cache and lookup cache policies to store some data in the cache, however this data is an object.

I am very easily able to store simple string and variables in the cache, but when I try to store an object, or even a prefix variable like prefix.data, then it stores it empty.

object:

var data = { name: 'name', value: 'value' }

Is it not possible to store non-simple variables in cache? How does response cache work, does that need to store complex responses as well?

PS: Response times are important, as well as quick availability across all MPs. So if KVM can be used and has comparable response times, then I am willing to try that option too.

0 3 1,824
3 REPLIES 3

Hi @Prashanth Subrahmanyam,

objects are serialized when stored in cache, in your case if your JSON is

var data = {name:'name',value:'value'},

so. you are constructing this object in js policy, right?

you can do context.setVariable('serializedData',JSON.stringify(data))

you could then use this variable serializedData in your populateCache and LookupCache,

After you lookup, if you want to use it in the JS policy, just do JSON.parse('lookupValue')

Thanks,

Ya, exactly, but this doesn't work. 😞 Full details below.

Extract Variables Policy : $.data -> data

data

Variables Assigned

{"firstName":"Prashanth","lastName":"Subrahmanyam"}

Try #1

JS Policy :

var data = context.getVariable('data');
context.setVariable('message', JSON.stringify(data));

Error :

Execution of JSPrepareMessage failed with error: Javascript runtime error: \"Access to Java class \"java.lang.Class\" is prohibited. (JSPrepareMessage_js#3)\"

Try #2

JS Policy :

var data = context.getVariable('data');
context.setVariable('message', data);

Error :

Execution of JSPrepareMessage failed on line 3 with error: null

Request Body:

{
    "topic": "test2",
    "data": {
        "firstName": "Prashanth",
        "lastName": "Subrahmanyam"
    }
}

'message' is a predefined variable, so that maybe the reason, its failing. i tried the following and it worked fine, try setting the variable name to different name

var data  = {name:'name', value:'value'}
context.setVariable('jsondata', JSON.stringify(data))
var retrieved = JSON.parse(context.getVariable('jsondata'))
context.setVariable('json_name',retrieved.name)