merlin

object-oriented MVC framework for node.js

cacheClient

classes/other/cacheClient.js

function cacheClient() { this.fullConstruct() }

cacheClient.parents = ['merlinObject']

cacheClient.construct = function() { var myself = this with ({myself: myself, x: 0}) {

    myself.merlin().switchboard.on('clearAllCaches', function() {
        myself.clearAllCaches()
      })
    
      myself.merlin().switchboard.on('clearCacheType', function(type) {
        myself.clearCacheType(type)
      })
    
      myself.merlin().switchboard.on('clearCache', function(which) {
        myself.clearCache(which)
      })

} return this } cacheClient.bootstrap = function() { return this }

cacheClient.methods = { getCache: function(key, callback) { cacheClient.redisClient = cacheClient.redisClient || require('redis').createClient() cacheClient.redisClient.hgetall('merlin:cacheClient', function(err, redisCache) { if (typeof redisCache[key] == 'undefined' && !err) err = 'cache not found'

    if (!err) {
      var cacheResponse = {
        data: redisCache[key],
        save: function() {
          if (typeof this.data == 'string' || this.data instanceof String)
            theData = this.data
          else
            theData = JSON.stringify(this.data)

          cacheClient.redisClient.hset('merlin:cacheClient', key, theData)
        }
      }
    }
    callback(err, cacheResponse)
  })
},

ensureCache: function(key, setCallback, retrievedCallback) {
  cacheClient.redisClient = cacheClient.redisClient || require('redis').createClient()
  var outerArgs = arguments
  cacheClient.redisClient.hgetall('merlin:cacheClient', function(err, cache) {
    if (err) {
      retrievedCallback(err)
      return
    }

    if (typeof cache[key] == 'undefined') {
      var saveCache = function(err, updatedCache) {
        if (!err) {
          if (typeof updatedCache == 'object')
            updatedCache = JSON.stringify(updatedCache)

          cacheClient.redisClient.hset('merlin:cacheClient', key, updatedCache)
        }
        retrievedCallback(err, updatedCache)
      }

      var args = Array.prototype.slice.apply(outerArgs).slice(3)
      args.unshift(saveCache)
      setCallback.apply(this, args)
    }
    else {
      retrievedCallback(null, cache[key])
    }
  })
},

clearAllCaches: function() {
  cacheClient.redisClient = cacheClient.redisClient || require('redis').createClient()
  cacheClient.redisClient.del('merlin:cacheClient')
},

clearCacheByType: function(type) {
  // to be handled by subclasses
},

clearCacheByKey: function(key) {
  cacheClient.redisClient = cacheClient.redisClient || require('redis').createClient()
  cacheClient.redisClient.hdel('merlin:cacheClient', key)
}

}

module.exports = cacheClient

merlinObject

classes/other/merlinObject.js

function merlinObject() { this.fullConstruct() }

merlinObject.parents = []

merlinObject.bootstrap = function() { return this } merlinObject.construct = function() { return this }

merlinObject.methods = { superConstruct: function() { for (k in this.parentInitializers) { this.parentInitializers[k].call(this) } return this },

fullConstruct: function() {
  this.superConstruct()
  this.constructor.construct.apply(this, arguments)
  return this
}
merlin: function() {
      return this.constructor.merlin
    }

}

module.exports = merlinObject