Comparing version 0.0.1 to 0.0.2
var CacheServer = require('./CacheServer') | ||
var ConsistentHasher = require('./ConsistentHasher') | ||
var Q = require('kew') | ||
@@ -10,2 +11,3 @@ var common = require('./common') | ||
this._clientsPerKey = options.clientsPerKey || 3 | ||
this._clientCtor = options.clientCtor | ||
this._servers = {} | ||
@@ -24,3 +26,3 @@ this._hasher = new ConsistentHasher | ||
if (!this._servers[uri]) { | ||
this._servers[uri] = new CacheServer(uri) | ||
this._servers[uri] = new CacheServer(this._clientCtor, uri) | ||
this._servers[uri].on('capacity', this._onCapacity.bind(this, uri)) | ||
@@ -60,2 +62,29 @@ this._servers[uri].on('error', function () {}) | ||
CacheCluster.prototype.getStats = function (key) { | ||
var self = this | ||
var stats = {} | ||
var promises = [] | ||
for (var uri in this._servers) { | ||
if (this._servers[uri].getStatus() === common.SERVER_STATUS.CONNECTED) { | ||
(function (uri) { | ||
var defer = Q.defer() | ||
self._servers[uri].getClient().stats(key, defer.makeNodeResolver()) | ||
promises.push(defer.promise.then(function (data) { | ||
stats[uri] = data | ||
})) | ||
})(uri) | ||
} else { | ||
stats[uri] = { | ||
status: "disconnected" | ||
} | ||
} | ||
} | ||
return Q.all(promises) | ||
.then(function () { | ||
return stats | ||
}) | ||
} | ||
/** | ||
@@ -62,0 +91,0 @@ * Retrieve a map of servers to their capacities |
@@ -19,2 +19,3 @@ var Q = require('kew') | ||
CacheManager.prototype.get = function (key) { | ||
if (!this._sortedClusters.length) return Q.resolve(undefined) | ||
var clients = this._getClosestClientsForKeys([key])[key] | ||
@@ -42,2 +43,3 @@ if (!clients || !clients.length) return Q.resolve(undefined) | ||
CacheManager.prototype.set = function (key, val, lifetime) { | ||
if (!this._sortedClusters.length) return Q.resolve(true) | ||
var clients = this._getAllClientsForKeys([key])[key] | ||
@@ -57,3 +59,22 @@ var promises = [] | ||
CacheManager.prototype.stats = function (stats) { | ||
var allStats = {} | ||
var promises = [] | ||
for (var key in this._clusters) { | ||
promises.push(this._clusters[key].getStats(stats).then(function (data) { | ||
for (var key in data) { | ||
allStats[key] = data[key] | ||
} | ||
})) | ||
} | ||
return Q.all(promises) | ||
.then(function () { | ||
return allStats | ||
}) | ||
} | ||
CacheManager.prototype.del = function (key) { | ||
if (!this._sortedClusters.length) return Q.resolve(true) | ||
var clients = this._getAllClientsForKeys([key])[key] | ||
@@ -60,0 +81,0 @@ var promises = [] |
@@ -7,3 +7,3 @@ var events = require('events') | ||
function CacheServer(uri) { | ||
function CacheServer(clientCtor, uri) { | ||
this._uri = uri | ||
@@ -14,3 +14,4 @@ var uriParts = this._uri.split(':') | ||
this._port = uriParts[1] | ||
this._client = new memcache.Client(this._port, this._host) | ||
clientCtor = clientCtor || memcache.Client | ||
this._client = new clientCtor(this._port, this._host) | ||
@@ -17,0 +18,0 @@ this._currentCapacity = 0 |
{ | ||
"name": "zcache" | ||
, "description": "AWS zone-aware caching" | ||
, "version": "0.0.1" | ||
, "version": "0.0.2" | ||
, "homepage": "https://github.com/azulus/zcache" | ||
@@ -6,0 +6,0 @@ , "authors": [ |
var CacheCluster = require('../lib/CacheCluster') | ||
var CacheManager = require('../lib/CacheManager') | ||
var FakeMemcache = require('../lib/FakeMemcache') | ||
var Q = require('kew') | ||
@@ -11,8 +12,12 @@ | ||
var primaryCluster = new CacheCluster() | ||
primaryCluster.setServerCapacity('localhost:11212', 5) | ||
primaryCluster.setServerCapacity('localhost:11213', 5) | ||
var primaryCluster = new CacheCluster({ | ||
clientCtor: FakeMemcache | ||
}) | ||
primaryCluster.setServerCapacity('localhost:11212', 100) | ||
primaryCluster.setServerCapacity('localhost:11213', 100 ) | ||
cacheManager.addCluster('primary', primaryCluster, 1) | ||
var secondaryCluster = new CacheCluster() | ||
var secondaryCluster = new CacheCluster({ | ||
clientCtor: FakeMemcache | ||
}) | ||
secondaryCluster.setServerCapacity('localhost:11214', 100) | ||
@@ -22,56 +27,61 @@ secondaryCluster.setServerCapacity('localhost:11215', 100) | ||
var defer = Q.defer() | ||
setTimeout(function () { | ||
cacheManager.set("a", "123") | ||
.then(function (data) { | ||
console.log("SET", data) | ||
primaryCluster.setServerCapacity('localhost:11212', 0) | ||
primaryCluster.setServerCapacity('localhost:11213', 0) | ||
return cacheManager.get("a") | ||
}) | ||
.then(function (data) { | ||
console.log("GET", data) | ||
return cacheManager.set("a", "456") | ||
}) | ||
.then(function (data) { | ||
console.log("SET", data) | ||
return cacheManager.get("a") | ||
}) | ||
.then(function (data) { | ||
console.log("GET", data) | ||
return cacheManager.del("a") | ||
}) | ||
.then(function (data) { | ||
console.log("DEL", data) | ||
return cacheManager.get("a") | ||
}) | ||
.then(function (data) { | ||
console.log("GET", data) | ||
primaryCluster.setServerCapacity('localhost:11212', 5) | ||
primaryCluster.setServerCapacity('localhost:11213', 5) | ||
var defer = Q.defer() | ||
setTimeout(function () { | ||
defer.resolve(cacheManager.get("a")) | ||
}, 1000) | ||
return defer.promise | ||
}) | ||
.then(function (data) { | ||
console.log("GET", data) | ||
return cacheManager.del("a") | ||
}) | ||
.then(function (data) { | ||
console.log("DEL", data) | ||
return cacheManager.get("a") | ||
}) | ||
.then(function (data) { | ||
console.log("GET", data) | ||
}) | ||
.fail(function (e) { | ||
console.error("ERROR", e, e.stack) | ||
throw e | ||
}) | ||
}, 2000) | ||
defer.resolve(true) | ||
}, 100) | ||
setTimeout(function () { | ||
test.done() | ||
}, 300000) | ||
defer.promise | ||
.then(function () { | ||
// set a key with all servers in rotation | ||
return cacheManager.set("a", "123") | ||
}) | ||
.then(function (data) { | ||
test.equal(data, true, "Should set key") | ||
// drop the primaries out of rotation | ||
primaryCluster.setServerCapacity('localhost:11212', 0) | ||
primaryCluster.setServerCapacity('localhost:11213', 0) | ||
return cacheManager.get("a") | ||
}) | ||
.then(function (data) { | ||
test.equal(data, '123', "Should get key") | ||
return cacheManager.set("a", "456") | ||
}) | ||
.then(function (data) { | ||
test.equal(data, true, "Should set key again") | ||
return cacheManager.get("a") | ||
}) | ||
.then(function (data) { | ||
test.equal(data, '456', "Should get key again") | ||
return cacheManager.del("a") | ||
}) | ||
.then(function (data) { | ||
test.equal(data, true, "Should delete key") | ||
return cacheManager.get("a") | ||
}) | ||
.then(function (data) { | ||
test.equal(data, undefined, "Should get undefined") | ||
// put the primaries back into rotation | ||
primaryCluster.setServerCapacity('localhost:11212', 5) | ||
primaryCluster.setServerCapacity('localhost:11213', 5) | ||
var defer = Q.defer() | ||
setTimeout(function () { | ||
defer.resolve(cacheManager.get("a")) | ||
}, 100) | ||
return defer.promise | ||
}) | ||
.then(function (data) { | ||
test.equal(data, '123', "Should get original key") | ||
return cacheManager.del("a") | ||
}) | ||
.then(function (data) { | ||
test.equal(data, true, "Should delete key again") | ||
return cacheManager.get("a") | ||
}) | ||
.then(function (data) { | ||
test.equal(data, undefined, "Should get undefined again") | ||
test.done() | ||
}) | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42998
12
747
133