我正尝试使用Underscore.js
中的memoize
函数缓存一个ajax调用的结果。我不确定我的实现是否正确,也不知道如何使用键检索缓存的结果数据。以下是我的实现代码:
Javascript 代码:
var cdata = $http
.get(HOST_URL + "/v1/report/states")
.success(function(data) {
//put the result in the angularJs scope object.
$scope.states = data;
});
//store the result in the cache.
var cachedResult = _.memoize(
function() {
return cdata;
}, "states");
我使用记忆化技术将ajax的结果存储起来是正确的吗?一旦放入缓存中,如何根据关键字检索它,例如"states"。