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;

});
1

3 Answers 3

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;

});
Sign up to request clarification or add additional context in comments.

Comments

0

cacheManager is a Object and its not a function()

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

        cacheManager.add("key1", "value1");
        cacheManager.add("key2", "value2");

Comments

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");

1 Comment

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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.