Angular's $http has a cache built in. According to the docs:
cache – {boolean|Object} – A boolean value or object created with $cacheFactory to enable or disable caching of the HTTP response. See
$http Caching for more
information.
Boolean value
So you can set cache
to true in its options:
$http.get(url, { cache: true}).success(...);
or, if you prefer the config type of call:
$http({ cache: true, url: url, method: 'GET'}).success(...);
Cache Object
You can also use a cache factory:
var cache = $cacheFactory('myCache');
$http.get(url, { cache: cache })
You can implement it yourself using $cacheFactory (especially handly when using $resource):
var cache = $cacheFactory('myCache');
var data = cache.get(someKey);
if (!data) {
$http.get(url).success(function(result) {
data = result;
cache.put(someKey, data);
});
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…