Using the following policies, your proxy can store and retrieve cached data at runtime:
- PopulateCache policy to add data to the cache.
- LookupCache policy to access cached data.
- InvalidateCache policy to flush the cache.
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. (The ResponseCache policy uses a dedicated cache that is different and isolated from what is described here. For more information on response caching, see Reduce latency using ResponseCache.)
Each policy type is defined by an XML schema (.xsd). For reference, policy schemas are available on GitHub.
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 PopulateCache policy.
<PopulateCache name="token-cache"> <!-- The cache to write to. --> <CacheResource>mycache</CacheResource> <!-- The source of the data, a variable containing the value. --> <Source>twitter-translate.apiAccessToken</Source> <!-- An enumeration representing a prefix for namespace scope. --> <Scope>Exclusive</Scope> <!-- A unique pointer (a flow variable value) to the data. Use this later to retrieve it. --> <CacheKey> <KeyFragment>apiAccessToken</KeyFragment> <KeyFragment>ref='request.queryparam.client_id'</KeyFragment> </CacheKey> <!-- Entries placed into the cache with this policy will expire after 600 seconds. --> <ExpirySettings> <TimeoutInSec>600</TimeoutInSec> </ExpirySettings> </PopulateCache>
Variables can be populated by policies or by code. The
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. --> <CacheResource>mycache</CacheResource> <!-- Where to assign the retrieved value - here, a variable. --> <AssignTo>twitter-translate.apiAccessToken</AssignTo> <!-- An enumeration representing a prefix for namespace scope. --> <Scope>Exclusive</Scope> <!-- The unique pointer (a flow variable value) that was used to store the data in the cache. <CacheKey> <KeyFragment>apiAccessToken</KeyFragment> <KeyFragment>ref='request.queryparam.client_id'</KeyFragment> </CacheKey> </LookupCache>
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. --> <CacheResource>test-cache</CacheResource> <!-- An enumeration representing a prefix for namespace scope. --> <Scope>Exclusive</Scope> <!-- Fragments constructing the unique pointer used when the data was put into the cache. --> <CacheKey> <KeyFragment>apiAccessToken</KeyFragment> <KeyFragment ref="request.queryparam.client.id" /> </CacheKey> <PurgeChildEntries>true</PurgeChildEntries> </InvalidateCache>
Help or comments?
- Something's not working: See Apigee Support.
- Something's wrong with the docs: Click Send Feedback in the lower right.
(Incorrect? Unclear? Broken link? Typo?)