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

Question: how to use with onExpire in factory with $resource? #97

Closed davidwparker closed 10 years ago

davidwparker commented 10 years ago

I couldn't see where to contact you, so I'm asking here (sorry!)

Question: why isn't my onExpire function firing?

CODE (I'm injecting $angularCacheFactory into a factory which a resource):

      $angularCacheFactory('userCache', {
        maxAge: 20000, // 20 seconds for testing
        recycleFreq: 1000, // 1 second for testing
        deleteOnExpire: 'aggressive',
        onExpire: function() {
          console.log('expired!');
        }
      });
        var User = $resource(
            '/users/:id',
            {id:"@id"},
            {
              query: {
                method:'GET',
                isArray:true,
                cache:'userCache',
              },
            }
        );

Thanks!

Edit: I'm using angular-cache-2.3.1.js

davidwparker commented 10 years ago

Oh, I should have explored a little more. Seems that Angular's resource uses it's own internal cache still (with the name), I have to reference the actual $angularCacheFactory:

        var userCache = $angularCacheFactory.get('userCache');
        var User = $resource(
            '/users/:id',
            {id:"@id"},
            {
              query: {
                method:'GET',
                isArray:true,
                cache:userCache,
              }
            }
        );

Closing!