簡體   English   中英

在 SuiteScript 2.0 中使用 N/Cache 模塊

[英]Using the N/Cache Module in SuiteScript 2.0

我正在嘗試在自定義模塊中實現 N/cache 模塊的使用,以跨包保留數據,而無需每次需要數據時都從遠程源檢索數據。 所以我創建了這個來獲取緩存數據:

function data_GetCachedData() {
    var remoteInfo = null;
    require(['N/cache'], function (cache) {
        var rmtCache = cache.getCache({
                name : _REMOTE_CACHE_NAME
            ,   scope : cache.Scope.PROTECTED
        });
        remoteInfo = rmtCache.get({
                key : _REMOTE_CACHE_INDEX
            ,   loader : comms_ObtainRemoteData(params)
        });
    });
    return JSON.parse(remoteInfo || "{ }");
}

我將其添加為加載程序:

function comms_ObtainRemoteData(params) {
    var remoteData = null;
    /*
        make HTTPS calls to remote server to add values to 'remoteData'
    */
    
    require(['N/cache'], function (cache) {
        var rmtCache = cache.getCache({
                name : _REMOTE_CACHE_NAME
            ,   scope : cache.Scope.PROTECTED
        });
        ptCache.put({
                key : _REMOTE_CACHE_INDEX
            ,   value : remoteData.values
            ,   ttl : (18 * 60 * 60)
        });
    });
    
    return remoteData.values;
}

但是,我添加了一些日志記錄,每次調用 GetCachedData 時,它總是會觸發加載程序。 這有什么我想念的嗎? 因為據我所知,這樣做應該很好,而不必總是調用加載程序。

您的腳本每次都調用遠程 function,因為您在帶有參數的加載程序 function 之后放置了括號。 請參閱此行loader: comms_ObtainRemoteData(params)如果您將其更改為loader: comms_ObtainRemoteData那么您應該沒問題。 cache.get 的“loader”選項需要 function 的名稱,而不是 function 調用的返回結果。

我遇到了同樣的問題,不得不切換代碼。 以下是您的代碼示例:

 var rmtCache = cache.getCache({ name: _REMOTE_CACHE_NAME, scope: cache.Scope.PROTECTED }); if (rmtCache==null){ rmtCache = remoteData.values ptCache.put({ key: _REMOTE_CACHE_INDEX, value: rmtCache, ttl: (18 * 60 * 60) }); }

正如 NetSuite 明確指出的那樣

不能保證緩存值在 ttl 值的整個持續時間內都保留在緩存中。 ttl 值表示可以存儲緩存值的最長時間。

所以可能是由於資源緊縮(可能是由於在所述帳戶中運行多個腳本/緩存)或緩存因未及時使用/請求而失效,它可以從 memory 重置/清除。

另外,您是否在測試(Dev/Sandbox)環境中測試您的腳本? 由於與生產環境相比,測試環境的資源有限。

我在我的開發環境中多次遇到同樣的問題,但它似乎在生產環境中運行良好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM