Is CacheFactory Angular Single?

I created a service using CacheFactory. I expected it to be singleton. I inject it into the controller and it works fine within the controller. But as soon as I go to another page with a different scope, I don't seem to have any cache values ​​that I saved in one controller in another area. Should the CacheFactory behavior be single, where I have the same cached objects, where I insert the CacheService?

This is my example service:

angular.module('MyService', []).factory('CacheService', function($cacheFactory) {
        return $cacheFactory('cacheService', {
            capacity: 3 // optional - turns the cache into LRU cache
        })
    });

Then in my controller:

function MyController($scope, CacheService) {
   var results= CacheService.get('storedvalue');
   if(!results){
       CacheService.put('storedvalue', results);
      alert('results not stored');
   }
   else
      alert('results stored');
}
+5
source share
1 answer

$cacheFactory , , - factory, singleton, . , , . , , . :

.factory('CacheService', function($cacheFactory) {
   var cache = $cacheFactory('cacheService', {
     capacity: 3 // optional - turns the cache into LRU cache
   });

   return cache;
});

CacheService - singleton, , $cacheFactory, , . , , .

Plunker: http://plnkr.co/edit/loKWGms1lMCnmiWa1QA7?p=preview


- , , , Plunker, , .

+16

All Articles