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

Can't get a previously created Cache #256

Closed guilhermemaranhao closed 7 years ago

guilhermemaranhao commented 7 years ago

Hi everyone,

In the main.js, I'm setting the default parameters to my cache as follows:

.config(function (CacheFactoryProvider) {
            angular.extend(CacheFactoryProvider.defaults, {
                storageMode: 'localStorage',
                deleteOnExpire: 'aggressive',
                onExpire: function (key, value) {
                    console.log(key + ' expirado');
                }
            });
        })

After that, the run function is declared as follows:

.run(['$rootScope', 'CacheFactory', 'MyResource', function($rootScope, CacheFactory, MyResource){

$rootScope.$on('listLoaded', addListToCache);

            retrieveListFromServer = function(category_id) {
                MyResource.get({category_id: category_id}, function (success_response) {

                    $rootScope.$emit('listLoaded', [category_id, success_response]);

                }, function (error_response) {
                    if (error_response.data) {
                        Notification.warning(error_response.data.message);
                    } else {
                        Notification.warning('Error');
                    }
                });
            }

            if (!CacheFactory.get('lists')){
                retrieveListFromServer(1);
            }

            function addListToCache (event, args){

                var category_id = args[0];

                switch(category_id)
                {
                    case 1:
                        var cacheList = CacheFactory.createCache('lists');
                        cacheList.put('listCategory1Cache', args[1]);
                        break;
                }
            }
        }])

The browser application tab has the following local storage keys:


key --------------- angular-cache.caches.lists.data.listCategory1Cache {"key":"listCategory1Cache","value":[{all_my_list}]
angular-cache.caches.lists.keys -------------- ["listCategory1Cache"]

How am I supposed to get my previously cache created? They are all properly loaded, but anything is being returned when I invoke: if (!CacheFactory.get('lists')){

Or

if (!CacheFactory.get('listCategory1Cache')){

Am I missing something?

Can anybody give me a hint?

Thanks,

Guilherme

jmdobry commented 7 years ago

Your app needs to create the cache every time it runs, at which point any previously cached data will be loaded. See https://github.com/jmdobry/angular-cache#cachefactorygetcacheid

guilhermemaranhao commented 7 years ago

Thank you, @jmdobry ! I really missed that.