Lookup cache hit always shows True

Not applicable

Hi ,

I am trying to Implement cache mechanism in response flow. but my Lookup cache hit always shows true as below .

lookupcache.Lookup-Cache-CrtPackageID.cachehittrue

XML Populate Cache :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PopulateCache async="false" continueOnError="false" enabled="true" name="Populate-Cache_CrtPackageID">
    <DisplayName>Populate Cache_CrtPackageID</DisplayName>
    <Properties />
    <CacheKey>
        <Prefix />
        <KeyFragment ref="response.header.Content-Type" />
    </CacheKey>
    <CacheResource>Test1</CacheResource>
    <Scope>Application</Scope>
    <ExpirySettings>
        <TimeoutInSec>30</TimeoutInSec>
    </ExpirySettings>
    <Source>crtidvalue</Source>
</PopulateCache>

XML :lookup cache

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LookupCache async="false" continueOnError="false" enabled="true" name="Lookup-Cache-CrtPackageID">
    <DisplayName>Lookup-Cache-CrtPackageID</DisplayName>
    <Properties />
    <CacheKey>
        <Prefix />
        <KeyFragment ref="response.header.Content-Type" />
    </CacheKey>
    <CacheResource>Test1</CacheResource>
    <Scope>Application</Scope>
    <AssignTo>crt_cache</AssignTo>
</LookupCache>

Can anyone suggest me how can I resolve the issue?

Solved Solved
0 12 1,581
1 ACCEPTED SOLUTION

Not applicable

Hi @Ambili Venugopal

Instead of using populate and lookup cache policies, you need to use response cache policy.

The response cache policy automatically adds a policy fragment in both request and response flows.

here is a sample code.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ResponseCache async="false" continueOnError="false" enabled="true" name="ResponseCacheForProductSearch">
    <DisplayName>ResponseCachePolicy</DisplayName>
    <Properties/>
    <CacheKey>
        <Prefix/>
        <KeyFragment ref="request.uri" type="string"/>
        <KeyFragment ref="request.querystring" type="string"/>
    </CacheKey>
    <Scope>Exclusive</Scope>
    <ExpirySettings>
        <TimeoutInSec ref="">3600</TimeoutInSec>
    </ExpirySettings>
    <SkipCacheLookup>request.verb=="POST"</SkipCacheLookup>
    <SkipCachePopulation>request.verb=="POST"</SkipCachePopulation>
    <ExcludeErrorResponse>true</ExcludeErrorResponse>
    <UseAcceptHeader>true</UseAcceptHeader>
    <CacheResource>ProductsCache</CacheResource>
</ResponseCache>

the above code will make sure your key for the cache is formed by the "CacheKey" and the corresponding response is stored against the Cachekey automatically.

View solution in original post

12 REPLIES 12

what are you trying to do? can you describe your flow with populate and lookup cache -- i see you are seeing 'response.header.Content-Type' as the cache key, which i think, is supposed to give you the results you are seeing?

yes, in other words, if the request header Content-type is application/json, then the result will always be served from cache. Is that what you want, @Ambili Venugopal ?

Hi ,

I need to cache one set of values from Response content but in the response there is no specific key so I given response header content type as key reference ..I am trying to cache another value which I extracted and kept from response content

@Ambili Venugopal The cache key should usually be one of the attributes of the request that you are trying to cache. I am not sure why you would use the response content-type as the cache key. If there's nothing unique about the request you might consider using the request.url and request.verb as the cache keys.

I am attaching a sample bundle that demonstrates the use of the lookup cache policy and related policies. Please take a look.apigee-discovery-rev21-2016-03-22.zip

You should use as part of the cache key, a part of the REQUEST, not a part of the response. The Response needs to be stored as the cached value, not as the cache key.

