Unable to get variable from Lookup cache from two proxies

Not applicable

Hello There,

I'm facing an issue where I'm unable to get a cached value using lookup cache policy as explained below:

I have two proxies (proxy1 and proxy2) that are deployed under same API product and each proxy hit a different target endpointpoint:

Proxy1 : calls authenticate API.

Proxy2 : calls accounts API

In proxy1, once I get the response back, I'm caching the token using the below polices in sequence to the response flow:

ExtractVariables:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract_IAM_Token">
    <DisplayName>Extract IAM Token From IAM Response</DisplayName>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="AccessToken">
            <JSONPath>$.access_token</JSONPath>
        </Variable>
        <Variable name="AccessTokenType">
            <JSONPath>$.token_type</JSONPath>
        </Variable>
        <Variable name="AccessTokenExpiry">
            <JSONPath>$.expires_in</JSONPath>
        </Variable>
        <Variable name="AccessTokenScope">
            <JSONPath>$.scope</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">response</Source>
    <VariablePrefix>varIAMToken</VariablePrefix>
</ExtractVariables>

PopulateCache:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PopulateCache async="false" continueOnError="false" enabled="true" name="IAM_MG_Token">
    <DisplayName>Populate IAM Token From Cache</DisplayName>
    <Properties/>
    <CacheKey>
        <KeyFragment>mgAccessToken</KeyFragment>
        <KeyFragment ref="apigee.access_token"/>
    </CacheKey>
    <Scope>Exclusive</Scope>
    <ExpirySettings>
        <TimeoutInSec>3600</TimeoutInSec>
    </ExpirySettings>
    <Source>varIAMToken.AccessToken</Source>
</PopulateCache>

LookupCache (used to confirm that I can get the token):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LookupCache async="false" continueOnError="false" enabled="true" name="Get_IAM_Token">
    <DisplayName>Lookup IAM Token From Cache</DisplayName>
    <Properties/>
    <CacheKey>
        <KeyFragment>mgAccessToken</KeyFragment>
        <KeyFragment ref="apigee.access_token"/>
    </CacheKey>
    <Scope>Exclusive</Scope>
    <AssignTo>varIAMToken.AccessToken</AssignTo>
</LookupCache>

AssignMessage:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Return_MG_Token">
    <DisplayName>Remove IAM Token From the Response Body</DisplayName>
    <Set>
        <Payload contentType="application/json">
            {
                "AccessToken":"{varIAMToken.AccessToken}"
            }
        </Payload>
        <StatusCode>200</StatusCode>
        <ReasonPhrase>OK</ReasonPhrase>
    </Set>
    <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
</AssignMessage>

Which gives me the token. However, for proxy2, I need to lookup the cache so I can make the call to my accounts API with that token. I'm adding the below policy to the request flow :

LookupCache:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LookupCache async="false" continueOnError="false" enabled="true" name="Get_IAM_Token">
    <DisplayName>Lookup IAM Token From Cache</DisplayName>
    <Properties/>
    <CacheKey>
        <KeyFragment>mgAccessToken</KeyFragment>
        <KeyFragment ref="apigee.access_token"/>
    </CacheKey>
    <Scope>Global</Scope>
    <AssignTo>varIAMToken.AccessToken</AssignTo>
</LookupCache>

However, I'm not getting the token back and the cache comes empty.

6720-screen-shot-2018-04-06-at-33617-pm.png

I'm not sure why am i missing in my polices or if i'm doing something incorrect. I really appreciate your help.

Thank you

1 2 720
2 REPLIES 2

THANKS for the clear, detailed question! With all that detail, it makes it really easy for us to understand what's going on.

Answer: check the Scope.

In one of your LookupCache policies, you have

<Scope>Global</Scope>

In the other LookupCache and in the PopulateCache, you have

<Scope>Exclusive</Scope>

I think you want Global scope, everywhere.

The scope must match, across Populate and Lookup. Internally, the PopulateCache or LookupCache will use a different key if you specify Scope= Exclusive vs Global. Another way to say this: If you use PopulateCache with Exclusive scope, a lookup with Global scope will not retrieve the value you just populated.

Also, according to the documentation for the LookupCache policy, a value populated with Exclusive scope would be available only to a LookupCache policy operating in the same proxyEndpoint of the same revision of the same proxy bundle! Therefore if you use PopulateCache in proxy1 with Scope=Exclusive, you cannot retrieve from proxy2 with LookupCache, regardless of what you use for Scope.

I think you want to use Global Scope, in every policy. With that, values populated in proxy1 would be available for lookup from proxy1 or proxy2.

Thank you so much for your response. I update the Scope and faced the same issue. However, The thing I didn't mention in my issue is that I was using Flow Callout policy to do shared-double-proxy between my Apigee Edge and MicroGatway. This policy (Internally) was doing "add cache" between my "apigee.access_token" and my micorgateway token causing that I lost "varIAMToken.AccessToken" from the cache since it is overriding it as I'm using the same key.

To solve the issue I had to first lookup my cache (using Lookup cache policy) in my account proxy before doing the double-shared-porxy. Then after the flow callout policy, I'm re-populating the cache with "apigee.access_token"/"varIAMToken.AccessToken" which solved my issue.