put Cache in NodeJS

Hi,

As we are attempting to use the apigee Cahce get and put in Node JS, we found the documentation a bit confusing

https://docs.apigee.com/api-services/content/access-cache-nodejs

put

cache.put('<em>key</em>',<em>data</em>,<em>ttl</em>,function(<em>error</em>));

Put data into a cache.

Parameters:

  • key: (Required) A string that uniquely identifies the item in the cache. Cache keys are limited to a size of 2 KB.
  • data: (Required) A string, Buffer, or object that represents the data to cache. Any other data type will result in an error. For convenience, objects will be converted into a string using "JSON.stringify".
  • ttl: (Optional) The maximum time to persist the data in the cache, in seconds. If not specified then a default TTL will be used.
  • callback: (Optional) If specified, a function that will be called once the data is safely in the cache. It will be called with an Error object as the first parameter if there is an insertion error, and otherwise it will be called with no parameters.

Example:

var apigee =require('apigee-access');
var cache = apigee.getCache();
// Insert a string with a timeout of 120 seconds
cache.put('key2','Hello, World!',120);
// Insert a string and get notified when insert is complete
cache.put('key4','Hello, World!',function(err){
  // "err" will be undefined unless there was an error on insert
});

1) timeout vs ttl

The third parameter in the description it is listed as ttl, but in the example it is listed as "timeout".

From our trials it looks like it is cache ttl "persist the data".

2) when the parameter in not present the description says "a default TTL will be used"

We assumed that the default will be the Cache Resource default, but from our trials it looks like a different value (our Cache Resource has ttl 300 seconds, but trails were showing something around 45 seconds of ttl if the option is not present).

This is on a private cloud installation version 4.17.9

Solved Solved
0 4 313
1 ACCEPTED SOLUTION

the third param timeout of the function (as in the example of the function) or ttl of the cache being "put" (as in the description)?

The third param is TTL for the cache element. You said "as in the example of the function" - maybe you are being confused by the preceding comment which is "Insert a string with a timeout of 120 seconds". I re-read this, and understood the timeout to apply to the cache element. But I will change the documentation to be more explicit.

when the param is not present, what is the expected behaviour?.

The expected behavior is for the actual TTL of the element to be the configured TTL for the named cache, if you use a named cache.

In the code you showed, you did not access a named cache. Instead you initialized the cache with no name,

var cache = apigee.getCache();

This means your nodejs code will be putting and getting to the unnamed cache. There is no way to set the default TTL for the unnamed cache. You said "our Cache Resource has ttl 300 seconds", but it does not appear that your nodejs code is using that cache resource.

I believe the TTL for the default "un-named" cache resource is undocumented. I looked here, and saw nothing relevant. 45 seconds seems reasonable.

So you have these ways to get a well-known TTL when calling cache.put():

  1. Specify the TTL when you initialize the cache object, eg.,

    var cache = apigee.getCache('MyCustomCache', {defaultTtl: 120});
  2. specify the TTL in the cache.put() call
  3. use a specific, environment-scoped cache, for which you can set the TTL in the Admin UI. For that, use this javascript:
    var cache = apigee.getCache(undefined, {scope: 'global', 
                                            resource: 'cacheResource'});
    	

In my opinion, the documentation is not very clear on this.

View solution in original post

4 REPLIES 4

What is your question?

1) It the third param timeout of the function (as in the example of the function) or ttl of the cache being "put" (as in the description)?

2) when the param is not present, what is the expected behaviour?. Doc says it will use the default TTL of the Cache Resource, but we are getting a completly different value (somewhere around 45 seconds).

the third param timeout of the function (as in the example of the function) or ttl of the cache being "put" (as in the description)?

The third param is TTL for the cache element. You said "as in the example of the function" - maybe you are being confused by the preceding comment which is "Insert a string with a timeout of 120 seconds". I re-read this, and understood the timeout to apply to the cache element. But I will change the documentation to be more explicit.

when the param is not present, what is the expected behaviour?.

The expected behavior is for the actual TTL of the element to be the configured TTL for the named cache, if you use a named cache.

In the code you showed, you did not access a named cache. Instead you initialized the cache with no name,

var cache = apigee.getCache();

This means your nodejs code will be putting and getting to the unnamed cache. There is no way to set the default TTL for the unnamed cache. You said "our Cache Resource has ttl 300 seconds", but it does not appear that your nodejs code is using that cache resource.

I believe the TTL for the default "un-named" cache resource is undocumented. I looked here, and saw nothing relevant. 45 seconds seems reasonable.

So you have these ways to get a well-known TTL when calling cache.put():

  1. Specify the TTL when you initialize the cache object, eg.,

    var cache = apigee.getCache('MyCustomCache', {defaultTtl: 120});
  2. specify the TTL in the cache.put() call
  3. use a specific, environment-scoped cache, for which you can set the TTL in the Admin UI. For that, use this javascript:
    var cache = apigee.getCache(undefined, {scope: 'global', 
                                            resource: 'cacheResource'});
    	

In my opinion, the documentation is not very clear on this.

aha!

------------------------------------

So you have these ways to get a well-known TTL when calling cache.put():

  1. Specify the TTL when you initialize the cache object, eg.,
    var cache = apigee.getCache('MyCustomCache',{defaultTtl:120});

-------------------------

I had assumed that not explicitly specifying the {defaultTtl:x} value will result in the default "MyCustomCahe" TTL will be used.

We got it working by specifying the TTL in the "put", which made more sense to us because different puts can result in different TTL values.

var customCache = apigee.getCache('MyCustomCache', {
resource: 'cacheResourceName',
scope: 'global',
prefix: 'prefixValue',
timeout: 3
});
.........
customCache.put(cacheEntryKey //Not including prefix, 
                cacheEntryValue, 
                180, //180 sec or 3 minutes timeout for the cache entry
function(err) {...} );

Thanks for your help clarifying this.