הדף הזה רלוונטי ל-Apigee ול-Apigee Hybrid.
לעיון במסמכי התיעוד של
Apigee Edge
אתם יכולים להשתמש במדיניות כדי לאחסן נתונים במטמון לשימוש כללי, כדי לשלוף אותם מהר יותר. באמצעות המדיניות הבאה, ה-proxy יכול לאחסן ולשלוף נתונים שנשמרו במטמון בזמן הריצה:
- מאכלסים את מדיניות המטמון כדי להוסיף נתונים למטמון.
- מדיניות LookupCache כדי לגשת לנתונים שנשמרו במטמון.
- InvalidateCache policy כדי לנקות את המטמון.
כללי המדיניות האלה מיועדים לשמירת נתונים במטמון באופן כללי, נתונים שמשמשים את השרתים שלכם.
קוד הדוגמה בנושא הזה מבוסס על דוגמה לשרת proxy של OAuth לשיחות יוצאות ב-GitHub. בדוגמה הזו נעשה שימוש במדיניות שמירת נתונים במטמון כדי לאחסן טוקן גישה של OAuth לשימוש חוזר בכמה שיחות יוצאות.
בדוגמה הבאה, אסימון גישה של OAuth נכתב למטמון באמצעות מדיניות PopulateCache. אסימון ה-OAuth מאוחזר לבקשות הבאות באמצעות מדיניות LookupCache. אחרי שפג התוקף של אסימון הגישה, נעשה שימוש ב-JavaScript כדי לאחזר אסימון גישה חדש, שמאוחסן במטמון על ידי מדיניות 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 בדוגמה הזו מאוכלס באמצעות הקריאה הבאה ל-JavaScript:
context.setVariable('twitter-translate.apiAccessToken', getAccessToken());
מידע נוסף על מפתחות מטמון זמין במאמר עבודה עם מפתחות מטמון.
חיפוש נתונים שנשמרו במטמון
אפשר לאחזר ערכים ששמורים במטמון באמצעות המדיניות LookupCache. מדיניות LookupCache הבאה קוראת ערך מ-mycache וכותבת את הערך למשתנה twitter-translate.apiAccessToken. מידע על מדיניות זמין במאמר LookupCache policy.
<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 policy.
<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>