jmdobry / angular-cache

angular-cache is a very useful replacement for the Angular 1 $cacheFactory.
http://jmdobry.github.io/angular-cache
MIT License
1.39k stars 156 forks source link

Expires value gets sometimes automatically set to null #211

Closed valnub closed 8 years ago

valnub commented 8 years ago

Hi there, I am using angular-cache 4.4.3 like this:

Configuration

  CacheFactory('listCacheOnline', {
    maxAge: 15 * 60 * 1000, // Items added to this cache expire after 15 minutes
    deleteOnExpire: 'aggressive', // Items will be deleted from this cache when they expire
    storagePrefix : 'my.news.',
    capacity : 1,
    storageMode: 'localStorage'
  });

Get data:

  $http.get(url, {cache: CacheFactory.get('listCacheOnline')}).then(function (result) {
    doSomethingWith(result);
  });

As you can see I am using 15 min expire time, so after 15 mins I'd expect that angular-cache invalidates this cache entry and gets fresh data from the server. I noticed though that this sometimes does not work. After running my app for a while (especially after not using it for a day or so) I noticed that it won't get the fresh data from the server any more.

When I looked into the issue I saw that the "data" value in localStorage had an attribute called "expires". This is sometimes set to null. If this happens my app will never get fresh data from the server and is stuck at this point. I have to manually flush localStorage and then it starts working normally again with "expires" actually containing a timestamp value until it stops working again when this null value reappears out of nowhere.

So, my question is: How is it possible that expires is set to null with the configuration set above?

jmdobry commented 8 years ago

I'm not sure, I will have to investigate.

cgwyllie commented 8 years ago

Hi,

Just to say I have observed this behaviour today when using a cache config like so:

        $http.defaults.cache = CacheFactory.createCache('offlineCache', {
            deleteOnExpire: 'none',
            maxAge: 10000,
            storageMode: 'localStorage'
        });

For me, switching deleteOnExpire to passive seems to resolve the issue (at least in my limited testing). However, it seems in this scenario that the cache entry will be served to $http after expiry is reached (which is the behaviour I want, but it seems like none was the correct option to use for that?).

angular-cache 4.4.3 angular 1.3.20 chrome 47.0.2526.106

ronaldheft commented 8 years ago

I'm seeing the expires value set to null as well, but my configuration is using deleteOnExpire: 'passive'.

CacheFactory.createCache('SocialManagerSelectorCache', {
        maxAge: 3 * 60 * 60 * 1000, // 3 hours
        deleteOnExpire: 'passive',
        storageMode: 'localStorage'
      });
jmdobry commented 8 years ago

Do any of you think this issue might be fixed by jmdobry/CacheFactory#8?

ronaldheft commented 8 years ago

I tried a number of different parameter order configurations related to jmdobry/CacheFactory#8 and I still occasionally see a null expiry on all of them.

Also related, I believe #212 is being affected by this. When using localStorage and the expire value is null, this line will return true, triggering the clearing of the key in localStorage.

jmdobry commented 8 years ago

Should be working better now: https://plnkr.co/edit/Cc1xT8Ra3PWXQvl2J9zs?p=preview

ronaldheft commented 8 years ago

:+1: Thanks!