Are cache entries made with apigee-access visible to cache policies?

Not applicable

Hello,

In the apigee-access docs Accessing the cache in Node.js there are the following statements:

"Inside Apigee Edge, the cache is distributed among all nodes where your Node.js application executes."

and, when discussing cache resource scope,

global: All cache entries may be seen by all Node.js applications in the same Apigee "environment."

Does this mean that entries made to a cache resource using apigee-access from nodejs are not visible to the Edge policy LookupCache?

Is there a separation between entries made by nodejs and cache entries made by the populate cache policy?

I would like to have a proxy that uses nodejs to populate a cache resource and then have the data from the cache resource read by another non-nodejs proxy using the Lookup Cache policy.

Is that possible?

Solved Solved
1 7 732
2 ACCEPTED SOLUTIONS

Hi @neil.munro

Yes thats possible. In the node app, you can use the apigee-access Cache object and use the put method to set a key and an object which stores it into the Cache globally. More info here. You can get the Cache object by passing the Cache name that is configured. Please use global scope, something like

var cache = apigee.getCache('CacheName', 
	{
	  resource: 'CacheName', 
	  scope:'global'
	});

cache.put("cacheKey", sresponseBody, 3600, 
	function(err) { 
		console.log("Cache updated") 
});

On the other non-node app, you can fetch using the lookup cache policy

<LookupCache async="false" continueOnError="false" enabled="true" name="Lookup-Cache-1">
    <DisplayName>Lookup-Cache-1</DisplayName>
    <Properties/>
    <CacheKey>
        <KeyFragment>cacheKey</KeyFragment>
    </CacheKey>
    <CacheResource>CacheName</CacheResource>
    <Scope>Global</Scope>
    <AssignTo>flowVar</AssignTo>
</LookupCache>

View solution in original post

Not applicable

Hi @Sai Saran Vaidyanathan,

Thanks again for your response. Having the confirmation helped me find my mistake.

The problem appears to have been with the configuration of the lookup policy where we had a <Prefix> element and a <Scope> element.

From the docs (I highlighted in bold the key statement)

Working with cache keys

<Scope> or<Prefix>Use the <Scope> or <Prefix> elements to further namespace cache keys. <Scope> enumerates a list of predefined values. The <Prefix> element overrides <Scope> with a value of your own choosing.

So, where I had

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LookupCache async="false" continueOnError="false" enabled="true" name="LookupCache.CustomerDashboard.Report">
    <DisplayName>LookupCache.CustomerDashboard.Report</DisplayName>
    <Properties/>
    <CacheKey>
        <Prefix>CustomerDashboard</Prefix>
        <KeyFragment ref="kao.resource"/>
        <KeyFragment ref="kao.param.sales_group"/>
        <KeyFragment ref="kao.param.sales_office"/>
        <KeyFragment ref="kao.param.page"/>
    </CacheKey>
    <CacheResource>SFA2_CustDash</CacheResource>
    <Scope>Global</Scope>
    <AssignTo>kao.cache.report</AssignTo>
</LookupCache>

It needed to be:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LookupCache async="false" continueOnError="false" enabled="true" name="LookupCache.CustomerDashboard.Report">
    <DisplayName>LookupCache.CustomerDashboard.Report</DisplayName>
    <Properties/>
    <CacheKey>
        <Prefix/>
        <KeyFragment>CustomerDashboard</KeyFragment>
        <KeyFragment ref="kao.resource"/>
        <KeyFragment ref="kao.param.sales_group"/>
        <KeyFragment ref="kao.param.sales_office"/>
        <KeyFragment ref="kao.param.page"/>
    </CacheKey>
    <CacheResource>SFA2_CustDash</CacheResource>
    <Scope>Global</Scope>
    <AssignTo>kao.cache.report</AssignTo>
</LookupCache>

View solution in original post

7 REPLIES 7

Hi @neil.munro

Yes thats possible. In the node app, you can use the apigee-access Cache object and use the put method to set a key and an object which stores it into the Cache globally. More info here. You can get the Cache object by passing the Cache name that is configured. Please use global scope, something like

