Cache- SkipCachePopulation - Unable to validate condition

Hi,

I am trying to cache the response only if one the query param is equal to a value and rest of them not to populate. I have configured my proxy as below.

   <CacheKey>
        <Prefix/>
        <KeyFragment ref="request.uri" type="string"/>
    </CacheKey>
    <CacheResource><<ResourceName>></CacheResource>
    <SkipCacheLookup>name12 != "SeniorManagers"</SkipCacheLookup>
    <SkipCachePopulation>name12 != "SeniorManagers"</SkipCachePopulation>
    <Scope>Exclusive</Scope>

URI: /ldbpmcache?OrganizationName=xyz⩔gUnitName=qwe&PositionName=XXX

I am extracting the query param PositionName and assigning to name12 and evaluating as condition.

When i execute the proxy i could still see the response being cached. Let me know if i am missing something.

Thanks,

Latheef D

0 5 405
5 REPLIES 5

Yes, you are missing something, though I don't know exactly what.

You said

I am trying to cache the response only if one the query param is equal to a value

ok, good. The logic in the Skip elements seems to be the right idea. I don't know why you chose to assign the value of the queryparam to "name12". You can just reference the queryparam directly like request.queryparam.PositionName .

ok, now beyond that, you need to ATTACH the policy. You didn't specify how you are attaching the policy so it's possible you've got it wrong. The ResponseCache policy should be attached exactly twice: once in the request flow (preferably early in the request flow) and once in the response flow, AFTER the response has been assigned.

That's it.

I have a loopback proxy bundle to illustrate. Find it attached below. This proxy doesn't connect to anything, but it dynamically assigns a payload that looks like this:

{
    "status" : "ok",
    "key"    : "/cache-skip-logic/c?action=cache",
    "now"    : "20181205 00:06:24.299"
}

The key is the request.uri. The "now" is a formatted timestamp string.

Inserting the ResponseCache policy once in the request flow, and once in the response flow, after that payload has been assigned, allows me to store and receive cached values. I have included these elements:

  <SkipCacheLookup>request.queryparam.action != "cache"</SkipCacheLookup>
  <SkipCachePopulation>request.queryparam.action != "cache"</SkipCachePopulation>

In other words, the cache is active if and only if there is a query param named "action" and its value is "cache".

And this proxy behaves just as I would expect, consistent with the documentation:

action queryparam cache status return cached data? populate cache?
not present warm no no
not present cold no no
="cache" warm yes no
="cache" cold no yes
!="cache" warm no no
!="cache" cold no no

You MAY be running into an already-populated cache. This is a common pitfall when people are testing out the Apigee Edge cache. They fail to set a timeout in the ResponseCache policy, and as a result, the cache never expires.

  <ExpirySettings>
    <TimeoutInSec ref="flow.variable.here">20</TimeoutInSec>
  </ExpirySettings>

To avoid that, include the expiry, and during testing, make the expiry relatively quick, like 10 or 20 seconds.

<ResponseCache name="Cache-1">
  <CacheResource>cache1</CacheResource>
  <CacheKey>
    <Prefix>value</Prefix>
    <KeyFragment ref='request.uri' />
  </CacheKey>
  <Scope>Exclusive</Scope>
  <ExpirySettings>
    <TimeoutInSec ref="flow.variable.here">20</TimeoutInSec>
  </ExpirySettings>
  <SkipCacheLookup>request.queryparam.action != "cache"</SkipCacheLookup>
  <SkipCachePopulation>request.queryparam.action != "cache"</SkipCachePopulation>
</ResponseCache>

If the cache is already populated though, you will need to modify your policy to use a different cachekey, probably adding a unique prefix, so that you avoid the never-expiring cache entry. The prefix can be any fixed value.

Please find the working proxy attached. apiproxy-cache-skip-logic.zip

Thanks @Dino-at-Google for the explanation.

I have tried to use the queryparam variable it was not successful . So i tried to use extract variable and use it , still no luck. Sequence of policy is Extract Variable, Assign Message , Cache (request and response).

The time interval i configured is very short and also i cleared the cache manually to rule out the possibility of already populated-cache.

So i tried to use the proxy attached , it threw below error on 4.17.05.

Error uploading proxy: Error occurred while validation of bean AV-Now.xml. Reason: - Schema validation failed. Cause : unexpected element (uri:"", local:"Template"). Expected elements are <{}Ref>,<{}Value>,<{}Name>. Line number : 8. Column number : 15. File name : AV-Now.xml..<br>

I tried to do it on cloud and build and deployment was successful . I tried to test it and could see the condition is not being evaluated.

7779-cache.jpg

Please let me know if there is anything different.

The Template is a new element. It won't work on 4.17.05.

But it doesn't matter. It is not central to the behavior of the sample proxy I showed you. You can replace it with a JS policy. The whole point is just to set a variable that can be checked into cache, and recognized as fresh or older. I encourage you to do this replacement yourself.

You said you also tried it in SaaS. I don't know what you mean by "I could see the condition is not being evaluated."

I'll try to help if you can elaborate.

As attached in the screenshot in earlier post, even when action queryparam is not set to canche , the cache is being populated. Meaning condition is not being evaluated.

I don't see a screenshot. I understand what you're saying but it doesn't make any sense to me. you'll need to provide some additional tracing or something to help me diagnose, if you want more assistance.

Some simple suggestions:

  1. Be careful of uppercase/lowercase issues, maybe a misnamed variable.
  2. try to explain to another developer in your shop, sometimes another pair of eyes will easily spot a simple mistake