0

In this plunk I have a factory that wraps Angular's $cacheFactory. The controller uses the factory to create two independent caches, however I get the following error: cacheManager is not a function. What's wrong with this code?

HTML

<body ng-app="app" ng-controller="myCtl">
  cache1 : {{cache1 | json}}
  <br/>
  cache2 : {{cache2 | json}}
</body>

Javascript:

var app = angular.module('app',[]);    
app.controller('myCtl', function ($scope,cacheManager) {

    var cache1 = new cacheManager();
    var cache2 = new cacheManager();

    cache1.create("cache1");
    cache2.create("cache2");

    cache1.add("key1", "value1");
    cache2.add("key2", "value2");
})

.factory('cacheManager',function($cacheFactory){

    var factory = {};
    var cache = null;

    factory.create = function(cacheId){  
        cache = $cacheFactory(cacheId); 
        return cache;
    };

    factory.add = function(key,value){
        if (!cache)
            return;
        cache.put(key, value);
    }

    factory.get = function(key){
        if (!cache)
            return;
        return cache.get(key);
    }

    return factory;

});
ps0604
  • 1,227
  • 23
  • 133
  • 330

3 Answers3

0

This is the code that did the trick, the factory needs to return a function to allow the controller do a new. Also, the methods need to be defined with the prototype keyword:

var app = angular.module('app',[]);

app.controller('myCtl', function ($scope,cacheManager) {

    var cache1 = new cacheManager();
    var cache2 = new cacheManager();

    cache1.create("cache1");
    cache2.create("cache2");

    cache1.add("key1", "value1");
    cache2.add("key2", "value2");

    alert(cache1.get("key1"));


})

.factory('cacheManager',function($cacheFactory){

    var factory = function() { };

    factory.cache = null;

    factory.prototype.create = function(cacheId){  
        cache = $cacheFactory(cacheId); 
        return cache;
    };

    factory.prototype.add = function(key,value){
        if (!cache)
            return;
        cache.put(key, value);
    }

    factory.prototype.get = function(key){
        if (!cache)
            return;
        return cache.get(key);
    }

    return factory;

});
ps0604
  • 1,227
  • 23
  • 133
  • 330
0

cacheManager is a Object and its not a function()

        cacheManager.create("cache1");
        cacheManager.create("cache2");

        cacheManager.add("key1", "value1");
        cacheManager.add("key2", "value2");
Michal Kucaj
  • 681
  • 5
  • 15
0

I think you should not create multiple instances of cacheManager instead you should create multiple cache objects and add multiple key/value to the different cache objects.

var cache1 = cacheManager.create("cache1");
    var cache2 = cacheManager.create("cache2");

cache1.add("key1", "value1"); cache2.add("key2", "value2");

Nitin Garg
  • 888
  • 6
  • 7
  • you can add multiple key/value on cache1 as well as cache2. like cache1.add('key11','11');cache1.add('key22','22');cache1.add('key33','33'); – Nitin Garg Jan 29 '16 at 15:31