The cache is a key/value store. That means, for a given key (let's say, the key value is "A"), there is a given value, for example, "Please do the needful"). For another key, let's say "B", there is a different value, perhaps "Like that only."

This map might be illustrated this way:

A => "Please do the needful"
B => "Like that only."

When you request from cache the item 'A', you will always get "Please do the needful" until the cache entry expires. If you were going to use this cache to back an API, it might be like this:

GET /phrase/A => "Please do the needful"
GET /phrase/B => "Like that only."

Which says, when you receive a GET on urlpath /phrase/A, you will always get the first phrase, etc.

In your case, you wish to cache API responses (I think). In this case you must use some part of the inbound API request. This can be the uri path, the request headers, the user authentication token (if present) or some combination of these things.

Ravi Shah has provided a good example in his answer, for how to specify a unique cache key. like so:

  <CacheKey>
      <Prefix/>
      <KeyFragment ref="request.uri" type="string"/>
      <KeyFragment ref="request.querystring" type="string"/>
  </CacheKey>
  <ExcludeErrorResponse>true</ExcludeErrorResponse>
  <UseAcceptHeader>true</UseAcceptHeader>

This will cache a response based on the inbound URI and querystring.

Not applicable

Assuming that you are working on Response Cache and have added them in both request and response flows.

I suggest making your CacheKey is unique. I generally keep the query params,uri and Accept headers in the cache key.

    <CacheKey>
        <Prefix/>
        <KeyFragment ref="request.uri" type="string"/>
        <KeyFragment ref="request.querystring" type="string"/>
    </CacheKey>
    <ExcludeErrorResponse>true</ExcludeErrorResponse>
    <UseAcceptHeader>true</UseAcceptHeader>

Also, make sure to exclude the error cases from cache as mentioned below.

Hi All,

Thanks for the suggestions .but still my lookup doesn't seems to be working

I am populating and looking up the value in the same request . Next after the populate policy I have used lookup. I have changed the key to request.uri for both populate and lookup .but still the first call itself showing lookupcache.Lookup-Cache-CrtPackageID.cachehit is true.I need to populate one Id from response and have to lookup the same ID .when lookupcache.Lookup-Cache-CrtPackageID is false I need to make another service call.

.

Hi All,

will the response cache work only for default target end point ! If I wanted to cache service callout response in response cache how can I do that ?

Thank you

In that case you can use the LookupCache and PopulateCache policies.

Take similar care with the cachekey for those policies.

Not applicable

Hi @Ambili Venugopal

Instead of using populate and lookup cache policies, you need to use response cache policy.

The response cache policy automatically adds a policy fragment in both request and response flows.

here is a sample code.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ResponseCache async="false" continueOnError="false" enabled="true" name="ResponseCacheForProductSearch">
    <DisplayName>ResponseCachePolicy</DisplayName>
    <Properties/>
    <CacheKey>
        <Prefix/>
        <KeyFragment ref="request.uri" type="string"/>
        <KeyFragment ref="request.querystring" type="string"/>
    </CacheKey>
    <Scope>Exclusive</Scope>
    <ExpirySettings>
        <TimeoutInSec ref="">3600</TimeoutInSec>
    </ExpirySettings>
    <SkipCacheLookup>request.verb=="POST"</SkipCacheLookup>
    <SkipCachePopulation>request.verb=="POST"</SkipCachePopulation>
    <ExcludeErrorResponse>true</ExcludeErrorResponse>
    <UseAcceptHeader>true</UseAcceptHeader>
    <CacheResource>ProductsCache</CacheResource>
</ResponseCache>

the above code will make sure your key for the cache is formed by the "CacheKey" and the corresponding response is stored against the Cachekey automatically.

Hi Ravi,

Sorry I am new to this cache so just got confused how response key will work .I cannot mention <Source> in the response cache so that means it will cache full response isn't it ? I need to cache one set of IDS not full response.without populate and lookup cache how it will be possible

Hi @Ambili Venugopal,

If you want to cache servicecallout response please use populate and lookup cache by defining proper cache key(cache key should be selected carefully).