for TTL 1h, see below example
add factory:
.factory('cacheInterceptor', ['$cacheFactory', function($cacheFactory) {
var http_ttl_cache = {};
return {
request: function(config) {
var N;
if (config.timeToLive) {
config.cache = true;
N = config.timeToLive;
delete config.timeToLive;
if (new Date().getTime() - (http_ttl_cache[config.url] || 0) > N) {
$cacheFactory.get('$http').remove(config.url);
http_ttl_cache[config.url] = new Date().getTime();
}
}
return config;
}
};
}])
then init in config push your interceptor.
An interceptor is simply a regular service factory that is registered to that array.
.config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider) {
$httpProvider.interceptors.push('cacheInterceptor');
example of request
$http.get('/permissions.json', {timeToLive: Constant.timeToLive}).then(function(result){
Constant is:
.constant('Constant', {
url: {
logout: '/auth/logout'
},
timeToLive: 60*60*1000
})
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…