Can you limit Response Cache Policy to be used only when specific requirements are met?

Hello community,

I am struggling to find explanation if it is possible to limit response caching policy to a specific request path value and specific property values within request payload.

To be more precise, I would like the policy to work only in case where path of the request matches "

/customers/password-reminder" and if that matching is hit, I want it to create cache based on two properties withing request payload which is json.

"value" within first KeyFragment I added blindly just to describe what I'm looking for (I am not sure if it is right syntax or if something like that is achievable at all).

Do you think I'm on the right path to make this work?

Example:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ResponseCache async="false" continueOnError="false" enabled="true" name="Response-Cache-Discovery-v1">
<DisplayName>Response Cache Discovery v1</DisplayName>
<FaultRules/>
<Properties/>
<CacheKey>
<Prefix/>
<KeyFragment ref="request.path" value="/customers/password-reminder"/>
<KeyFragment ref="request.formparam.username"/>
<KeyFragment ref="request.formparam.market"/>
</CacheKey>
<Scope>Exclusive</Scope>
<ExpirySettings>
<ExpiryDate/>
<TimeOfDay/>
<TimeoutInSec ref="">300</TimeoutInSec>
</ExpirySettings>
<SkipCacheLookup/>
<SkipCachePopulation>response.header.Cache-Control~~"(?i)no-cache"</SkipCachePopulation>
</ResponseCache>


Regards,
Matija

Solved Solved
0 8 247
2 ACCEPTED SOLUTIONS

Not applicable

Yes, you can put the condition for the response cache both in request and response, so that it will cache for the particular path and same will be used when the path in request matches after cached.

View solution in original post

Regarding the second question, where you want to cache based on request content.
It looks like you have a json request body.

You can use an extract variables policy to extract your two example variables

https://docs.apigee.com/api-platform/reference/policies/extract-variables-policy#jsonpayloadelement-...

For example

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="evCacheKeys">
    <DisplayName>evCacheKeys</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="market">
            <JSONPath>$.market</JSONPath>
        </Variable>
        <Variable name="username">
            <JSONPath>$.username</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">request</Source>
    <VariablePrefix>keyfrag</VariablePrefix>
</ExtractVariables>

Should store your variables in keyfrag.market, keyfrag.username

Then use these variables in your key fragments

View solution in original post

8 REPLIES 8

Not applicable

Yes, you can put the condition for the response cache both in request and response, so that it will cache for the particular path and same will be used when the path in request matches after cached.

Maybe I was unclear.
I know I can cache like that.
The core of my question is can you limit that cache to only work when specific endpoint (path) is hit?
Something like from my question:

<KeyFragment ref="request.path" value="/customers/password-reminder"/>

Instead of using key fragment, you can put the response cache policy with condition request.path = "/customers"

Great! This did the job. Thank you!

The second part of my problem was making cache unique based on values located within Request Contents.
Content looks like this:

{"market":"CH","preferred_language":"de-ch","username":"b2c.1617120321910t5lGQ@exampl3.com"}

I tried with:

<KeyFragment ref="request.formparam.username"/>
<KeyFragment ref="request.formparam.market"/>


But it doesn't work that way..

Anyone found a solution?

Ok... That was easy.
I managed to do it with

<KeyFragment ref="request.content"/>


However this make cache unique based on whole content.
Is it possible to narrow it down for just few json properties?

Regarding the second question, where you want to cache based on request content.
It looks like you have a json request body.

You can use an extract variables policy to extract your two example variables

https://docs.apigee.com/api-platform/reference/policies/extract-variables-policy#jsonpayloadelement-...

For example

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="evCacheKeys">
    <DisplayName>evCacheKeys</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="market">
            <JSONPath>$.market</JSONPath>
        </Variable>
        <Variable name="username">
            <JSONPath>$.username</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">request</Source>
    <VariablePrefix>keyfrag</VariablePrefix>
</ExtractVariables>

Should store your variables in keyfrag.market, keyfrag.username

Then use these variables in your key fragments

Great! Thank you!