예시: 범용 캐싱

현재 Apigee Edge 문서가 표시되고 있습니다.
Apigee X 문서로 이동
정보

정책을 사용하여 더 빠른 검색을 위해 일반 용도 캐시에 데이터를 저장할 수 있습니다. 프록시는 다음 정책을 사용하여 런타임 시 캐시된 데이터를 저장하고 검색할 수 있습니다.

이러한 정책은 프록시에서 사용되는 데이터의 일반적인 캐싱에 맞추어 설계되었습니다.

이 주제의 샘플 코드는 GitHub의 아웃바운드 OAuth 샘플 프록시를 기반으로 합니다 (샘플 목록 참고). 이 샘플에서는 캐시 정책을 사용하여 여러 아웃바운드 호출에서 재사용할 수 있도록 OAuth 액세스 토큰을 저장합니다.

각 정책 유형은 XML 스키마 (.xsd)로 정의됩니다. 참고로 GitHub에서 정책 스키마를 확인할 수 있습니다.

다음 예시에서는 PopulateCache 정책을 사용하여 OAuth 액세스 토큰이 캐시에 기록됩니다. OAuth 토큰은 LookupCache 정책의 후속 요청에 따라 검색됩니다. 액세스 토큰이 만료되면 자바스크립트를 사용하여 새로운 액세스 토큰을 검색합니다. 이 토큰은 PopulateCache 정책에 따라 캐시됩니다.

캐시 채우기

PopulateCache 정책을 사용하여 캐시에 데이터를 기록합니다. 이 예시에서는 OAuth 액세스 토큰을 캐시에 기록합니다. 정책 참조 정보는 캐시 채우기 정책을 참조하세요.

<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>

변수는 정책 또는 코드로 채울 수 있습니다. 이 예시의 Source 변수는 다음 자바스크립트 호출로 채워집니다. context.setVariable('twitter-translate.apiAccessToken', getAccessToken());

캐시 키에 대한 자세한 내용은 캐시 키 다루기를 참조하세요.

캐시된 데이터 조회

LookupCache 정책을 사용하여 캐시된 값을 검색할 수 있습니다. 다음 LookupCache 정책은 mycache에서 값을 읽고 값을 twitter-translate.apiAccessToken 변수에 기록합니다. 정책 참조 정보는 LookupCache 정책을 참조하세요.

<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>

캐시 무효화

HTTP 헤더를 지정하여 캐시를 명시적으로 무효화할 수 있습니다. 지정된 HTTP 헤더가 포함된 요청을 수신하면 캐시가 삭제됩니다. 정책 참조 정보는 InvalidateCache 정책을 참조하세요.

<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>