ตัวอย่าง: การแคชเพื่อวัตถุประสงค์ทั่วไป

คุณกำลังดูเอกสารประกอบของ Apigee Edge
ไปที่เอกสารประกอบของ Apigee X
ข้อมูล

คุณใช้นโยบายเพื่อจัดเก็บข้อมูลไว้ในแคชสำหรับวัตถุประสงค์ทั่วไปเพื่อให้ดึงข้อมูลได้เร็วขึ้น พร็อกซีจะจัดเก็บและเรียกข้อมูลที่แคชไว้ขณะรันไทม์ด้วยการใช้นโยบายต่อไปนี้

นโยบายเหล่านี้ออกแบบมาสำหรับการแคชข้อมูลที่พร็อกซีของคุณใช้โดยทั่วไป

โค้ดตัวอย่างในหัวข้อนี้จะอิงตามพร็อกซีตัวอย่าง OAuth ขาออกใน GitHub (ดูรายการตัวอย่าง) ตัวอย่างนี้ใช้นโยบายแคชในการจัดเก็บโทเค็นเพื่อการเข้าถึง OAuth สำหรับใช้ซ้ำในการโทรออกหลายรายการ

นโยบายแต่ละประเภทจะกำหนดโดยสคีมา XML (.xsd) คุณสามารถดูสคีมานโยบายได้ใน GitHub

ในตัวอย่างต่อไปนี้ ระบบจะเขียนโทเค็นเพื่อการเข้าถึง OAuth ไปยังแคชโดยใช้นโยบาย buildingeCache ระบบจะเรียกโทเค็น OAuth สำหรับคำขอที่ตามมาด้วยนโยบาย LookupCache เมื่อโทเค็นเพื่อการเข้าถึงหมดอายุ ระบบจะใช้ JavaScript เพื่อเรียกโทเค็นเพื่อการเข้าถึงใหม่ ซึ่งก็มีการแคชไว้โดยนโยบาย CaptioneCache

ล้างแคช

ใช้นโยบาย roweCache เพื่อเขียนข้อมูลลงในแคช ตัวอย่างนี้เขียนโทเค็นเพื่อการเข้าถึง 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 ในตัวอย่างนี้เติมค่าโดยการเรียก JavaScript ต่อไปนี้ 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 ที่ระบุ ระบบจะล้างแคช สำหรับข้อมูลอ้างอิงนโยบาย โปรดดูนโยบาย TakeoutCache

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