执行某些操作后强制 Angular 清理 $cacheFactory

Force Angular to clean $cacheFactory after some action

所以我们以这种方式在下拉资源模块中缓存下拉列表中的值

var cache = new CacheFactory('DropDownResource', {
      maxAge: 3600000,
      deleteOnExpire: 'aggressive',
      capacity: 5,
      storageMode: 'sessionStorage'
    });

然后在 $http 回调中我们以这种方式保存下拉列表的数据

cache.put('dataForDropDown', data);

在其他模块中,我们可以更改用户设置,尤其是更改语言。执行此操作后,我需要清理缓存。因为当用户更改语言并转到带有下拉菜单的页面时,他希望看到带有所需语言的下拉菜单。

因此在带有下拉菜单的页面上,我们需要再次向服务器发送调用。为此,我们需要在更改语言后清理缓存。

我们该怎么做?

我在下拉模块中尝试了这段代码。

var cache = $cacheFactory('dataForDropDown'); cache.remove('dataForDropDown');

但是没有用。它说已经创建了名为 dataForDropDown 的缓存。我不明白我做错了什么

根据文档,$cacheFactory('name') returns 一个新的缓存对象。如果你想让缓存对象调用它的removeAll方法,写这样的东西:

angular.module('superCache')
  .factory('superCache', ['$cacheFactory', function($cacheFactory) {
    return $cacheFactory('super-cache');
  }]);

这会为您的模块注册一个工厂。工厂 superCache 包含缓存对象。您可以通过调用 removeAll 方法清除它。或者您可以使用 remove(key) 方法从缓存中删除特定项目,并且它是可注入的(与其他 angular 工厂一样)在控制器和服务中。