Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 93 additions & 5 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link
Owner

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 a namespace that is also 229 characters and being far past the 250 key limit set by Memcached core.

I'd much prefer to either

  1. Add a limit on the length of a namespace as well (here at 20 characters)
  2. Just run validateKey() after appending the namespace with the key

I favor 2. because it doesn't establish an arbitrary limit on namespace (you've chosen 20 and that seems rather arbitrary).

Copy link
Author

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.

Copy link

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

}

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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return _.toString(prefix) + '_' + key;
return _.toString(prefix) + ':' + key;

Copy link
Owner

Choose a reason for hiding this comment

The 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
*/
Expand Down Expand Up @@ -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);
};

/**
Expand Down Expand Up @@ -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);
}
};

Expand Down Expand Up @@ -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
Expand Down
71 changes: 71 additions & 0 deletions test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,20 @@ describe('Client', function() {
});
});

describe('with namespace', function () {
it('should work', function () {
var key = getKey(), ns = getKey(), val = chance.word();

return cache.set(key, val, { namespace: ns })
.then(function () {
return cache.get(key, { namespace: ns });
})
.then(function (v) {
val.should.equal(v);
});
});
});

it('does not throw an error when setting a value number', function() {
var key = chance.guid(), val = chance.natural();

Expand Down Expand Up @@ -1277,6 +1291,63 @@ describe('Client', function() {
});
});


describe('namespace', function () {
var cache;
var savedPrefix;
var namespace = getKey();
beforeEach(function () {
cache = new Client();
});


describe('getNamespacePrefix', function () {
it('exists', function () {
return cache.should.have.property('getNamespacePrefix');
});

it('when namespace is not already set', function () {
return cache.getNamespacePrefix(namespace).then(function(prefix) {
expect(prefix).to.be.a('number');
savedPrefix = prefix;
});
});

it('when namespace is already set', function () {
return cache.getNamespacePrefix(namespace).then(function(prefix) {
expect(prefix).to.equal(savedPrefix);
});
});
});

describe('invalidateNamespace', function () {
it('exists', function () {
return cache.should.have.property('invalidateNamespace');
});

it('should invalidate previously stored keys', function () {
var key = getKey(), ns = getKey(), val = chance.word();

return cache.set(key, val, { namespace: ns })
.then(function () {
return cache.get(key, { namespace: ns });
})
.then(function (v) {
val.should.equal(v);
})
.then(function() {
return cache.invalidateNamespace(ns);
})
.then(function () {
return cache.get(key, { namespace: ns });
})
.then(function (v) {
expect(v).to.be.null;
});
});
});
});

describe('version', function () {
var cache;
beforeEach(function() {
Expand Down