Adr*_*n E 10 caching angularjs
我读到了Angular'$ cacheFactory',但找不到有关为缓存内容设置过期日期的任何文档.
如果我想将所有GET请求缓存30秒,如何在'$ cacheFactory中定义它,或者我是否需要自己扩展功能,该怎么办?
Anj*_*ova 12
对于TTL 1h,请参见下面的示例
添加工厂:
.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;
}
};
}])
Run Code Online (Sandbox Code Playgroud)
然后init in config推送你的拦截器.拦截器只是注册到该阵列的常规服务工厂.
.config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider) {
$httpProvider.interceptors.push('cacheInterceptor');
Run Code Online (Sandbox Code Playgroud)
请求的示例
$http.get('/permissions.json', {timeToLive: Constant.timeToLive}).then(function(result){
Run Code Online (Sandbox Code Playgroud)
常数是:
.constant('Constant', {
url: {
logout: '/auth/logout'
},
timeToLive: 60*60*1000
})
Run Code Online (Sandbox Code Playgroud)
Oka*_*ari 10
我也遇到了这个问题.默认的$ cacheFactory没有时间(TTL).
您需要自己实现.但之前,你可以看一看,看看是否有人已经这样做了:
这个看起来很完整 - http://jmdobry.github.io/angular-cache/
如果你真的想要实现自己的解决方案(通过实现自己的$ cacheFactory)并需要一些帮助,请随时提问.
希望它能给你一些线索.
| 归档时间: |
|
| 查看次数: |
10283 次 |
| 最近记录: |