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:
- Populate Cache policy to add data to the cache.
- LookupCache policy to access cached data.
- InvalidateCache policy to flush the cache.
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.
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. --> <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>