Skip to content

Commit

Permalink
Add close for redis adapter
Browse files Browse the repository at this point in the history
Upgrade dependencies
  • Loading branch information
taoyuan committed Dec 19, 2017
1 parent 7dfa694 commit 8912b5a
Show file tree
Hide file tree
Showing 17 changed files with 557 additions and 534 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
@@ -0,0 +1,13 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# http://editorconfig.org

root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
57 changes: 30 additions & 27 deletions examples/example.js
@@ -1,56 +1,59 @@
var kvs = require('kvs');

var redisStore = kvs.store('redis', { db: 1 });
var redisBucket = redisStore.createBucket({ ttl: 100/*seconds*/ });
var redisStore = kvs.store('redis', {db: 1});
var redisBucket = redisStore.createBucket({ttl: 100/*seconds*/});

var memoryStore = kvs.store('memory');
var memoryBucket = memoryStore.createBucket({ max: 100, ttl: 10/*seconds*/ });
var memoryBucket = memoryStore.createBucket({max: 100, ttl: 10/*seconds*/});


redisBucket.set('foo', 'bar', function(err) {
if (err) { throw err; }
redisBucket.set('foo', 'bar', function (err) {
if (err) {
throw err;
}

redisBucket.get('foo', function(err, result) {
console.log(result);
// >> 'bar'
redisBucket.del('foo', function(err) {});
redisBucket.get('foo', function (err, result) {
console.log(result);
// >> 'bar'
redisBucket.del('foo', function (err) {
});
});
});

function getUser(id, cb) {
setTimeout(function () {
console.log("Returning user from slow database.");
cb(null, {id: id, name: 'Bob'});
}, 100);
setTimeout(function () {
console.log("Returning user from slow database.");
cb(null, {id: id, name: 'Bob'});
}, 100);
}

var user_id = 123;
var key = '#' + user_id; // for test if key is not the parameter (user_id) to load.

// Using namespace "user"
var redisLoadBucket = redisStore.createBucket('user', {
ttl: 100, /*seconds*/

// method to load a thing if it's not in the bucket.
load: function (user_id, cb) {
// this method will only be called if it's not already in bucket, and will
// store the result in the bucket store.
getUser(user_id, cb);
}
ttl: 100, /*seconds*/

// method to load a thing if it's not in the bucket.
load: function (user_id, cb) {
// this method will only be called if it's not already in bucket, and will
// store the result in the bucket store.
getUser(user_id, cb);
}
});

// `user_id` is the parameter used to load.
// if no parameter is specified for loading, the `key` will be used.
redisLoadBucket.get(key, user_id, function (err, user) {
console.log(user);
console.log(user);

// Second time fetches user from redisLoadBucket
redisLoadBucket.get(key, user_id, function (err, user) {
console.log(user);
});
// Second time fetches user from redisLoadBucket
redisLoadBucket.get(key, user_id, function (err, user) {
console.log(user);
});
});

// Outputs:
// Returning user from slow database.
// { id: 123, name: 'Bob' }
// { id: 123, name: 'Bob' }
// { id: 123, name: 'Bob' }
6 changes: 3 additions & 3 deletions index.js
@@ -1,8 +1,8 @@
var fs = require('fs');

exports.store =
exports.Store = require('./lib/store');
exports.Store = require('./lib/store');

exports.__defineGetter__('version', function () {
return JSON.parse(fs.readFileSync(__dirname + '/package.json')).version;
});
return JSON.parse(fs.readFileSync(__dirname + '/package.json')).version;
});
8 changes: 4 additions & 4 deletions lib/adapters/memory.js
@@ -1,4 +1,4 @@
var LRU = require("lru-cache");
const LRU = require("lru-cache");

exports.initialize = function initializeMemory(store, callback) {
store.adapter = {
Expand Down Expand Up @@ -39,14 +39,14 @@ Memory.prototype.getset = function (key, value, cb) {
cb = value;
value = null;
}
var result = this.bucket.get(key);
const result = this.bucket.get(key);
this.bucket.set(key, value);
cb(null, result);
return this;
};

Memory.prototype.getdel = function (key, cb) {
var result = this.bucket.get(key);
const result = this.bucket.get(key);
this.bucket.del(key);
cb(null, result);
return this;
Expand All @@ -70,7 +70,7 @@ Memory.prototype.clear = function (pattern, cb) {
if (typeof pattern === 'function') {
cb = pattern;
}
var del_count = this.bucket.itemCount;
const del_count = this.bucket.itemCount;
this.bucket.reset();
cb(null, del_count);
};
33 changes: 19 additions & 14 deletions lib/adapters/redis.js
@@ -1,10 +1,10 @@
var redis = require('redis');
var _ = require('lodash');
const redis = require('redis');
const _ = require('lodash');

var defaultPort = 6379;
var defaultHost = 'localhost';
const defaultPort = 6379;
const defaultHost = 'localhost';

var defaultPacker = {
const defaultPacker = {
pack: function (target) {
return JSON.stringify(target);
},
Expand All @@ -15,8 +15,9 @@ var defaultPacker = {

// settings ref: https://www.npmjs.com/package/redis
exports.initialize = function initializeRedis(store, callback) {
var settings = store.settings || {};
var client, packer = defaultPacker;
const settings = store.settings || {};
let packer = defaultPacker;
let client;
if (settings instanceof redis.RedisClient) {
client = settings;
process.nextTick(done);
Expand All @@ -28,7 +29,7 @@ exports.initialize = function initializeRedis(store, callback) {
settings.host = settings.host || defaultHost;
client = redis.createClient(settings.port, settings.host, settings);
client.on('connect', function () {
var db = settings.database || settings.db;
const db = settings.database || settings.db;
if (db) {
client.select(db, done);
} else {
Expand All @@ -46,6 +47,10 @@ exports.initialize = function initializeRedis(store, callback) {
store.adapter = {
createBucket: function (options) {
return new Redis(client, packer, options);
},
close: function (cb) {
client.end(true);
cb();
}
};

Expand All @@ -70,7 +75,7 @@ Redis.prototype.get = function (key, cb) {
if (this.isHash) {
this.client.hgetall(key, cb);
} else {
var packer = this.packer;
const packer = this.packer;
this.client.get(key, function (err, result) {
cb(err, packer.unpack(result));
});
Expand All @@ -94,15 +99,15 @@ Redis.prototype.set = function (key, value, cb) {
};

Redis.prototype.getset = function (key, value, cb) {
var self = this;
const self = this;
if (this.isHash) {
this.get(key, function (err, result) {
self.set(key, value, function (err) {
cb(err, result);
});
});
} else {
var packer = this.packer;
const packer = this.packer;
this.client.getset(key, packer.pack(value), function (err, result) {
cb(err, packer.unpack(result));
});
Expand All @@ -111,7 +116,7 @@ Redis.prototype.getset = function (key, value, cb) {
};

Redis.prototype.getdel = function (key, cb) {
var self = this;
const self = this;
this.get(key, function (err, value) {
self.del(key, function (err) {
cb(err, value);
Expand Down Expand Up @@ -165,11 +170,11 @@ Redis.prototype.clear = function (pattern, callback) {
callback = pattern;
pattern = '*';
}
var self = this;
const self = this;
this.keys(pattern, function (err, keys) {
if (err) return callback(err, null);
if (!keys) return callback(err, []);
var error = false, remaining = keys.length, del_count = 0;
let error = false, remaining = keys.length, del_count = 0;
if (remaining === 0) return callback(err, 0);
return keys.forEach(function (key) {
self.client.del(key, function (err) {
Expand Down
14 changes: 7 additions & 7 deletions lib/bucket.js
@@ -1,6 +1,6 @@
"use strict";

var async = require('async');
const async = require('async');

module.exports = exports = Bucket;

Expand Down Expand Up @@ -29,13 +29,13 @@ Bucket.prototype.get = function (key, data, cb) {
cb = data;
data = key;
}
var _key = this.getKey(key);
const _key = this.getKey(key);
if (!this._load) return this.adapter.get(_key, cb);

var loading = this._loading;
const loading = this._loading;
if (loading[key]) return loading[key].push(cb);

var self = this;
const self = this;
async.waterfall([
function (callback) {
self.adapter.has(_key, callback);
Expand All @@ -57,7 +57,7 @@ Bucket.prototype.get = function (key, data, cb) {
});
}
], function (err, value) {
var cbs = loading[key];
const cbs = loading[key];
if (!cbs) return;
loading[key] = false;
if (err) value = null;
Expand Down Expand Up @@ -94,12 +94,12 @@ Bucket.prototype.keys = function (pattern, cb) {
cb = pattern;
pattern = '*';
}
var namespace_len = this.namespace.length + 1;
const namespace_len = this.namespace.length + 1;
this.adapter.keys(this.namespace + ':' + pattern, function (err, keys) {
if (err) return cb(err, null);
if (!keys) return cb(null, []);
if (null == keys) return cb(null, []);
for (var i = 0, l = keys.length; i < l; i++) {
for (let i = 0, l = keys.length; i < l; i++) {
keys[i] = keys[i].substr(namespace_len);
}
return cb(null, keys);
Expand Down
30 changes: 14 additions & 16 deletions lib/store.js
@@ -1,10 +1,10 @@
var _ = require('lodash');
var path = require('path');
var fs = require('fs');
var EventEmitter = require('events').EventEmitter;
var Bucket = require('./bucket');
const _ = require('lodash');
const path = require('path');
const fs = require('fs');
const EventEmitter = require('events').EventEmitter;
const Bucket = require('./bucket');

var existsSync = fs.existsSync || path.existsSync;
const existsSync = fs.existsSync || path.existsSync;

module.exports = exports = Store;

Expand All @@ -26,7 +26,7 @@ function Store(name, settings) {
// and initialize store using adapter
// this is only one initialization entry point of adapter
// this module should define `adapter` member of `this` (store)
var adapter;
let adapter;
if (typeof name === 'object') {
adapter = name;
this.name = adapter.name;
Expand All @@ -45,7 +45,7 @@ function Store(name, settings) {
}
}

var that = this;
const that = this;
adapter.initialize(this, function () {
that.initialized = true;
that.emit('ready');
Expand All @@ -64,11 +64,11 @@ function Store(name, settings) {
require('util').inherits(Store, EventEmitter);

Store.prototype.bucketOptions = function (namespace) {
var bucketOptions = {};
let bucketOptions = {};
if (this.settings && this.settings.buckets) {
bucketOptions = this.settings.buckets[namespace] || {};
}
return _.assign({}, this.defaultBucketOptions, bucketOptions);
return Object.assign({}, this.defaultBucketOptions, bucketOptions);
};

Store.prototype.createBucket = function (namespace, options) {
Expand All @@ -83,12 +83,10 @@ Store.prototype.createBucket = function (namespace, options) {

Store.prototype.bucket = function (namespace) {
namespace = namespace || this.defaultNamespace;
var c = this.buckets[namespace];
if (!c) {
c = this.createBucket(namespace);
this.buckets[namespace] = c;
if (!this.buckets[namespace]) {
this.buckets[namespace] = this.createBucket(namespace);
}
return c;
return this.buckets[namespace];
};

/**
Expand All @@ -100,4 +98,4 @@ Store.prototype.close = function close(cb) {
} else if (cb) {
cb();
}
};
};
15 changes: 8 additions & 7 deletions package.json
Expand Up @@ -26,14 +26,15 @@
"url": "https://github.com/taoyuan/kvs/issues"
},
"dependencies": {
"lodash": "~4.11.2",
"async": "~2.0.0-rc.4",
"lru-cache": "~4.0.1",
"redis": "~2.6.0-2"
"async": "~2.6.0",
"bluebird": "^3.5.1",
"lodash": "~4.17.4",
"lru-cache": "~4.1.1",
"redis": "~2.8.0"
},
"devDependencies": {
"chai": "~3.5.0",
"sinon": "~1.17.4",
"mocha": "~2.4.5"
"chai": "~4.1.2",
"sinon": "~4.1.3",
"mocha": "~4.0.1"
}
}

0 comments on commit 8912b5a

Please sign in to comment.