Jak odświeżyć / unieważnić pamięć podręczną zasobów $ w AngularJS


95

Mam prosty zasób User $, który korzysta z domyślnej implementacji pamięci podręcznej $ http:

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

Działa to bardzo dobrze, tzn. Mój serwer jest wywoływany tylko raz w mojej aplikacji, a następnie wartość jest pobierana z pamięci podręcznej.

Ale muszę odświeżyć wartość z serwera po pewnej operacji. Czy jest na to łatwy sposób?

Dzięki.


1
Używam niestabilnej (1.1.5, ale myślę, że jest tam od 1.1.2) cache- {boolean|Cache}- Jeśli prawda, domyślna pamięć podręczna $ http zostanie użyta do buforowania żądania GET, w przeciwnym razie, jeśli instancja pamięci podręcznej zbudowana za pomocą
Alexandre Bulté

1
mam podobny problem, ale tylko podczas testowania. jak to zepsuć na poziomie przeglądarki?
chovy

Odpowiedzi:


117

Zachowaj wartość logiczną i zdobądź $httppamięć podręczną:

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

Następnie możesz sterować nim jak każdą inną pamięcią podręczną utworzoną za pomocą $cacheFactory, przykład użycia podany poniżej:

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

52
Perfect, thank you! Exactly what I was looking for. For those wondering, you can call $cacheFactory.get('$http').remove(key), with key being the relative URL of your resource (ex: /api/user/current/51a9020d91799f1e9b8db12f).
Alexandre Bulté

2
Actually I found that I needed to specify the full url along with any query parameters when calling remove(). Am I missing something here?
shangxiao

3
I have dynamic query parameters. Is there a way to access the url from $resource factory?
suzanshakya

1
While this works. It may be more complexity than needed. A better solution would be if this was implemented: github.com/angular/angular.js/issues/9064
KFunk

5
For me, $cacheFactory.get('$http').removeAll() did the trick, as I needed to clear all cached data.
S. Baggy

18

Instead of using a boolean argument in the cache property of each action you can pass on a cache instance created with $cacheFactory which you can have more control over (i.e. clear the cache).

Example usage:

app.factory('Todos', function($resource, $cacheFactory) {
    var cache = $cacheFactory('todo');
    return $resource(apiBaseUrl + '/todos/:id', { id: '@id' }, {
        'get': { method: 'GET', cache: cache  },
        'query': { method: 'GET', cache: cache, isArray: true }
    });
});

Thanks. I saw that too, but I was looking for a "standard" way to do it before going down that path...
Alexandre Bulté

1
seems like a very "standard" approach conforming with the "angular way" :)
Variant

1
You're right. I meant an approach with the standard $resource cache.
Alexandre Bulté

6

I came across this thread looking for something similar, but found that $resource will manage the cache for you automatically, so there's no need to force the cache to be cleared.

The idea is that if you have a resource that you can query, that query response will be cached, but if you save something for that same resource, the previously cached data must be invalid, so it is cleared for you. It makes sense that it would work this way.

Here's some code I use to do this (you can ignore the possibly odd-looking factory creation part and pay attention to the "class" body).

'use strict';

sampleApp.players.$ng.factory('sampleApp.players.PlayerService', [
    '$log',
    '$resource',
    sampleApp.players.PlayerService = function ($log, $resource) {
        var service = {};

        $log.info('Creating player resource.');
        var Player = $resource('/api/players', {}, {query: {
            isArray: true,
            cache: true,
            method: 'GET'
        }});

        service.addPlayer = function(playerName) {
            $log.info('Saving a new player.');
            return new Player({name: playerName}).$save();
        };

        service.listPlayers = function () {
            $log.info('Fetching players.');
            return Player.query();
        };

        return service;
    }]);

If you call the listPlayers function several times, the first call makes a http get request and all subsequent calls are cached. If you call addPlayer though, a http post is performed as expected, and then the next call to listPlayers will perform a http get (not cached).

This keeps you out of the business of managing someone else's ($http) cache and trying to keep up with which url's are being used for requests and which are clearing caches at the right times.

I suppose the moral of the story here is to work with the library and all will be well... except for any bugs or incomplete features, but Angular doesn't have any of those ;)

p.s. This is all running on AngularJS 1.2.0.


2
Yes, I understand and acknowledge that in "normal" conditions the Angular resource knows how and when to invalidate the cache, and it works perfectly. But my use case was slightly different: I wanted to force a refresh because Angular had no way of knowing that a refresh was needed - the user object being modified outside the Angular app.
Alexandre Bulté

3
Can anyone point to where this is documented? I've read about this before on Stack Overflow, but cannot find any mention of it in the documentation. I've tried it as well in my app, but perhaps have done something wrong along the way...
Sunil D.

1
It does not appear however to work with $delete. The next call will pull from cache again and the deleted item will reappear. Can anyone confirm?
Lukus

This will not work ngResource DO NOT HANDLE cache invalidation go here for example : stackoverflow.com/questions/25117388/…
HugoPoi
Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.