-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add namespace support #67
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,6 +19,23 @@ function validateKey(key, operation) { | |||||
misc.assert(key.length < 250, 'Key must be less than 250 characters long'); | ||||||
} | ||||||
|
||||||
function validateNamespaceKey(key, operation) { | ||||||
validateKey(key, operation); | ||||||
misc.assert(key.length < 230, 'Key must be less than 230 characters long when using namespaces'); | ||||||
} | ||||||
|
||||||
function keyWithPrefix(key, prefix) { | ||||||
if (_.isUndefined(prefix)) { | ||||||
// only prefix was supplied | ||||||
// return a curried funtion that expects the key | ||||||
prefix = key; | ||||||
return function(key) { | ||||||
return _.toString(prefix) + '_' + key; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's please keep with the colon as the separator for namespaces as suggested by the Memcached article you linked That's also the way I've always seen it done in production |
||||||
}; | ||||||
} | ||||||
return _.toString(prefix) + '_' + key; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Constructor - Initiate client | ||||||
*/ | ||||||
|
@@ -324,14 +341,26 @@ Client.prototype.deleteMulti = function(keys, cb) { | |||||
* @returns {Promise} | ||||||
*/ | ||||||
Client.prototype.set = function(key, val, ttl, cb) { | ||||||
validateKey(key, 'set'); | ||||||
|
||||||
var command = 'set'; | ||||||
var self = this; | ||||||
if (typeof ttl === 'function') { | ||||||
cb = ttl; | ||||||
ttl = 0; | ||||||
} | ||||||
|
||||||
if (_.isPlainObject(ttl) && _.isString(ttl.namespace)) { | ||||||
var namespace = ttl.namespace; | ||||||
validateNamespaceKey(key, command); | ||||||
|
||||||
return this.getNamespacePrefix(namespace).then(function (prefix) { | ||||||
key = keyWithPrefix(key, prefix); | ||||||
return self.run(command, [key, val, ttl], cb); | ||||||
}); | ||||||
} | ||||||
|
||||||
return this.run('set', [key, val, ttl], cb); | ||||||
validateKey(key, command); | ||||||
|
||||||
return this.run(command, [key, val, ttl], cb); | ||||||
}; | ||||||
|
||||||
/** | ||||||
|
@@ -384,16 +413,34 @@ Client.prototype.gets = function(key, opts, cb) { | |||||
* @returns {Promise} | ||||||
*/ | ||||||
Client.prototype.get = function(key, opts, cb) { | ||||||
var command = 'get'; | ||||||
var self = this; | ||||||
if (typeof opts === 'function' && typeof cb === 'undefined') { | ||||||
cb = opts; | ||||||
opts = {}; | ||||||
} | ||||||
|
||||||
if (_.isPlainObject(opts) && _.isString(opts.namespace)) { | ||||||
var namespace = opts.namespace; | ||||||
validateNamespaceKey(key, command); | ||||||
|
||||||
return this.getNamespacePrefix(namespace).then(function (prefix) { | ||||||
|
||||||
if(_.isArray(key)) { | ||||||
key = _.map(key, keyWithPrefix(prefix)); | ||||||
return this.getMulti(key, opts, cb); | ||||||
} else { | ||||||
key = keyWithPrefix(key, prefix); | ||||||
return self.run(command, [key, opts], cb); | ||||||
} | ||||||
}); | ||||||
} | ||||||
|
||||||
if (_.isArray(key)) { | ||||||
return this.getMulti(key, opts, cb); | ||||||
} else { | ||||||
validateKey(key, 'get'); | ||||||
return this.run('get', [key, opts], cb); | ||||||
validateKey(key, command); | ||||||
return this.run(command, [key, opts], cb); | ||||||
} | ||||||
}; | ||||||
|
||||||
|
@@ -584,6 +631,47 @@ Client.prototype.cachedump = function(slabsId, limit, cb) { | |||||
return this.run('stats cachedump', [slabsId, limit], cb); | ||||||
}; | ||||||
|
||||||
/** | ||||||
* getNamespacePrefix() - Get prefix value for the provided namespace | ||||||
* | ||||||
* @param {String} namespace - The namespace for which prefix is to be fetched | ||||||
* @param {Function} [cb] - The (optional) callback called on completion | ||||||
* @returns {Promise} | ||||||
*/ | ||||||
Client.prototype.getNamespacePrefix = function(namespace, cb) { | ||||||
validateKey(namespace); | ||||||
var self = this; | ||||||
var timestamp = _.toNumber(new Date()); | ||||||
var prefix = this.get(namespace); | ||||||
|
||||||
return prefix.then(function (value) { | ||||||
if (!value) { | ||||||
// the namespace is not set | ||||||
return self.add(namespace, timestamp); | ||||||
} | ||||||
return value; | ||||||
}).then(function (value) { | ||||||
// if value is undefined it means we just | ||||||
// added the namespace to cache. | ||||||
return value || timestamp; | ||||||
}).nodeify(cb); | ||||||
}; | ||||||
|
||||||
|
||||||
/** | ||||||
* invalidateNamespace() - Invalidate all data for a namespace. | ||||||
* Warning! This does not flush the cache, but instead | ||||||
* relies on the unused values to expire on their own | ||||||
* | ||||||
* @param {String} namespace - The namespace to invalidate | ||||||
* @param {Function} [cb] - The (optional) callback called on completion | ||||||
* @returns {Promise} | ||||||
*/ | ||||||
Client.prototype.invalidateNamespace = function(namespace, cb) { | ||||||
validateKey(namespace); | ||||||
return this.incr(namespace, cb); | ||||||
}; | ||||||
|
||||||
/** | ||||||
* version() - Get current Memcached version from the server | ||||||
* @param {Function} [cb] - The (optional) callback called on completion | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it's good that you're checking for the namespace key to be shorter, but this without a check on the length of namespace itself isn't super useful. There is nothing preventing me from having a
key
that is 229 characters and anamespace
that is also 229 characters and being far past the 250 key limit set by Memcached core.I'd much prefer to either
validateKey()
after appending the namespace with the keyI favor 2. because it doesn't establish an arbitrary limit on namespace (you've chosen 20 and that seems rather arbitrary).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I wrote in my other comment, I think the idea of this feature was misunderstood.
It doesn't matter what the length of the name of the namespace is. The prefix is always going to be a unix timestamp which is around 13 characters. The key which uses namespace needs to be at most (250-13) characters long.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great works for adding the name spacing feature!
https://www.npmjs.com/package/memcached-lite has a similar feature named
keyPrefix
.I use it with some prefix string longer than 20 characters.
In my case, the prefix would include some from below:
require("./package.json").name
- this works when a memcached server is shared by multiple apps.require("./package.json").version
- this works when updating the app which has a difference schema than before__filename.split("/").pop()
- this works to separate namespaces for each files.+new Date()
as @royshouvik mentioned