Comparing version 0.0.10 to 0.0.11
@@ -14,3 +14,3 @@ var Q = require('kew') | ||
this._priorities[name] = priority | ||
this._sortClusters() | ||
@@ -29,3 +29,5 @@ } | ||
var defer = Q.defer() | ||
nextClient.get(key, defer.makeNodeResolver()) | ||
nextClient.get(key, function (response) { | ||
defer.resolve(response.body ? response.body.toString('utf8') : undefined) | ||
}) | ||
@@ -66,2 +68,3 @@ // no promise currently exists, return the first in the chain | ||
console.log("Getting from", keyClients.length) | ||
for (j = 0; j < keyClients.length; j++) { | ||
@@ -84,3 +87,3 @@ // for every client available to this | ||
CacheManager.prototype.set = function (key, val, lifetime) { | ||
CacheManager.prototype.set = function (key, val, lifetimeMs) { | ||
if (!this._sortedClusters.length) return Q.resolve(true) | ||
@@ -91,8 +94,12 @@ var clients = this._getAllClientsForKeys([key])[key] | ||
for (i = 0; i < clients.length; i++) { | ||
var defer = Q.defer() | ||
clients[i].set(key, val, lifetime || 0, defer.makeNodeResolver()) | ||
try { | ||
var defer = Q.defer() | ||
clients[i].set(key, val, 0x01, Math.floor(lifetimeMs ? lifetimeMs / 1000 : 86400), function (defer, response) { | ||
defer.resolve(true) | ||
}.bind(null, defer)) | ||
promises.push(defer.promise) | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
promises.push(defer.promise.fail(function (e) { | ||
return true | ||
})) | ||
} | ||
@@ -127,12 +134,16 @@ | ||
var promises = [] | ||
var rand = Math.floor(Math.random() * 1000) | ||
console.log("Deleting from", clients.length) | ||
for (i = 0; i < clients.length; i++) { | ||
var defer = Q.defer() | ||
clients[i].delete(key, defer.makeNodeResolver()) | ||
promises.push(defer.promise.fail(function () { | ||
return true | ||
})) | ||
clients[i].delete(key, function (defer, response) { | ||
console.log("done deleting 1") | ||
defer.resolve(true) | ||
}.bind(null, defer)) | ||
promises.push(defer.promise) | ||
} | ||
return Q.all(promises).then(function () { | ||
console.log("done deleting all") | ||
return true | ||
@@ -139,0 +150,0 @@ }) |
var events = require('events') | ||
var Memcached = require('memcached') | ||
var memc = require('node-memcache-parser-obvfork').client | ||
var util = require('util') | ||
@@ -9,33 +9,30 @@ | ||
this._uri = uri | ||
this._status = common.SERVER_STATUS.DISCONNECTED | ||
var uriParts = this._uri.split(':') | ||
clientCtor = clientCtor || Memcached | ||
this._client = new clientCtor(this._uri, { | ||
timeout: 20, | ||
retries: 5, | ||
retry: 10, | ||
remove: true, | ||
poolSize: 50 | ||
}) | ||
this._host = uriParts[0] | ||
this._port = uriParts[1] | ||
if (clientCtor) { | ||
this._client = new clientCtor(this._port, this._host) | ||
} else { | ||
this._client = new memc.Connection() | ||
} | ||
this._currentCapacity = 0 | ||
this._targetCapacity = 0 | ||
this._status = common.SERVER_STATUS.DISCONNECTED | ||
this._connectionTimer = null | ||
this._connectionIntervalMs = 0 | ||
this._capacityTimer = null | ||
this.checkStatus() | ||
this._statusInterval = setInterval(this.checkStatus.bind(this), 5000) | ||
process.nextTick(this.checkStatus.bind(this)) | ||
this._bound_onConnect = this._onConnect.bind(this) | ||
this._client.on('close', this._bound_onClose = this._onClose.bind(this)) | ||
this._client.on('error', this._bound_onError = this._onError.bind(this)) | ||
this._scheduleConnect() | ||
} | ||
util.inherits(CacheServer, events.EventEmitter) | ||
CacheServer.prototype.checkStatus = function () { | ||
if (this._status === common.SERVER_STATUS.DESTROYED) return | ||
var self = this | ||
this._client.version(function (err, data) { | ||
self._status = err ? common.SERVER_STATUS.DISCONNECTED : common.SERVER_STATUS.CONNECTED | ||
}) | ||
} | ||
/** | ||
@@ -45,7 +42,8 @@ * Shut down this server client | ||
CacheServer.prototype.close = function () { | ||
if (this._statusInterval) clearInterval(this._statusInterval) | ||
console.log("closing") | ||
this._client.removeListener('close', this._bound_onClose) | ||
this._client.removeListener('error', this._bound_onError) | ||
this._status = common.SERVER_STATUS.DESTROYED | ||
this._client.end() | ||
this._client.quit(function () {}) | ||
} | ||
@@ -120,2 +118,59 @@ | ||
/** | ||
* Schedule the next connection attempt for a memcache client | ||
* | ||
* @param {string} host colon-delimited host with port | ||
*/ | ||
CacheServer.prototype._scheduleConnect = function () { | ||
if (this._connectionTimer || this._status == common.SERVER_STATUS.DESTROYED) return | ||
var attemptMs | ||
var self = this | ||
if (!this._connectionIntervalMs) { | ||
attemptMs = 1 | ||
this._connectionIntervalMs = common.MIN_BACKOFF | ||
} else { | ||
attemptMs = this._connectionIntervalMs | ||
if (this._connectionIntervalMs < common.MAX_BACKOFF) { | ||
this._connectionIntervalMs *= common.BACKOFF_MULTIPLIER | ||
if (this._connectionIntervalMs > common.MAX_BACKOFF) this._connectionIntervalMs = common.MAX_BACKOFF | ||
} | ||
} | ||
this.emit('connectionAttempt', attemptMs) | ||
self._connectionTimer = setTimeout(function () { | ||
delete self._connectionTimer | ||
self._client.connect(self._port, self._host, self._bound_onConnect) | ||
}, attemptMs) | ||
} | ||
/** | ||
* Handle an erroring client | ||
*/ | ||
CacheServer.prototype._onError = function (e) { | ||
this.emit('error', e) | ||
this._scheduleConnect() | ||
} | ||
/** | ||
* Handle a closed client | ||
*/ | ||
CacheServer.prototype._onClose = function () { | ||
console.log("closed") | ||
this.emit('close') | ||
this._status = common.SERVER_STATUS.DISCONNECTED | ||
this._scheduleConnect() | ||
} | ||
/** | ||
* Handle a connected client | ||
*/ | ||
CacheServer.prototype._onConnect = function () { | ||
console.log("connected") | ||
this._connectionIntervalMs = 0 | ||
this._status = common.SERVER_STATUS.CONNECTED | ||
this.emit('connect') | ||
} | ||
module.exports = CacheServer |
@@ -13,7 +13,7 @@ var events = require('events') | ||
FakeMemcache.prototype.connect = function () { | ||
this.emit('connect') | ||
FakeMemcache.prototype.connect = function (host, port, callback) { | ||
callback() | ||
} | ||
FakeMemcache.prototype.end = function () { | ||
FakeMemcache.prototype.quit = function () { | ||
this.emit('close') | ||
@@ -30,17 +30,9 @@ } | ||
FakeMemcache.prototype.version = function (callback) { | ||
callback(null, {}) | ||
FakeMemcache.prototype.set = function (key, val, flags, lifetimeSeconds, callback) { | ||
this._data[key] = new Buffer(String(val), 'utf8') | ||
callback(true) | ||
} | ||
FakeMemcache.prototype.set = function (key, val, lifetimeMs, callback) { | ||
this._data[key] = val | ||
callback(null, true) | ||
} | ||
FakeMemcache.prototype.get = function (key, callback) { | ||
if (this._data[key]) { | ||
callback(null, this._data[key]) | ||
} else { | ||
callback(null, undefined) | ||
} | ||
callback(this._data[key] ? {body: this._data[key]} : {}) | ||
} | ||
@@ -51,5 +43,5 @@ | ||
if (callback) callback(null, true) | ||
if (callback) callback(true) | ||
} | ||
module.exports = FakeMemcache |
{ | ||
"name": "zcache" | ||
, "description": "AWS zone-aware caching" | ||
, "version": "0.0.10" | ||
, "version": "0.0.11" | ||
, "homepage": "https://github.com/azulus/zcache" | ||
@@ -19,3 +19,3 @@ , "authors": [ | ||
, "dependencies": { | ||
"memcached": "*" | ||
"node-memcache-parser-obvfork": "0.1.1" | ||
} | ||
@@ -22,0 +22,0 @@ , "devDependencies": { |
@@ -32,5 +32,5 @@ var CacheCluster = require('../lib/CacheCluster') | ||
defer.promise.then(function (data) { | ||
var currentVal = 0 | ||
var currentVal = '0' | ||
var setVal = function () { | ||
currentVal++ | ||
currentVal = String(parseInt(currentVal, 10) + 1) | ||
//console.log('setting', currentVal) | ||
@@ -49,3 +49,3 @@ return cacheManager.set('testKey', currentVal) | ||
test.equal(val, currentVal, "Val should be correct") | ||
if (currentVal <= 1000) { | ||
if (parseInt(currentVal, 10) <= 2) { | ||
var defer = Q.defer() | ||
@@ -52,0 +52,0 @@ process.nextTick(function () { |
@@ -14,3 +14,3 @@ var CacheCluster = require('../lib/CacheCluster') | ||
var primaryCluster = new CacheCluster({ | ||
clientCtor: FakeMemcache | ||
//clientCtor: FakeMemcache | ||
}) | ||
@@ -22,3 +22,3 @@ primaryCluster.setServerCapacity('localhost:11212', 100) | ||
var secondaryCluster = new CacheCluster({ | ||
clientCtor: FakeMemcache | ||
//clientCtor: FakeMemcache | ||
}) | ||
@@ -29,16 +29,18 @@ secondaryCluster.setServerCapacity('localhost:11214', 100) | ||
cacheManager.set("a", "123") | ||
.then(function () { | ||
return cacheManager.get(undefined) | ||
}) | ||
.then(function (data) { | ||
test.equal(data, undefined, "Response should be undefined") | ||
return cacheManager.mget([undefined, null, "a"]) | ||
}) | ||
.then(function (data) { | ||
test.equal(data[0], undefined, "Response[0] should be undefined") | ||
test.equal(data[1], undefined, "Response[1] should be undefined") | ||
test.equal(data[2], "123", "Response[2] should be 123") | ||
test.done() | ||
}) | ||
setTimeout(function () { | ||
cacheManager.set("a", "123") | ||
.then(function () { | ||
return cacheManager.get(undefined) | ||
}) | ||
.then(function (data) { | ||
test.equal(data, undefined, "Response should be undefined") | ||
return cacheManager.mget([undefined, null, "a"]) | ||
}) | ||
.then(function (data) { | ||
test.equal(data[0], undefined, "Response[0] should be undefined") | ||
test.equal(data[1], undefined, "Response[1] should be undefined") | ||
test.equal(data[2], "123", "Response[2] should be 123") | ||
test.done() | ||
}) | ||
}, 500) | ||
} | ||
@@ -50,3 +52,3 @@ | ||
var primaryCluster = new CacheCluster({ | ||
clientCtor: FakeMemcache | ||
//clientCtor: FakeMemcache | ||
}) | ||
@@ -58,3 +60,3 @@ primaryCluster.setServerCapacity('localhost:11212', 100) | ||
var secondaryCluster = new CacheCluster({ | ||
clientCtor: FakeMemcache | ||
//clientCtor: FakeMemcache | ||
}) | ||
@@ -100,2 +102,3 @@ secondaryCluster.setServerCapacity('localhost:11214', 100) | ||
.then(function (data) { | ||
console.log("deleted", data) | ||
test.equal(data, undefined, "Should get undefined") | ||
@@ -102,0 +105,0 @@ |
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
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
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
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
47614
888
1
1
+ Addednode-memcache-parser-obvfork@0.1.1(transitive)
- Removedmemcached@*
- Removedconnection-parse@0.0.7(transitive)
- Removedhashring@3.2.0(transitive)
- Removedjackpot@0.0.6(transitive)
- Removedmemcached@2.2.2(transitive)
- Removedretry@0.6.0(transitive)
- Removedsimple-lru-cache@0.0.2(transitive)