var cache = apigee.getCache('CacheName', 
	{
	  resource: 'CacheName', 
	  scope:'global'
	});

cache.put("cacheKey", sresponseBody, 3600, 
	function(err) { 
		console.log("Cache updated") 
});

On the other non-node app, you can fetch using the lookup cache policy

<LookupCache async="false" continueOnError="false" enabled="true" name="Lookup-Cache-1">
    <DisplayName>Lookup-Cache-1</DisplayName>
    <Properties/>
    <CacheKey>
        <KeyFragment>cacheKey</KeyFragment>
    </CacheKey>
    <CacheResource>CacheName</CacheResource>
    <Scope>Global</Scope>
    <AssignTo>flowVar</AssignTo>
</LookupCache>

Thanks for the response.

I have been trying to do this - my code looks identical to yours aside from the names used but the policy is never successful.

The value in the cache is successfully retrieved using nodejs so I know that the cache is populated.

I must have a typo or a cache key mismatch somewhere.

Would it be possible to share the code ? Are you passing the timeout arg while using the put ? Can you make sure the value of the timeout and the cache configured are the same (or lesser) ?

Not applicable

Hi @Sai Saran Vaidyanathan,

Thanks again for your response. Having the confirmation helped me find my mistake.

The problem appears to have been with the configuration of the lookup policy where we had a <Prefix> element and a <Scope> element.

From the docs (I highlighted in bold the key statement)

Working with cache keys

<Scope> or<Prefix>Use the <Scope> or <Prefix> elements to further namespace cache keys. <Scope> enumerates a list of predefined values. The <Prefix> element overrides <Scope> with a value of your own choosing.

So, where I had

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LookupCache async="false" continueOnError="false" enabled="true" name="LookupCache.CustomerDashboard.Report">
    <DisplayName>LookupCache.CustomerDashboard.Report</DisplayName>
    <Properties/>
    <CacheKey>
        <Prefix>CustomerDashboard</Prefix>
        <KeyFragment ref="kao.resource"/>
        <KeyFragment ref="kao.param.sales_group"/>
        <KeyFragment ref="kao.param.sales_office"/>
        <KeyFragment ref="kao.param.page"/>
    </CacheKey>
    <CacheResource>SFA2_CustDash</CacheResource>
    <Scope>Global</Scope>
    <AssignTo>kao.cache.report</AssignTo>
</LookupCache>

It needed to be:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LookupCache async="false" continueOnError="false" enabled="true" name="LookupCache.CustomerDashboard.Report">
    <DisplayName>LookupCache.CustomerDashboard.Report</DisplayName>
    <Properties/>
    <CacheKey>
        <Prefix/>
        <KeyFragment>CustomerDashboard</KeyFragment>
        <KeyFragment ref="kao.resource"/>
        <KeyFragment ref="kao.param.sales_group"/>
        <KeyFragment ref="kao.param.sales_office"/>
        <KeyFragment ref="kao.param.page"/>
    </CacheKey>
    <CacheResource>SFA2_CustDash</CacheResource>
    <Scope>Global</Scope>
    <AssignTo>kao.cache.report</AssignTo>
</LookupCache>

Awesome ! Thanks for sharing

and @neil.munro , in nodejs code, how do you compute the cache key?

Is it a simple string concatenation of all of those fragments?

Isn't this inconsistent? I can use a prefix and scope on Edge policy but not on node js since they are mutually exclusive. What is the solution if I want to use both?

Update: It is a bit ambigous. The document 'working with cache keys' just specifies how keys are created using the 'scope' and 'prefix' parameters. In the absence of prefix, the keys will have org and env prefixed to the key (seperated by '__'). If prefix is specified, then the key is prefixed with this value. It doesn't state anywhere that these parameters are mutually exclusive -- so apologies, this was just my (incorrect) interpretation of what the document was saying.

Having said this, I do not believe the prefix parameter is treated correctly in the node backend. My understanding of this is if I specify a prefix value on edge and use this value in node backend (appended with '__'), then the behaviour should be predictable. I.e I should be able to populate the cache in node backend and use the Edge policies in API Proxy to retrieve the value from the cache. This however is not the case.