使Angular中的所有$ http缓存无效 [英] Invalidating all $http caches in Angular
问题描述
我有一个Angular应用程序,其中包含基于Angular内置的$resource
服务的许多服务.其中许多使用cacheFactory
创建自己的独立缓存.但是,当有人注销时,我想将所有这些东西(包括命名的缓存和默认" $http
缓存)吹走.现在,我可以使用location.reload(true)
完成此操作,该功能当然可以工作,但是如果可以在不完全更改应用程序结构的情况下,无需重新加载就可以很好地实现它.
I have an Angular application with a number of services based on Angular's built-in $resource
service. Many of these use the cacheFactory
to create their own independent caches. However, I want to blow all of these away (both the named caches and the "default" $http
cache) when someone logs out. Right now I'm accomplishing this with location.reload(true)
, which certainly works, but it would be nice to achieve it without the reload if it's possible without totally changing the structure of the application.
为澄清起见,我知道如果我引用了范围内的单个缓存,则可以删除缓存的值,但是我想做的是全面删除所有缓存,而不必知道他们都叫什么.
To clarify, I'm aware that I can remove the cached values if I have a reference to the individual cache in scope, but what I want to do is to remove all caches, across the board, without necessarily having to know what they're all called.
推荐答案
您可以注入 $cacheFactory
并从工厂构造函数中获取缓存对象(例如:$cacheFactory.get('$http')
),然后使用removeAll()
清理所有缓存.如果要完全删除缓存对象,请使用destroy()
.
You can inject $cacheFactory
and get the cache object from the factory constructor (eg: $cacheFactory.get('$http')
) and use removeAll()
to cleanup all the cache. Use destroy()
if you want to remove the cache object altogether.
In order to get all the cacheObject id's you could use $cacheFactory.info()
which will return object with summary info for each cache object {id:'cacheObjId', size:'cacheSize'}
.
示例:-
angular.forEach($cacheFactory.info(), function(ob, key) {
$cacheFactory.get(key).removeAll();
});
您可以将removeAll
/destroyAll
函数添加到cacheFactory,以便通过装饰$cacheFactory
可以在其他任何地方使用它,
You could add removeAll
/ destroyAll
function to the cacheFactory so that you can use it anywhere else by decorating the $cacheFactory
, something like this.
.config(['$provide',
function($provide) {
$provide.decorator('$cacheFactory', function($delegate) {
$delegate.removeAll = function() {
angular.forEach($delegate.info(), function(ob, key) {
$delegate.get(key).removeAll();
});
}
$delegate.destroyAll = function() {
angular.forEach($delegate.info(), function(ob, key) {
$delegate.get(key).destroy();
});
}
return $delegate;
});
}
])
angular.module('App', [])
.config(['$provide',
function($provide) {
$provide.decorator('$cacheFactory', function($delegate) {
$delegate.removeAll = function() {
angular.forEach($delegate.info(), function(ob, key) {
$delegate.get(key).removeAll();
});
}
$delegate.destroyAll = function() {
angular.forEach($delegate.info(), function(ob, key) {
$delegate.get(key).destroy();
});
}
return $delegate;
});
}
])
.run(function($cacheFactory) {
var value = 123;
$cacheFactory('cache1').put('test', value);
$cacheFactory('cache2').put('test', value);
console.log($cacheFactory.info());
$cacheFactory.removeAll();
console.log($cacheFactory.info());
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="App">
</div>
这篇关于使Angular中的所有$ http缓存无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!