jpodwys / cache-service

A tiered caching solution for JavaScript.
MIT License
12 stars 6 forks source link

When this.db is set to false in cache modules, an error occurs in functions #10

Closed marybeshaw closed 9 years ago

marybeshaw commented 9 years ago

This is an issue I found with my unit tests, where the redis cache isn't used. It doesn't matter if the cache is set up properly, but the behavior is odd when it's not set up properly (which can happen when setting up redis initially).

I am finding that, in the redis cache, if the client isn't created and the function mget() is called, I get a runtime error: TypeError: Object false has no method 'mget'. I've explained the issue below, and suggested a couple of solutions that would make the modules a bit more robust. Do you agree with the proposed solution(s)? Would you like me to implement the fixes?

An example of what can happen:

  1. In the init() function, this.db is set to false and a log message is created
  2. When calling mget(), a function is getting called on the object (which is "false"): this.db.mget()
  3. Runtime error occurs because this.db is false and has no function mget()

Possible solutions:

  1. Add a public function to the cache module which returns true or false whether the cache module was initialized correctly.
  2. Internally, when cache module functions are called, ensure the cache was actually initialized correctly before trying to call methods on that value.

Have a great day! Mary

jpodwys commented 9 years ago

One of the solutions you proposed is to provide a public function that tells an implementer whether the cache was successfully initialized. You already have access to this data like so:

if(cache.db){
  //Proceed
}

So, whether you're using redisCacheModule as a standalone cache or instantiating it and then injecting it into a cacheService instance, just check the .db property first.

marybeshaw commented 9 years ago

Yes, that will work great! Thanks!