Layer7 API Management

The definitive guide to the Store and Lookup Cache assertions 

Dec 07, 2016 01:54 PM

Hello API Community!

 

Every time I have to do something using either the Store to Cache or Look Up in the Cache, I make mistakes and forget how to store or lookup things. Today I decided to write something to help me, and those other forgetful folks, to master those two assertions  You will find the exported policy at the end of this document. You can download it and deploy in your own gateway.

 

The Store to Cache:

 

Some folks will get confused about where to set what's going to be stored. That's done by right clicking on the Store to Cache assertion and selecting the "Target Message". Even though it's saying "message" you can store a string variable without any problem. At least it's been working for me  Let's take a look into a policy snippet to clarify it furthe

 

Notice that the assertion number 14 is storing the string that has been passed as a HTTP parameter (line #10). Encoding is not mandatory. I was just doing it because of the spaces. Let's have a look into the Storage properties:

 

As you can see the "Cache ID" isn't really the ID. It's more of a label to tag the real cache ID, that is the "Cache entry key".

 

The Look Up in Cache:

 

To retrieve something back from the cache, all you have to know is to use the same label (aka "Cache ID") and entry key. For the sake of this small sample, I'm hard coding the "Cache ID" as definitiveCache (policy line #12 and #22). And where the retrieved value will be saved? In the target message again. In the previous policy snippet (line 23) you can notice that I'm retrieving the value and storing it into the ${where.to.save.the.cached.value} variable. Then I'm decoding it (again, not mandatory if you're not encoding when storing it), using the same label and key and printing it inside the return like this:

 

What has been stored: ${where.to.save.the.cached.value}

 

One pro of encode/decode is that you don't need to print the variable.mainpart. If you're not encoding, then you most likely have to use the "mainpart" when returning the value somewhere.

 

You can give it a try by hitting my test environment here:

 

To store something: https://gw01.apimca.com:8443/v1/definitiveCache?store=<what is going to be stored>&id=<cache key>.

To retrieve something back: https://gw01.apimca.com:8443/v1/definitiveCache?lookup=<cache key used to store>.

 

Example:

 

Storing: https://gw01.apimca.com:8443/v1/definitiveCache?store=alan has gorgeous blue eyes&id=alanInfo

Looking up: https://gw01.apimca.com:8443/v1/definitiveCache?lookup=alanInfo

 

I hope this can help you as much as it will help me every time I have to use it and cannot remember how to use it

Statistics
0 Favorited
12 Views
1 Files
0 Shares
1 Downloads
Attachment(s)
zip file
Definitive Cache - V1.xml.zip   2 KB   1 version
Uploaded - May 29, 2019

Related Entries and Links

No Related Resource entered.