accessing cache in nodejs with scope of Proxy

I am trying to access cache with nodejs with the scope of proxy, but it is not recognized. If you look at the nodejs cache access documentation, you are only allowed to use 3 types of scope, global, exclusive and application, but if you look at populate cache policy, it supports more scopes and even Proxy scope, why is there a disconnect?

Why is the scope options more for policies compared to nodejs codes?

0 6 190
6 REPLIES 6

I tried to write to cache in Nodejs via applicaiton scope, and try to read the same cache via policy to the same cache resource and same applicaiton scope but the policy returns no result. But node js code says successfully updated cache. It is as if the nodejs cache put only works with in the scope of the nodejs applicaiton that is hosted in apigee and it is no capable of updating the cache so that other applicaitons can read it.

Here is why there is a difference:

There are five options in the PopulateCache policy: Global, Application (=apiproxy), Proxy (=proxy endpoint), target (=target endpoint) or Exclusive.

the "apigee-access" module, that provides access to the Apigee Edge cache, runs always as a target. The target endpoint and proxy endpoint are not known, within that target. Therefore those options are not feasible.

As for how to use Application scope from nodejs (apigee-access) and the policy, show your code on both sides. (Are you using a prefix in the policy configuration? are you specifying the cache name and resource consistently? Show us.)

Have you looked at this Q&A?

EDIT : See attached for a working proxy that demonstrates how to

  • load the cache at Application scope from nodejs
  • read the same cache from nodejs
  • read or write the same cache from policy

apiproxy-cache-test.zip

HI this is my code from in node js

var http = require('http'); 
var apigee = require('apigee-access'); 
var csv    = require("csvtojson");
var cache = apigee.getCache('acclookupcachekian',
                            { resource: 'lookupcachekian',
                              scope: 'application'
                            });
console.log('node.js application starting...');
var svr = http.createServer(function(req, resp) {
    console.log('The deployment mode is ' + apigee.getMode());
    csv()
      .fromFile('./someCsvFile.csv')
      .then(function(jsonArrayObj){ //when parse finished, result will be emitted here.
         console.log('Size of record is : ' + jsonArrayObj.length);
         for(var i =0; i < jsonArrayObj.length; i++){
              cache.put(jsonArrayObj[i].some_id, JSON.stringify(jsonArrayObj[i], null, 2),
                          function(error) {
                              if(error){
                                  console.log(JSON.stringify(error));
                              }
                            }
                        );

         }
       });
       resp.end('Acc look up date loaded successfully to cache !');
});
svr.listen(9000, function() {
    console.log('Node HTTP server is listening');
});

And here is my code in the proxy.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LookupCache async="false" continueOnError="false" enabled="true" name="Lookup-Cache-1">
    <DisplayName>Lookup Cache-1</DisplayName>
    <Properties/>
    <CacheKey>
        <KeyFragment ref="request.queryparam.id"/>
    </CacheKey>
    <CacheResource>lookupcachekian</CacheResource>
    <Scope>Application</Scope>
    <AssignTo>cache.content</AssignTo>
</LookupCache>

The look up policies will get zero entries for the same some_id that has been successfully loaded by the nodejs code.

And yes I have looked at the QnA my code is much simpler in terms of cache key, but I still couldnt access the cache via policy not sure if it is there are issues with applicaiton scope.

Hi @Dino-at-Google I have confirmed that this is a bug, if I use the scope application the same code wont work but if I use the scope global for both it works. We want lower granular level , we want to use applicaiton if possible but there seems to be a bug in nodejs apigee-access cache put, when I put applicaiton scope for it the policy cant retrieve the cached data.

Actually what we are trying to do is to load about 300k records into cache, wanted to use node js to load the records from file into cache, but access is not working as expected. It halts and stops at certain records, and some cache are not loaded while some are.

ok I understand, let me look at your example.

Hi Kian

I put together an example that shows how to load the cache from nodejs, and then read it from LookupCache. See the attachment in my edited answer.

Let me know if this helps.