added memcached kvstore backend

This commit is contained in:
Danny Coates 2013-05-15 14:13:36 -07:00
Родитель d7a1621c84
Коммит c2d39b42e9
4 изменённых файлов: 94 добавлений и 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',

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

@ -0,0 +1,81 @@
/* 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');
var client;
var lifetime = 0;
function get(key, cb) {
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)
}
);
}
}
);
}
function set(key, value, cb) {
client.set(key, value, lifetime,
function (err, result) {
if (err) { cb(err); }
else if (!result) { cb('NOT SET'); }
else { cb(null); }
}
);
}
function cas(key, value, casid, cb) {
client.cas(key, value, casid, lifetime,
function (err, result) {
if (err) { cb(err); }
else if (!result) { cb(kvstore.ERROR_CAS_MISMATCH); }
else { cb(null); }
}
);
}
function del(key, cb) {
client.del(
key,
function (err) {
cb(err);
}
);
}
module.exports = {
connect: function (options, callback) {
if (!client) {
options = Hapi.utils.merge(options, config.get('memcached'));
client = new Memcached(options.hosts, options);
lifetime = options.lifetime;
}
var api = {
get: get,
set: set,
cas: cas,
delete: del
};
callback(null, api);
},
close: function (cb) {
if (client) {
client.end();
}
if (cb) { cb(); }
}
};

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

@ -17,6 +17,7 @@
"node": "0.10.x"
},
"dependencies": {
"memcached": "0.2.2",
"hapi": "1.1.0",
"compute-cluster": "0.0.7",
"jwcrypto": "0.4.3",

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

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