According to the $http documentation, it is possible to customize your own cache object instance by using the cache configuration option.
In this example, a custom $cacheFactory
is created with an overridden put
method to automatically clear the cache after a certain timeout period. Please note that this implementation is specific to a single URL. To create a more generic timer cache object, further modifications may be needed.
function Ctrl($scope, $http, $cacheFactory, $timeout) {
var timedCache = $cacheFactory('myCache'),
TIMEOUT = 5000,
cacheTimer = null;
timedCache._put = timedCache.put;
timedCache.put = function (key, val) {
console.log('caching', key);
$timeout.cancel(cacheTimer);
cacheTimer = $timeout(function () {
console.log('clearing cache for', key);
timedCache.remove(key);
}, TIMEOUT, false);
timedCache._put(key, val);
};
$scope.request = function () {
$http.get('/echo/json', {
cache: timedCache
})
.success(function (data) {
console.log('received', data);
});
};
}
To see this in action, check out the working fiddle here: http://jsfiddle.net/sirhc/jvLPX/