Was this helpful?

ResponseCache can be configured to save and periodically refresh copies of response messages. As apps make requests to the same URI, the API Platform can be configured to return cached responses, rather than forwarding those requests to the backend server.

ResponseCache is commonly used when backend data is updated only periodically. For example, an API that exposes weather report data might only refresh the weather data once every ten minutes. By returning cached responses and refreshing once every ten minutes, ResponseCache decreases the number of request reaching the backend, and also eliminates a network hop for a significant portion of requests to the API.

There are two main benefits to ResponseCache:

  • Reduced latency: The request is satisfied from the cache (removing a network hop), so the response is returned in a shorter time, making the API more responsive.
  • Reduced network traffic: Representations are reused, so the impact of processing duplicate or redundant requests is reduced. ResponseCache reduces the amount of bandwidth consumed overall.

Unlike other policies, a ResponseCache policy is attached to both request and response paths within a Flow. Cached response messages are returned to requesting apps based on request message keys configured in the policy.  The API Platform inspects request message for a configured key, and when the key is present, it returns the cached response.

Sample

For example, responses to the following API call need to be cached for ten minutes.

http://weather.yahooapis.com/forecastrss?w=12797282

The query parameter w, which designates WOIED, can be used as a cache key. Whenever a request is received, the API Platform checks the value of the query parameter w. If a valid response is present in the cache, the cached response is returned to the requesting client.

.

<ResponseCache name="ReponseCache">
    <CacheKey>
        <KeyFragment ref="request.queryparam.w" />
    </CacheKey>
    <ExpirySettings>
        <TimeoutInSec>6000</TimeoutInSec>
    </ExpirySettings>
</ResponseCache>

Attaching a ResponseCache policy

ResponseCache is unique in that it must be attached to both the request and response Flow. ResponseCache is usually attached to the ProxyEndpoint PreFlow Request and Response, as in this example:

<PreFlow name="PreFlow">
    <Request>
        <Step><Name>ResponseCache</Name></Step>
    </Request>
    <Response>
        <Step><Name>ResponseCache</Name></Step>
    </Response>
</PreFlow>

In the request path, SkipCacheLookup condition is evaluated. Look up is skipped and the cache is refreshed if the condition evaluates to true. For example, the following configuration refreshes the cached response for the configuration below:

'http://{apifactory}org_name}-test.apigee.net/weather/forecastrss?w=23424778' -H "bypass-cache:true"
<ResponseCache name="ResponseCache">
    <CacheKey>
        <KeyFragment ref="request.queryparam.w" />
    </CacheKey>
    <!-- Explicitly refresh the cached response -->
    <SkipCacheLookup>request.header.bypass-cache = "true"</SkipCacheLookup>
    <ExpirySettings>
        <TimeoutInSec>6000</TimeoutInSec>
    </ExpirySettings>
</ResponseCache>

A common use of conditional cache refresh is a condition that defines a specific HTTP header that causes the condition to evaluate to true. A scripted client application could be configured to periodically submit a request with the appropriate HTTP header, explicitly causing the response cache to refresh.

For more information about conditions, see Create dynamic API flows using conditions.

Note: ResponseCache relies on a default cache resource configured for your organization when your API Platform account is provisioned. If you have questions about your cache resource configuration, contact Apigee Support.

Configuring the ResponseCache policy

Configure the ResponseCache policy using the following elements.

Field Name Description
CacheResource (Optional) Specifies the cache where messages should be stored. A default cache is available for all organizations on the API Platform.
Scope (Optional)
Used to construct the prefix, when Prefix in CacheKey is not provided. If Prefix is provided, then Scope is ignored.
Allowed values are Exclusive, Global, Application, Proxy, and Target.
  • Exclusive (default): ApiName_ProxyName if the policy is attached to the ProxyEndpoint flow. ApiName_TargetName if the policy is attached at TargetEndpoint flow.
  • Global: Keys are shared across all API proxies deployed in the environment.
  • Application: ApiName is used as the prefix for all cache keys.
  • Proxy: ApiName_ProxyName is used as the prefix for all cache keys.
  • Target: ApiName_TargetName is used as the prefix for all cache keys.
CacheKey (Optional) Prefix
Used for matching requests to the cached response.
If provided, used as prefix for all entries  stored in the cache by the policy.
KeyFragment
A cache key fragment can either contain a key or a value.
All the fragments together (plus the prefix) are concatenated to create the cache key. For example, if a request message has headers id1=1 & id2=1, the cache key is Apigee__1__2.
SkipCacheLookup (Optional) Valid values: true/false
SkipCachePopulation (Optional) Valid values: true/false
ExpirySettings (Optional) TimeoutInSeconds Duration (in seconds, ss) to leave for the cached entry.
TimeOfDay A specific time (hh:mm:ss) of day.
ExpiryDate Specifies the date (mm-dd-yyyy).

Policy schema

Each policy must conform to a policy schema. All policy constructs such as elements and attributes mentioned above are defined in a schema. To download the schema, click here.

Example - ResponseCache

<ResponseCache name="ResponseCache">
 <CacheResource>mycache</CacheResource>
    <Scope>Global</Scope>
    <CacheKey>
        <Prefix>Apigee</Prefix>
        <KeyFragment ref="request.path" />
    </CacheKey>
    <SkipCacheLookup>{condition}</SkipCacheLookup>
    <SkipCachePopulation>{condition}</SkipCachePopulation>
    <ExpirySettings>
        <TimeoutInSec>300</TimeoutInSec> OR <TimeoutInSec ref="response.header.timeout" />
    </ExpirySettings>
</ResponseCache>

ResponseCache flow variables

Flow variables play a pivotal role in message flow. These variables enable advanced users to customize the behavior of policies and flows, based on the headers or message content, or the context available in the flow. The following predefined flow variables are available after a ResponseCache policy is executed. For more information about flow variables, see Variables reference.

Variables Type Permission Description
responsecache.<policy-name>.cachename String Read-Only Returns the cache used in the policy
responsecache.<policy-name>.cachekey String Read-Only Returns the key used
responsecache.<policy-name>.cachehit Boolean Read-Only True if the policy execution is successful
responsecache.<policy-name>.invalidentry Boolean Read-Only True if the cache entry in not valid