mirror of
https://github.com/seejohnrun/haste-server.git
synced 2024-11-01 03:21:21 +00:00
Rewrite the memcached client
* Update syntax to ES6 * Use `memcached` instead of `memcache` * Fix restrictions where expirations weren't pushed forward on GET * Fix a bug where we were unnecessarily bumping expirations on key search Closes #201
This commit is contained in:
parent
ad7702aaf4
commit
827e7b51b5
4 changed files with 51 additions and 45 deletions
|
@ -156,9 +156,9 @@ All of which are optional except `type` with very logical default values.
|
||||||
|
|
||||||
### Memcached
|
### Memcached
|
||||||
|
|
||||||
To use memcached storage you must install the `memcache` package via npm
|
To use memcache storage you must install the `memcached` package via npm
|
||||||
|
|
||||||
`npm install memcache`
|
`npm install memcached`
|
||||||
|
|
||||||
Once you've done that, your config section should look like:
|
Once you've done that, your config section should look like:
|
||||||
|
|
||||||
|
|
|
@ -33,10 +33,9 @@
|
||||||
},
|
},
|
||||||
|
|
||||||
"storage": {
|
"storage": {
|
||||||
"type": "redis",
|
"type": "memcached",
|
||||||
"host": "0.0.0.0",
|
"host": "127.0.0.1",
|
||||||
"port": 6379,
|
"port": 11211,
|
||||||
"db": 2,
|
|
||||||
"expire": 2592000
|
"expire": 2592000
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -123,7 +123,7 @@ DocumentHandler.prototype.chooseKey = function(callback) {
|
||||||
} else {
|
} else {
|
||||||
callback(key);
|
callback(key);
|
||||||
}
|
}
|
||||||
});
|
}, true); // Don't bump expirations when key searching
|
||||||
};
|
};
|
||||||
|
|
||||||
DocumentHandler.prototype.acceptableKey = function() {
|
DocumentHandler.prototype.acceptableKey = function() {
|
||||||
|
|
|
@ -1,45 +1,52 @@
|
||||||
var memcached = require('memcache');
|
const memcached = require('memcached');
|
||||||
var winston = require('winston');
|
const winston = require('winston');
|
||||||
|
|
||||||
// Create a new store with options
|
class MemcachedDocumentStore {
|
||||||
var MemcachedDocumentStore = function(options) {
|
|
||||||
this.expire = options.expire;
|
// Create a new store with options
|
||||||
if (!MemcachedDocumentStore.client) {
|
constructor(options) {
|
||||||
MemcachedDocumentStore.connect(options);
|
this.expire = options.expire;
|
||||||
|
|
||||||
|
const host = options.host || '127.0.0.1';
|
||||||
|
const port = options.port || 11211;
|
||||||
|
const url = `${host}:${port}`;
|
||||||
|
this.connect(url);
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
// Create a connection
|
// Create a connection
|
||||||
MemcachedDocumentStore.connect = function(options) {
|
connect(url) {
|
||||||
var host = options.host || '127.0.0.1';
|
this.client = new memcached(url);
|
||||||
var port = options.port || 11211;
|
|
||||||
this.client = new memcached.Client(port, host);
|
|
||||||
this.client.connect();
|
|
||||||
this.client.on('connect', function() {
|
|
||||||
winston.info('connected to memcached on ' + host + ':' + port);
|
|
||||||
});
|
|
||||||
this.client.on('error', function(e) {
|
|
||||||
winston.info('error connecting to memcached', { error: e });
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// Save file in a key
|
winston.info(`connecting to memcached on ${url}`);
|
||||||
MemcachedDocumentStore.prototype.set =
|
|
||||||
function(key, data, callback, skipExpire) {
|
|
||||||
MemcachedDocumentStore.client.set(key, data, function(err) {
|
|
||||||
err ? callback(false) : callback(true);
|
|
||||||
}, skipExpire ? 0 : this.expire);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Get a file from a key
|
this.client.on('failure', function(error) {
|
||||||
MemcachedDocumentStore.prototype.get = function(key, callback, skipExpire) {
|
winston.info('error connecting to memcached', {error});
|
||||||
var _this = this;
|
});
|
||||||
MemcachedDocumentStore.client.get(key, function(err, reply) {
|
}
|
||||||
callback(err ? false : reply);
|
|
||||||
if (_this.expire && !skipExpire) {
|
// Save file in a key
|
||||||
winston.warn('store does not currently push forward expirations on GET');
|
set(key, data, callback, skipExpire) {
|
||||||
}
|
this.client.set(key, data, skipExpire ? 0 : this.expire, (error) => {
|
||||||
});
|
callback(!error);
|
||||||
};
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get a file from a key
|
||||||
|
get(key, callback, skipExpire) {
|
||||||
|
this.client.get(key, (error, data) => {
|
||||||
|
callback(error ? false : data);
|
||||||
|
|
||||||
|
// Update the key so that the expiration is pushed forward
|
||||||
|
if (!skipExpire) {
|
||||||
|
this.set(key, data, (updateSucceeded) => {
|
||||||
|
if (!updateSucceeded) {
|
||||||
|
winston.error('failed to update expiration on GET', {key});
|
||||||
|
}
|
||||||
|
}, skipExpire);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = MemcachedDocumentStore;
|
module.exports = MemcachedDocumentStore;
|
||||||
|
|
Loading…
Reference in a new issue