Merge pull request #3 from dannycoates/memcached

Memcached kvstore backend
This commit is contained in:
Zach Carter 2013-05-15 16:11:48 -07:00
Родитель f33a6317a3 318d72621a
Коммит 3d71292099
4 изменённых файлов: 84 добавлений и 1 удалений

Просмотреть файл

@ -6,7 +6,7 @@ const fs = require('fs');
const url = require('url');
const convict = require('convict');
const AVAILABLE_BACKENDS = ["memory", "mysql"];
const AVAILABLE_BACKENDS = ["memory", "mysql", "memcached"];
var conf = module.exports = convict({
@ -36,6 +36,14 @@ var conf = module.exports = convict({
default: AVAILABLE_BACKENDS
}
},
memcached: {
hosts: {
default: '127.0.0.1:11211'
},
lifetime: {
default: 1000 * 60 * 2
}
},
mysql: {
user: {
default: 'root',

71
lib/kvstore/memcached.js Normal file
Просмотреть файл

@ -0,0 +1,71 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const Hapi = require('hapi');
const config = require('../config');
const kvstore = require('../kvstore');
const Memcached = require('memcached');
function MemcachedStore(options) {
this.client = new Memcached(options.hosts, options);
this.lifetime = options.lifetime;
}
MemcachedStore.connect = function connect(options, callback) {
options = Hapi.utils.merge(options, config.get('memcached'));
callback(null, new MemcachedStore(options));
};
MemcachedStore.prototype.get = function get(key, cb) {
this.client.gets(key,
function (err, result) {
if (err) { cb(err); }
else if (!result) { cb(null, null); }
else {
cb(null,
{
value: result[key],
casid: +(result.cas)
}
);
}
}
);
};
MemcachedStore.prototype.set = function set(key, value, cb) {
this.client.set(key, value, this.lifetime,
function (err, result) {
if (err) { cb(err); }
else if (!result) { cb('NOT SET'); }
else { cb(null); }
}
);
};
MemcachedStore.prototype.cas = function cas(key, value, casid, cb) {
this.client.cas(key, value, casid, this.lifetime,
function (err, result) {
if (err) { cb(err); }
else if (!result) { cb(kvstore.ERROR_CAS_MISMATCH); }
else { cb(null); }
}
);
};
MemcachedStore.prototype.delete = function del(key, cb) {
this.client.del(
key,
function (err) {
cb(err);
}
);
};
MemcachedStore.prototype.close = function close(cb) {
this.client.end();
if (cb) cb();
};
module.exports = MemcachedStore;

Просмотреть файл

@ -31,6 +31,7 @@
"jshint": "0.9.1"
},
"optionalDependencies": {
"memcached": "0.2.2",
"mysql": "0.9.5"
}
}

Просмотреть файл

@ -30,5 +30,8 @@ server.ext(
}
);
//TODO throttle extension
//TODO toobusy extension
module.exports = server;