0
我有一個函數,應該從sessionStorage返回一個字典,如果它存在,如果不存在,它將從API加載字典到sessionStorage,然後返回字典。從sessionStorage服務需要很長時間
self.getDictionary = function(dictionary){
var deferred = $q.defer();
var saved_dictionary = JSON.parse($window.sessionStorage.getItem('dictionary_' + dictionary));
if (saved_dictionary && saved_dictionary !== "null"){
deferred.resolve(saved_dictionary);
} else {
var apiData = {module: "Dictionaries", method: "getDictionary", data: {name: dictionary}};
apiService.execute(apiData).then(function (response) {
$window.sessionStorage.setItem('dictionary_' + dictionary, JSON.stringify(response));
deferred.resolve(response);
});
}
return deferred.promise;
};
我想緩存字典,因爲它可能非常大(約0.5MB JSON)。
樣品詞典:
{"DictionaryName":"Gaming","DictionaryCategory":[{"CategoryName":"Games","MandatoryAtLeast":1,"CategoryWords":[{"title":"zelda","score":1,"exact":true,"mandatory":false,"reject":false},{"title":"mass effect","score":1,"exact":false,"mandatory":true,"reject":false},{"title":"pokemon","score":1,"exact":false,"mandatory":true,"reject":false},{"title":"fallout","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"cs:go","score":1,"exact":true,"mandatory":false,"reject":false},{"title":"sims","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"until dawn","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"deus ex","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"skyrim","score":1,"exact":false,"mandatory":true,"reject":false}]},{"CategoryName":"Companies","MandatoryAtLeast":1,"CategoryWords":[{"title":"bioware","score":1,"exact":false,"mandatory":true,"reject":false},{"title":"bethesda","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"steam","score":1,"exact":false,"mandatory":true,"reject":false},{"title":"valve","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"alienware","score":1,"exact":false,"mandatory":false,"reject":false}]},{"CategoryName":"other","MandatoryAtLeast":1,"CategoryWords":[{"title":"gamer","score":1,"exact":false,"mandatory":true,"reject":false},{"title":"mods","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"horror","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"survival","score":1,"exact":true,"mandatory":false,"reject":false},{"title":"multiplayer","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"action","score":1,"exact":true,"mandatory":true,"reject":false},{"title":"fps","score":1,"exact":false,"mandatory":true,"reject":false},{"title":"shooter","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"mmo","score":1,"exact":false,"mandatory":true,"reject":false}]}]}
的問題是,從高速緩存服務的字典是沒有幫助。從sessionStorage服務字典所用的時間幾乎與從遠程API加載字典所需的時間一樣長。
爲什麼從sessionStorage返回這麼久?
爲什麼在調用'JSON.parse()'之前不檢查'sessionStorage'中是否定義了key? – guest271314
我想我可以,但我不認爲這會有很大的不同。據我所知,在空值上調用JSON.parse()是可忽略的 –
很長時間?花費的時間有什麼問題? – charlietfl