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

Data Is Undefined When Refreshing the page. #175

Closed Kroitoru closed 9 years ago

Kroitoru commented 9 years ago

Hey, I get the data by the http method properly. At the time I refresh the page the data is lost. Do you have any idea why after refreshing the page dataCache.get("surveyList") is undefined?

   app.run(function ($http, CacheFactory) {
            $http.defaults.cache = CacheFactory('defaultCache', {
                maxAge: 15 * 60 * 1000, // Items added to this cache expire after 15 minutes
                cacheFlushInterval: 60 * 60 * 1000, // This cache will clear itself every hour
                deleteOnExpire: 'aggressive' // Items will be deleted from this cache when they expire
            });
        });
        app.service('CacheService', function ($http, $q, CacheFactory) {
            return {
                getSurveysList: function () {
                    var deferred = $q.defer();
                    var start = new Date().getTime();
                    var dataCache = CacheFactory.get('defaultCache');

                    if (dataCache.get("surveyList")) {
                        debugger;
                        deferred.resolve(dataCache.get("surveyList"));
                    } else {
                        debugger;//it's get here after refreshing.
                        $http.get("/compare/getsurveyslist").success(function (data) {
                            console.log('time taken for request: ' + (new Date().getTime() - start) + 'ms');
                            dataCache.put("surveyList", data);
                            deferred.resolve(data);
                        });
                    }
                    return deferred.promise;
                }
            };
        });
jmdobry commented 9 years ago

When you refresh the page, everything in memory is wiped. Use storageMode: "localStorage" to have stuff remain between page refreshes:

$http.defaults.cache = CacheFactory('defaultCache', {
  maxAge: 15 * 60 * 1000, // Items added to this cache expire after 15 minutes
  cacheFlushInterval: 60 * 60 * 1000, // This cache will clear itself every hour
  deleteOnExpire: 'aggressive' // Items will be deleted from this cache when they expire,
  storageMode: 'localStorage'
});

Closing because I’m not sure this is an issue, if you are convinced that this is really a bug, please feel free to re-open the issue and add more information:

Otherwise support is done via the mailing list.

Kroitoru commented 9 years ago

Thanks! that's work :+1: