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

אתם צופים במסמכי התיעוד של Apigee Edge.
כדאי לעיין במסמכי התיעוד של Apigee X.
מידע

אפשר להשתמש במדיניות כדי לאחסן נתונים במטמון לשימוש כללי, וכך לאחזר אותם מהר יותר. באמצעות המדיניות הבאה, ה-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 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>