Lookup Cache hit is always false when using node.js code to populate the cache.

I am using node.js code to populate the cache in a predefined cache resource. but while lookup the cache hit always returns a false. The requirement was to populate multiple values in the cache by generating different cache Key and assigning the respective values in those cache Keys. Below is the node code:

var svr = http.createServer(function (req, resp) {
      var apigeeEnv = apigee.getVariable(req, "environment.name");
      var apigeeOrg = apigee.getVariable(req, "environment.orgname");
      var newCache = apigee.getCache(apigeeOrg + "__" + apigeeEnv + "__My-cache", { resource: 'resource-cache', scope: 'global' });


      newCache.put(apigeeOrg + "__" + apigeeEnv + "____a3c__abc", "xyz");
      
      // Getting data from cache
      newCache.get(apigeeOrg + "__" + apigeeEnv + "____a3c__abc", function(error,data){
        console.log("data ==>" + data);
      });
      resp.end();
    });
      

I am able to get the details of the cache from the cache Key using the "newCache.get" call but I think some how the data in the cache key is not getting populated in that particular cache resource.

Solved Solved
1 3 382
1 ACCEPTED SOLUTION

There is some confusion around the use of the apigee.getCache() method. The docs are pretty clear, I think, but the parameter names seem to be misleading. The bottom line is, your nodejs code needs to be like this:

var newCache = apigee.getCache('doesNotMatter', 
         { resource: 'MyCacheName', scope: 'global' });
....
newCache.put("myCacheKey", "xyz");

In order for a LookupCache policy like this, to work:

<LookupCache name="Lookup-Cache-1">
  <CacheKey>
    <KeyFragment>myCacheKey</KeyFragment>
  </CacheKey>
  <CacheResource>MyCacheName</CacheResource>
  <Scope>Global</Scope>
  <AssignTo>contextVariable</AssignTo>
</LookupCache>

For more on this, check the doc on

I think it would be helpful to include this example in the docs for apigee-access. I'll look into that.

View solution in original post

3 REPLIES 3

Hi. I'm not clear on the exact problem. you wrote:

while lookup the cache hit always returns a false

And you also wrote:

I am able to get the details of the cache from the cache Key using the "newCache.get"

These two statements seem to contradict each other. Can you explain further? Are you using the LookupCache policy? If so, can you provide the configuration for that policy?

Hi Dino,

In the lookup policy I always get cachehit as false. But in the node.js code itself when I use the cache.get function it returns me the data present in that cacheKey. The Lookup cache policy configured is as below:

<?xml version="1.0"?> <LookupCache continueOnError="false" async="false" name="LookupCache.Data" enabled="true"> <CacheResource>resource-cache</CacheResource> <AssignTo>cachedResponse</AssignTo> <Scope>Global</Scope> <CacheKey> <Prefix/> <KeyFragment>a3c</KeyFragment><KeyFragment>abc</KeyFragment> </CacheKey> </LookupCache>

Also to mention that I am creating the cacheKey dynamically in the node.js code.

There is some confusion around the use of the apigee.getCache() method. The docs are pretty clear, I think, but the parameter names seem to be misleading. The bottom line is, your nodejs code needs to be like this:

var newCache = apigee.getCache('doesNotMatter', 
         { resource: 'MyCacheName', scope: 'global' });
....
newCache.put("myCacheKey", "xyz");

In order for a LookupCache policy like this, to work:

<LookupCache name="Lookup-Cache-1">
  <CacheKey>
    <KeyFragment>myCacheKey</KeyFragment>
  </CacheKey>
  <CacheResource>MyCacheName</CacheResource>
  <Scope>Global</Scope>
  <AssignTo>contextVariable</AssignTo>
</LookupCache>

For more on this, check the doc on

I think it would be helpful to include this example in the docs for apigee-access. I'll look into that.