Populate cache and Lookup cache - cannot get data

pxzxz1
New Member

Hello apigeeks

I want to cache the api key from my 1st proxy and use it on my 2nd proxy.

1st proxy - Student access

Flow request

- extract value to get the api key, i.e. apikey_ref

Flow response

- extract value, i.e. response_data_ref

- populate cache

I am caching with response value so that it is unique to individual when the api is being called.

<CacheKey>
    <Prefix>cacheKey</Prefix>
    <KeyFragment ref="apikey_ref" />
    <KeyFragment ref="response_data_ref" />
</CacheKey>
<Scope>Global</Scope> 
<ExpirySettings> 
    <TimeoutInSeconds>300</TimeoutInSeconds>
</ExpirySettings>

2nd proxy - Student result

Flow request

- lookup cache

<CacheKey>
    <Prefix>cacheKey</Prefix>
    <KeyFragment ref="apikey_ref" />
    <KeyFragment ref="response_data_ref" />
</CacheKey>
<Scope>Global</Scope>
<AssignTo>getCachedData</AssignTo>

However I cannot get the data from populate cache.

Solved Solved
0 6 817
1 ACCEPTED SOLUTION

@dane knezic is right. In your populate cache policy you are using both the response data and the apikey are the cache key fragments, and no value is cached since you havent mentioned the source value that needs to be cached.

My second proxy request will required the response value from the first proxy as one of the payload. Then I can use it as the key, and api key as the value so that I can verify the API key.

Its not a good practice to cache apikeys, you are actually allowing any request that has the response data value to use the apikey and get access to all the APIs provisioned for that key. The right practice is - your second proxy should receive the apikey in each request and validate it from the request parameter/header.

Here's how the populate and lookup cache would like based on what you mentioned

I want to use my response value from 1st proxy as the cache key. The API key will be the value.
PopulateCache policy:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PopulateCache async="false" continueOnError="false" enabled="true" name="Populate-Cache-1">
    <DisplayName>Populate Cache-1</DisplayName>
    <Properties/>
    <CacheKey>
        <Prefix/>
        <KeyFragment ref="response_data_ref"/>
    </CacheKey>
    <Scope>Global</Scope>
    <ExpirySettings>
        <TimeoutInSec>3600</TimeoutInSec>
    </ExpirySettings>
    <Source>apikey_ref</Source>
</PopulateCache>

LookupCache policy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LookupCache async="false" continueOnError="false" enabled="true" name="Lookup-Cache-1">
    <DisplayName>Lookup Cache-1</DisplayName>
    <Properties/>
    <CacheKey>
        <Prefix/>
        <KeyFragment ref="response_data_ref"/>
    </CacheKey>
    <Scope>Global</Scope>
    <AssignTo>cacheddata</AssignTo>
</LookupCache>

View solution in original post

6 REPLIES 6

It's not quite clear what you want to cache

Is it the response you're trying to cache, while using the apikey as a key?

Then your populate cache should have as a source (source tag seems to be missing?) the response, and only apikey as a key

Your second proxy also needs to have already resolved the key variable

Finally, I wouldnt think it's a good idea to use apikey as a cachekey, and instead use something that is associated with the request that's made to the backend. An apikey is used for identity of your consuming application, and anyone could be the end user of such an app.

Also having a key prefix that you're not properly using is redundant, so you could remove it <Prefix>cacheKey</Prefix>

Hello @dane knezic

I want to use my response value from 1st proxy as the cache key. The API key will be the value.

What does the source do? My source will be from Response?

The source is what you want to cache..

I don't understand why you would want to use a response as the cache key and api key as a value however.

My second proxy request will required the response value from the first proxy as one of the payload. Then I can use it as the key, and api key as the value so that I can verify the API key.

Understand?

nope. I think it might help if you showed the entire policy configuration, as opposed to just a snip of the policy configuration.

And maybe provide a more verbose description of the request/response for each request (what you have described as #1 and #2).

@dane knezic is right. In your populate cache policy you are using both the response data and the apikey are the cache key fragments, and no value is cached since you havent mentioned the source value that needs to be cached.

My second proxy request will required the response value from the first proxy as one of the payload. Then I can use it as the key, and api key as the value so that I can verify the API key.

Its not a good practice to cache apikeys, you are actually allowing any request that has the response data value to use the apikey and get access to all the APIs provisioned for that key. The right practice is - your second proxy should receive the apikey in each request and validate it from the request parameter/header.

Here's how the populate and lookup cache would like based on what you mentioned

I want to use my response value from 1st proxy as the cache key. The API key will be the value.
PopulateCache policy:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PopulateCache async="false" continueOnError="false" enabled="true" name="Populate-Cache-1">
    <DisplayName>Populate Cache-1</DisplayName>
    <Properties/>
    <CacheKey>
        <Prefix/>
        <KeyFragment ref="response_data_ref"/>
    </CacheKey>
    <Scope>Global</Scope>
    <ExpirySettings>
        <TimeoutInSec>3600</TimeoutInSec>
    </ExpirySettings>
    <Source>apikey_ref</Source>
</PopulateCache>

LookupCache policy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LookupCache async="false" continueOnError="false" enabled="true" name="Lookup-Cache-1">
    <DisplayName>Lookup Cache-1</DisplayName>
    <Properties/>
    <CacheKey>
        <Prefix/>
        <KeyFragment ref="response_data_ref"/>
    </CacheKey>
    <Scope>Global</Scope>
    <AssignTo>cacheddata</AssignTo>
</LookupCache>