Azure API management removing multiple cache values

Viewed 683

I'm working with Azure's API management (APIM) caching and I got stuck on removing cache values. The default caching with cache-store and cache-lookup worked fine but we had to allow changing the name field through our API which led to the cached response returning an incorrect value.

So, for example, the end user fetches data on GET /person, updates the name on POST /person/1 from Jeff to Dave but when it fetches the persons again, Dave is still called Jeff.

As far as I understood, there was no way to remove the cache value this way, only to specify the duration, so the policy had to be rewritten with the cache-lookup-value and cache-store-value statements which allow cache-remove-value to be used. This way, I defined that the cache values consist of the subscription key + jwt subject + endpoint + query parameters, which, in the end, looks like this: SUBSCRITION_KEY/JWT_SUBJECT/person?Name=Jeff

This way, I had control over the cache key and the caching basically worked the same as with cache-store and cache-lookup

Now, the part I'm stuck with - the user can do multiple different queries on /person, for example:

 GET /person  
 GET /person?Name=Jeff  
 GET /person?MinAge=30  
 GET /person?MaxAge=60&City=NY

but if the user does a POST /person (changes any of the data), I want to reset all the cache values for the /person endpoint, but only for that user.

The problem is, when the user does a POST /person, I'm not sure what keys do I have to reset, so I also store the visited GET /person endpoints in a SUBSCRITION_KEY/JWT_SUBJECT cache key and serialize a < string, datetime > dictionary, where the string is the endpoint + query string and the datetime is the time when the endpoint was visited.

If I could deserialize the dictionary somehow and go through the values, I could remove all of them but not sure if it's even possible

<outbound>
    <cache-lookup-value key="@("SUBSCRITION_KEY/JWT_SUBJECT" variable-name="cachevalue" />
    <base />
     <choose>
        <when condition="@(context.Variables.ContainsKey("cachevalue") && !string.isNullOrWhiteSpace(context.Variables["cachevalue"]))">
        // Deserialize the context.Variables["cachevalue"] to Dictionart<string, DateTime>
        // Do a cache-remove-value for every item
        </when>
    </choose>
</outbound>

or maybe use a wildcard character

<outbound>
    <base />
    <cache-remove-value key="SUBSCRITION_KEY/JWT_SUBJECT/person*" />
</outbound>

Anyone got any idea how I can remove all those cache keys?

0 Answers
Related