How to build a JavaScript Policy pulling data from a local data storage

anton
New Member

Hello,

I'm looking for an expert advice please.

I need to build an API call that does this:

  1. Receives a string ID as query parameter
  2. Looks up a record in the Apigee data storage identified by the given ID string. The record is super simple - only two fields - ID and Data. Data is up to 4096 bytes long.
  3. If a record found, the code calls some target endpoint passing on the found data and returns the response.
  4. If not found, it calls a different target endpoint that generates the Data, and the code creates a new record in the data storage, and then does #3.

I would greatly appreciate some simple code please as well as general advice on the best approach.

Lots of thanks in advance,

Anton.

Solved Solved
0 12 475
1 ACCEPTED SOLUTION

HI @Anton+Bar

Please find the attached zip. See if that works.

Assumptions I made to build the proxy: I am pointing it to httpbin.org/get end point. I wrote another mock service that generated a Guid when the data is not available in Cache.

Created an Env cache called "demoCache"

Hope this gives you some idea

cachingtest.zip

View solution in original post

12 REPLIES 12

HI @Anton+Bar

Please find the attached zip. See if that works.

Assumptions I made to build the proxy: I am pointing it to httpbin.org/get end point. I wrote another mock service that generated a Guid when the data is not available in Cache.

Created an Env cache called "demoCache"

Hope this gives you some idea

cachingtest.zip

Thank you Sai! Quick question - is the caching persistent or will it "forget" the entries after some time? I was researching after I posted this and bumped into KeyValueMap but couldn't make it work so far.

Yes, its time based and transient. Thats why I recommend that. KVM is not for that purpose. For more info on Caching, check this doc

@Anton+Bar - please accept this answer if it resolved your issue

I will accept when it will. Sorry, still not with you. I'm looking for persistent data storage, not something like cache.

Sure. My question to you is - you have a source of truth anyways, the callout which provides that info, then why does this need to be persisted always ? We can always go back to the backend system which I guess is the source of truth.

Yes, that's true, but to get that data string might take a relatively long time (long story as to why it is so), so the first call can take around 30 seconds. I'd like to avoid that.

Ok. You can have a longer Cache TTL in that case ? What if the values in the source change, how will that update KVM ? In other words, you will need to sync both the data sources. In stead have one as primary and the other a temp data store. Again this is my 2 cents.

Good point! However in our case the values in the source do not change, so I'd be happier with KVM. Would that work?

Yes it can. How many records do you think will be stored ? The reason I tell this is its generally recommended to store config in there. However its a persistent store.

I see, so we'll probably use Cache then.

Awesome !!!