How to cache a response I get from a Javascript policy?

benyuen-1
Participant II

Hi,

I've a javascript policy, inside the javascript policy im doing som rest callouts to other apis for fetcing some values. Now this callout takes 500ms and is somewhat static. So caching it would really fit in here to make everything super fast.

This is how my javascript callout look like:

keykey = context.getVariable("somecachedkey");
if(keykey)
{
    throw "caching is working!";   
}
var myRequest = new Request('http://someadress.com/api',"GET");
var test = httpClient.send(myRequest);
test.waitForComplete();
if (!test.isSuccess()) {
    throw 'Error contacting geocoding web service';
}
context.setVariable("somecachedkey", test.getResponse().content.asJSON.value);

It works fine.

I've read some about caching and I thought the populate cache policy was the one I was aiming for.

And I created a polulate cache policy but when trying to deploy it I get the error:

Invalid cache resource reference in Step definition <CachePolicyName>

I read that this should solve it: https://docs.apigee.com/api-platform/cache/manage-caches-environment

Which is great because I want the cached used by other api proxies. But I don't really understand how to configure everything. From checking the cache in my javascript policy, using a cache policy thats fetches a variable from the javascript step and sharing the cache between multiple proxies.

Solved Solved
0 10 1,005
1 ACCEPTED SOLUTION

You'll need to create a cache using the Admin menu, in the same environment where you've deployed your proxy and with the same name you're using in the CachePolicyName attribute

View solution in original post

10 REPLIES 10

You'll need to create a cache using the Admin menu, in the same environment where you've deployed your proxy and with the same name you're using in the CachePolicyName attribute

I've another question in <CacheResource> is it possible to have the value as a variable? I would like to reuse the caching in shared flow but for different values.

You can't use a dynamic value for CacheResource, but you can certainly dynamically assign different keys (using variables to compose part or all of the key). So, you would still be using one cache, but you could logically separate the values stored in it (if you really need to) by using different keys (eg: prefix each key with the proxy name).

See more about how to compose keys in https://docs.apigee.com/api-platform/reference/policies/populate-cache-policy#CacheKeyElement

Is there a way to fetch cache values into dynamically variables ( <AssignTo>)? I've a case where I've moved my caching to a shared flow. This caching shared flow is able to fetch different keys but I'm having problem with dynamically naming variables I'm caching them into.

It's not possible to use dynamic value for <AssignTo>, but you could leverage Shared Flow parameters.

Your shared flow could use something like this in the LookupCache policy:

<LookupCache>
....
<AssignTo>outputVar<AssignTo>
</LookupCache>

and any proxy using that shared flow could setup the actual variable name as a parameter:

<FlowCallout>
  ...
  <Parameters>
    <Parameter name="outputVar">someVariableName</Parameter>
  </Parameters>
  <SharedFlowBundle>YourSharedFlowName</SharedFlowBundle>
</FlowCallout>

See https://docs.apigee.com/api-platform/reference/policies/flow-callout-policy#samples for more details

@Ben Yuen

I have a few questions

Have you created the cache resource in the environment configuration?

Can you post your populate cache policy configuration.

The <CacheResource> tag in your populate cache configuration should have the name of the cache you created

what is the scope definition you have used for the cache configuration? If you use Global scope, then the cache will be available across api proxies in the same org.

I've another question in <CacheResource> is it possible to have the value as a variable? I would like to reuse the caching in shared flow but for different values.

ElangoD
Participant III

This error is due to

In Populate cache <CacheResource> is not match with the "Cache Created in the Environment Configuration".

Just Check "<CacheResource>"-- This will resolve the issue

I've another question in <CacheResource> is it possible to have the value as a variable? I would like to reuse the caching in shared flow but for different values.

ts not possible to use a riable in the <CacheResource>