Azure Custom Cache Policy not working

Oleg Semyonov 21 Reputation points
2021-05-13T14:38:24.887+00:00

Hi,

I am trying to implement custom caching in Azure operator policy.
I followed https://learn.microsoft.com/en-us/azure/api-management/api-management-sample-cache-by-key

For test I did simple solution and I expected to get 'world clock' from cache in 20 sec

Here is inbound section for test:

<inbound>  
        <set-variable name="propertyid" value="@{  
            string propertyid = "4qkk";  
            return propertyid;    
        }" />  
        <cache-lookup-value key="@((string)context.Variables["propertyid"])" variable-name="itemNumberJson" caching-type="internal" />  
        <!-- If API Management doesn’t find it in the cache, make a request for it and store it -->  
        <choose>  
            <when condition="@(!context.Variables.ContainsKey("itemNumberJson"))">  
                <send-request mode="new" response-variable-name="worldclock" timeout="10" ignore-error="true">  
                    <set-url>http://worldclockapi.com/api/json/est/now</set-url>  
                    <set-method>GET</set-method>  
                </send-request>  
                <!-- Store response body in context variable -->  
                <set-variable name="worldclock_value" value="@(((IResponse)context.Variables["worldclock"]).Body.As<string>())" />  
                <cache-store-value caching-type="internal" key="@((string)context.Variables["propertyid"])" value="@((string)context.Variables["worldclock_value"])" duration="20" />  
            </when>  
        </choose>          
        <base />  
    </inbound>  

It never returns me world clock result from cache
If I use azure cache by query, headers, developers it works, but not for custom implementation.

What's wrong in this simple sample and maybe some additional permissions/settings not enabled ?

Thanks,
Oleg

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,371 questions
{count} votes

Accepted answer
  1. ChaitanyaNaykodi-MSFT 27,366 Reputation points Microsoft Employee
    2021-05-17T22:21:03.207+00:00

    Hello @Oleg Semyonov , I think running a trace in this example can help in exactly pin-pointing the issue.
    You can run a trace for your API using the following steps.

    1. Go to your API in APIM on Azure portal
    2. Click on test

    97239-image.png
    3. Click "Send" to test the API. and then click on Trace
    97220-image.png

    In the "Inbound" section of the trace, you can see if the "cache-lookup-value" has resulted into a hit or a miss. If the "cache-lookup-value" has resulted in a miss try running it again if the previous run was more than 20 secs ago. If it still results into a cache miss, then check the "cache-store-value" in the inbound section of the trace. According to the issue mentioned here see if there is any mismatch in the "key" value set.
    Please let me know if there are any concerns. Thank you!

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Faraz Ahmed SH Shaikh 1 Reputation point
    2021-06-02T11:27:31.997+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.