คุณกำลังดูเอกสารประกอบของ Apigee Edge
ไปที่
เอกสารประกอบของ Apigee X info
คุณสามารถใช้นโยบายเพื่อจัดเก็บข้อมูลในแคชอเนกประสงค์เพื่อให้ดึงข้อมูลได้เร็วขึ้น พร็อกซีสามารถจัดเก็บและดึงข้อมูลที่แคชไว้ได้ในรันไทม์โดยใช้นโยบายต่อไปนี้
- นโยบาย Populate Cache เพื่อเพิ่มข้อมูลลงในแคช
- นโยบาย LookupCache เพื่อเข้าถึงข้อมูลที่แคชไว้
- นโยบาย InvalidateCache เพื่อล้างแคช
นโยบายเหล่านี้ออกแบบมาเพื่อการแคชข้อมูลทั่วไปที่พร็อกซีใช้
โค้ดตัวอย่างในหัวข้อนี้อิงตามพร็อกซีตัวอย่าง OAuth ขาออกใน Github (ดู รายการตัวอย่าง) ตัวอย่างนี้ใช้นโยบายแคช เพื่อจัดเก็บโทเค็นเพื่อการเข้าถึง OAuth สำหรับใช้ซ้ำในการเรียกขาออกหลายรายการ
นโยบายแต่ละประเภทกำหนดโดยสคีมา XML (.xsd) สำหรับอ้างอิง สคีมานโยบาย มีให้บริการใน GitHub
ในตัวอย่างต่อไปนี้ โทเค็นเพื่อการเข้าถึง OAuth จะเขียนลงในแคชโดยใช้นโยบาย PopulateCache นโยบาย LookupCache จะดึงข้อมูลโทเค็น OAuth สำหรับคำขอที่ตามมา เมื่อโทเค็นเพื่อการเข้าถึงหมดอายุ ระบบจะใช้ JavaScript เพื่อดึงข้อมูลโทเค็นเพื่อการเข้าถึงใหม่ ซึ่งนโยบาย PopulateCache จะแคชโทเค็นดังกล่าว
ป้อนข้อมูลลงในแคช
ใช้นโยบาย PopulateCache เพื่อเขียนข้อมูลลงในแคช ตัวอย่างนี้เขียนโทเค็นเพื่อการเข้าถึง OAuth ลงในแคช ดูข้อมูลอ้างอิงนโยบายได้ที่ นโยบาย Populate Cache
<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 ที่ระบุ ระบบจะล้างแคช ดูข้อมูลอ้างอิงสำหรับนโยบาย ได้ที่นโยบาย 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>