שמירה במטמון לשימוש כללי

הדף הזה רלוונטי ל-Apigee ול-Apigee Hybrid.

לעיון במסמכי התיעוד של Apigee Edge

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

המדיניות הזו מיועדת לשמירת נתונים במטמון באופן כללי, נתונים שמשמשים את השרתים שלכם.

הקוד לדוגמה בנושא הזה מבוסס על Outbound OAuth sample proxy ב-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.

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