Rate This Article
Send Docs Feedback

Optimize performance using cache

You can use Edge caches to improve your proxy's performance by storing data from backend resources in a cache, where it can be retrieved more quickly. You can use policies to either store data generally (more or less whatever you choose).

Using the following policies, your proxy can store and retrieve cached data at runtime:

These policies are designed for general caching of data used by your proxies. In contrast, another caching policy, the ResponseCache policy, is designed specifically for caching response data from a backend resource.

The sample code in this topic is based on the Outbound OAuth sample proxy in on Github (see Samples reference). This sample uses the cache policy to store an OAuth access token for re-use across multiple outbound calls.

Each policy type is defined by an XML schema (.xsd). For reference, policy schemas are available on GitHub.

In the following example, an OAuth access token is written to the cache using a PopulateCache policy. The OAuth token is retrieved for subsequent requests by a LookupCache policy. Once the access token expires, JavaScript is used to retrieve a new access token, which is in turn cached by the PopulateCache policy.

Populate the cache

Use the PopulateCache policy to write data to the cache. This example writes an OAuth access token to the cache. For policy reference information, see Populate Cache policy.

<PopulateCache name="token-cache">
    <!-- The cache to write to. -->
    <!-- The source of the data, a variable containing the value. -->
    <!-- An enumeration representing a prefix for namespace scope. -->
    <!-- A unique pointer (a flow variable value) to the data. Use this later to retrieve it. -->
        <KeyFragment ref="request.queryparam.client_id"></KeyFragment>
    <!-- Entries placed into the cache with this policy will expire after 600 seconds. -->

Variables can be populated by policies or by code. The Source variable in this example is populated by the following JavaScript call: context.setVariable('twitter-translate.apiAccessToken', getAccessToken());

For more on cache keys, see Working with cache keys.

Lookup cached data

You can retrieve cached values with the LookupCache policy. The following LookupCache policy reads a value from mycache and writes the value to the variable twitter-translate.apiAccessToken. For policy reference information, see LookupCache policy.

<LookupCache name="token-cache">
    <!-- The cache to read from. -->
    <!-- Where to assign the retrieved value - here, a variable. -->
    <!-- An enumeration representing a prefix for namespace scope. -->
    <!-- The unique pointer (a flow variable value) that was used to store the data in the cache. -->

        <KeyFragment ref="request.queryparam.client_id"></KeyFragment>

Invalidate the cache

The cache can be invalidated explicitly by specifying an HTTP header. When a request that contains the specified HTTP header is received, the cache will be flushed. For policy reference information, see InvalidateCache policy.

<InvalidateCache name="InvalidateMyCache">
    <!-- The cache to invalidate. -->
    <!-- An enumeration representing a prefix for namespace scope. -->
    <!-- Fragments constructing the unique pointer used when 
        the data was put into the cache. -->
        <KeyFragment ref="request.queryparam.client.id" />

Help or comments?

  • Something's not working: Ask the Apigee Community or see Apigee Support
  • Something's wrong with the docs: Click Send Docs Feedback on this page.
    (Incorrect? Unclear? Broken link? Typo?)