이 주제의 샘플 코드는 GitHub의 아웃바운드 OAuth 샘플 프록시를 기반으로 합니다. 이 샘플에서는 캐시 정책을 사용하여 여러 아웃바운드 호출에서 재사용할 수 있도록 OAuth 액세스 토큰을 저장합니다.
다음 예시에서는 PopulateCache 정책을 사용하여 OAuth 액세스 토큰이 캐시에 기록됩니다. OAuth 토큰은 LookupCache 정책의 후속 요청에 따라 검색됩니다. 액세스 토큰이 만료되면 자바스크립트를 사용하여 새로운 액세스 토큰을 검색합니다. 이 토큰은 PopulateCache 정책에 따라 캐시됩니다.
캐시 채우기
PopulateCache 정책을 사용하여 캐시에 데이터를 기록합니다. 이 예시에서는 OAuth 액세스 토큰을 캐시에 기록합니다. 정책 참조 정보는 캐시 채우기 정책을 참조하세요.
LookupCache 정책을 사용하여 캐시된 값을 검색할 수 있습니다. 다음 LookupCache 정책은 mycache에서 값을 읽고 값을 twitter-translate.apiAccessToken 변수에 기록합니다. 정책 참조 정보는 LookupCache 정책을 참조하세요.
[[["이해하기 쉬움","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-08-18(UTC)"],[[["\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```"]]