דוגמה: שמירה למטרה כללית

כרגע מוצג התיעוד של Apigee Edge.
כניסה למסמכי התיעוד של Apigee X.
מידע

כדי לאחזר נתונים מהר יותר, תוכלו להשתמש במדיניות לשמירת נתונים במטמון לשימוש כללי. באמצעות כללי המדיניות הבאים, שרת ה-proxy יכול לאחסן ולאחזר נתונים שנשמרו במטמון בזמן ריצה:

כללי המדיניות האלה נועדו לשמור במטמון נתונים שמשמשים את שרתי ה-proxy שלכם.

הקוד לדוגמה בנושא הזה מבוסס על שרת ה-proxy לדוגמה של OAuth יוצא ב-GitHub (פרטים נוספים זמינים ברשימת הדוגמאות). בדוגמה הזו נעשה שימוש במדיניות המטמון כדי לאחסן אסימון גישה מסוג OAuth לשימוש חוזר בשיחות יוצאות מרובות.

כל סוג מדיניות מוגדר באמצעות סכימת XML (.xsd). סכימות המדיניות זמינות ב-GitHub.

בדוגמה הבאה, אסימון גישה מסוג 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.

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