memcachier / memjs

A memcache client for node using the binary protocol and SASL authentication
MIT License
197 stars 52 forks source link

Feature: Key wrapper #132

Open vanatd opened 4 years ago

vanatd commented 4 years ago

Hi guys, i'm thinking about feature to add to memjs, but i want to discuss it with you.

For example, i have several clients for different cases, but they work with one memcached server (not server.js) and i want their keys be unique. Or i'm working with one version api then i switch to new version and don't want change previous data in case if i need to go back to old version of API.

So, my proposal to add some kind of keyWrapperFn to options and provide a possibility to change keys centralized for all methods and not to create some kind of Proxy etc.

// .....

var noopKeyWrapper = function(key){ return key; }

var Client = function(servers, options) {
  this.servers = servers;
  this.seq = 0;
  this.options = merge(options || {},
    {failoverTime: 60, retries: 2, retry_delay: 0.2, expires: 0, logger: console});

  this.keyWrapper = this.options.keyWrapper || noopKeyWrapper; 
  this.serializer = this.options.serializer || noopSerializer;
};

// .....

Client.prototype.get = function(key, callback) {
  // .....

  var request = makeRequestBuffer(0, this.keyWrapper(key), '', '', this.seq);

  // .....
}

Client.prototype.set = function(key, value, options, callback) {
  // .....
  var request = makeRequestBuffer(opcode, this.keyWrapper(key), serialized.extras, serialized.value, this.seq);
  // .....
}

@alevy @saschat What do you think about it? Should I create PR for it or just deal with it in my code?