[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間: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,["# General purpose caching\n\n*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------------------\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------------------\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--------------------\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```"]]