How to refresh / invalidate $resource cache in AngularJS

Alexandre Bulté picture Alexandre Bulté · Jun 12, 2013 · Viewed 59.4k times · Source

I have a simple User $resource that uses the default $http cache implementation like so:

factory('User', function($resource){
    return $resource(endpoint + '/user/current/:projectId', {},
        {get: 
            {
                cache: true,
                method: 'GET'
            }
        }
    );
})

This works very well, i.e. my server is only called once in my application, then the value is fetched from cache.

But I need to refresh the value from the server after a certain operation. Is there an easy way to do that?

Thanks.

Answer

Anthonny picture Anthonny · Jun 12, 2013

Keep the boolean and get the $http cache:

var $httpDefaultCache = $cacheFactory.get('$http');

Then you can control it like any another cache made with $cacheFactory, a usage instance provided below:

$httpDefaultCache.remove(key);
// Where key is the relative URL of your resource (eg: /api/user/current/51a9020d91799f1e9b8db12f)