Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force Angular to clean $cacheFactory after some action

So we cache values from drop down in dropdown resourse module in this way

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

And then in $http callback we save data for dropdown in this way

cache.put('dataForDropDown', data);

In other module we have posibility to change user setting especially to change language. After this action i need to clean cache. Because when user change language and goes to the page with dropdown he wants to see dropdown with needed language.

So at the page with dropdown we need to send call to server one more time. To do it we need to clean cache after language is change.

How can we do it?

I tries this code in drop down module.

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

But it doesn't work. It says that cache with name dataForDropDown have been already created. I can't understand what i am doing wrong

like image 676
VladosJS Avatar asked Oct 30 '22 19:10

VladosJS


1 Answers

According to documentation, $cacheFactory('name') returns a new cache object. If you want to get the cache object to call its removeAll method write something like this:

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

This registers a factory to your module. the factory superCache contains cache object. You can clear it by calling removeAll method. Or you can remove a specific item from your cache using remove(key) method and it is injectable (like other angular factories) in controllers and services.

like image 116
alisabzevari Avatar answered Nov 15 '22 05:11

alisabzevari