[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-05。"],[[["\u003cp\u003eThis content applies to Apigee and Apigee hybrid, outlining how to manage general-purpose caching within these platforms.\u003c/p\u003e\n"],["\u003cp\u003eApigee utilizes three primary policies for caching: PopulateCache to add data, LookupCache to retrieve data, and InvalidateCache to flush the cache.\u003c/p\u003e\n"],["\u003cp\u003eThe provided sample code illustrates caching an OAuth access token for reuse, using a PopulateCache policy to write the token and a LookupCache policy to access it.\u003c/p\u003e\n"],["\u003cp\u003eCache keys, limited to 2 KB, are used to uniquely identify and retrieve cached data, as shown in the provided examples that use \u003ccode\u003eKeyFragment\u003c/code\u003e to construct the key.\u003c/p\u003e\n"],["\u003cp\u003eThe InvalidateCache policy can be used to explicitly flush the cache by using HTTP header specifications in the request, as demonstrated in the provided sample code.\u003c/p\u003e\n"]]],[],null,["*This page\napplies to **Apigee** and **Apigee hybrid**.*\n\n\n*View [Apigee Edge](https://docs.apigee.com/api-platform/get-started/what-apigee-edge) documentation.*\n\nYou can use policies to store data in a general purpose cache for faster retrieval. Using the\nfollowing policies, your proxy can store and retrieve cached data at runtime:\n\n- [Populate Cache policy](/apigee/docs/api-platform/reference/policies/populate-cache-policy) to add data to the cache.\n- [LookupCache policy](/apigee/docs/api-platform/reference/policies/lookup-cache-policy) to access cached data.\n- [InvalidateCache policy](/apigee/docs/api-platform/reference/policies/invalidate-cache-policy) to flush the cache.\n\n| **Note:** This topic describes general purpose caching. For information about response caching, see [Response Cache policy](/apigee/docs/api-platform/reference/policies/response-cache-policy), a reference with examples on the Response Cache policy.\n\nThese policies are designed for general caching of data used by your proxies.\n\nThe sample code in this topic is based on the\n[Outbound\nOAuth sample proxy](https://github.com/apigee/api-platform-samples/tree/master/sample-proxies/outbound-oauth) on GitHub. This sample uses the\ncache policy to store an OAuth access token for re-use across multiple outbound calls.\n\nIn the following example, an OAuth access token is written to the cache using a PopulateCache\npolicy. The OAuth token is retrieved for subsequent requests by a LookupCache policy. Once the\naccess token expires, JavaScript is used to retrieve a new access token, which is in turn cached\nby the PopulateCache policy.\n\nPopulate the cache\n\nUse the PopulateCache policy to write data to the cache. This example writes an OAuth access\ntoken to the cache. For policy reference information, see\n[Populate Cache policy](/apigee/docs/api-platform/reference/policies/populate-cache-policy).\n**Note:** Cache keys are limited to a size of 2 KB. \n\n```gdscript\n\u003cPopulateCache name=\"token-cache\"\u003e\n \u003c!-- The cache to write to. --\u003e\n \u003cCacheResource\u003emycache\u003c/CacheResource\u003e\n \u003c!-- The source of the data, a variable containing the value. --\u003e\n \u003cSource\u003etwitter-translate.apiAccessToken\u003c/Source\u003e\n \u003c!-- An enumeration representing a prefix for namespace scope. --\u003e\n \u003cScope\u003eExclusive\u003c/Scope\u003e\n \u003c!-- A unique pointer (a flow variable value) to the data. Use this later to retrieve it. --\u003e\n \u003cCacheKey\u003e\n \u003cKeyFragment\u003eapiAccessToken\u003c/KeyFragment\u003e\n \u003cKeyFragment ref=\"request.queryparam.client_id\"\u003e\u003c/KeyFragment\u003e\n \u003c/CacheKey\u003e\n \u003c!-- Entries placed into the cache with this policy will expire after 600 seconds. --\u003e\n \u003cExpirySettings\u003e\n \u003cTimeoutInSec\u003e600\u003c/TimeoutInSec\u003e\n \u003c/ExpirySettings\u003e\n\u003c/PopulateCache\u003e\n```\n\nVariables can be populated by policies or by code. The `Source` variable in this\nexample is populated by the following JavaScript call:\n`context.setVariable('twitter-translate.apiAccessToken', getAccessToken());`\n\nFor more on cache keys, see [Working with cache keys](/apigee/docs/api-platform/reference/policies/working-cachekeys).\n\nLookup cached data\n\nYou can retrieve cached values with the LookupCache policy. The following LookupCache policy\nreads a value from `mycache` and writes the value to the\nvariable `twitter-translate.apiAccessToken`. For policy reference\ninformation, see [LookupCache policy](/apigee/docs/api-platform/reference/policies/lookup-cache-policy). \n\n```gdscript\n\u003cLookupCache name=\"token-cache\"\u003e\n \u003c!-- The cache to read from. --\u003e\n \u003cCacheResource\u003emycache\u003c/CacheResource\u003e\n \u003c!-- Where to assign the retrieved value - here, a variable. --\u003e\n \u003cAssignTo\u003etwitter-translate.apiAccessToken\u003c/AssignTo\u003e\n \u003c!-- An enumeration representing a prefix for namespace scope. --\u003e\n \u003cScope\u003eExclusive\u003c/Scope\u003e\n \u003c!-- The unique pointer (a flow variable value) that was used to store the data in the cache. --\u003e\n\n \u003cCacheKey\u003e\n \u003cKeyFragment\u003eapiAccessToken\u003c/KeyFragment\u003e\n \u003cKeyFragment ref=\"request.queryparam.client_id\"\u003e\u003c/KeyFragment\u003e\n \u003c/CacheKey\u003e\n\u003c/LookupCache\u003e\n```\n\nInvalidate the cache\n\nThe cache can be invalidated explicitly by specifying an HTTP header. When a request that\ncontains the specified HTTP header is received, the cache will be flushed. For policy\nreference information, see [InvalidateCache policy](/apigee/docs/api-platform/reference/policies/invalidate-cache-policy). \n\n```gdscript\n\u003cInvalidateCache name=\"InvalidateMyCache\"\u003e\n \u003c!-- The cache to invalidate. --\u003e\n \u003cCacheResource\u003etest-cache\u003c/CacheResource\u003e\n \u003c!-- An enumeration representing a prefix for namespace scope. --\u003e\n \u003cScope\u003eExclusive\u003c/Scope\u003e\n \u003c!-- Fragments constructing the unique pointer used when \n the data was put into the cache. --\u003e\n \u003cCacheKey\u003e\n \u003cKeyFragment\u003eapiAccessToken\u003c/KeyFragment\u003e\n \u003cKeyFragment ref=\"request.queryparam.client_id\" /\u003e\n \u003c/CacheKey\u003e\n \u003cPurgeChildEntries\u003etrue\u003c/PurgeChildEntries\u003e\n\u003c/InvalidateCache\u003e\n```"]]