commit
7f625e22f7
4 changed files with 55 additions and 49 deletions
@ -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
|
||||||
|
connect(url) { |
||||||
|
this.client = new memcached(url); |
||||||
|
|
||||||
|
winston.info(`connecting to memcached on ${url}`); |
||||||
|
|
||||||
|
this.client.on('failure', function(error) { |
||||||
|
winston.info('error connecting to memcached', {error}); |
||||||
|
}); |
||||||
} |
} |
||||||
}; |
|
||||||
|
// Save file in a key
|
||||||
// Create a connection
|
set(key, data, callback, skipExpire) { |
||||||
MemcachedDocumentStore.connect = function(options) { |
this.client.set(key, data, skipExpire ? 0 : this.expire, (error) => { |
||||||
var host = options.host || '127.0.0.1'; |
callback(!error); |
||||||
var port = options.port || 11211; |
}); |
||||||
this.client = new memcached.Client(port, host); |
} |
||||||
this.client.connect(); |
|
||||||
this.client.on('connect', function() { |
// Get a file from a key
|
||||||
winston.info('connected to memcached on ' + host + ':' + port); |
get(key, callback, skipExpire) { |
||||||
}); |
this.client.get(key, (error, data) => { |
||||||
this.client.on('error', function(e) { |
callback(error ? false : data); |
||||||
winston.info('error connecting to memcached', { error: e }); |
|
||||||
}); |
// Update the key so that the expiration is pushed forward
|
||||||
}; |
if (!skipExpire) { |
||||||
|
this.set(key, data, (updateSucceeded) => { |
||||||
// Save file in a key
|
if (!updateSucceeded) { |
||||||
MemcachedDocumentStore.prototype.set = |
winston.error('failed to update expiration on GET', {key}); |
||||||
function(key, data, callback, skipExpire) { |
} |
||||||
MemcachedDocumentStore.client.set(key, data, function(err) { |
}, skipExpire); |
||||||
err ? callback(false) : callback(true); |
} |
||||||
}, skipExpire ? 0 : this.expire); |
}); |
||||||
}; |
} |
||||||
|
|
||||||
// Get a file from a key
|
} |
||||||
MemcachedDocumentStore.prototype.get = function(key, callback, skipExpire) { |
|
||||||
var _this = this; |
|
||||||
MemcachedDocumentStore.client.get(key, function(err, reply) { |
|
||||||
callback(err ? false : reply); |
|
||||||
if (_this.expire && !skipExpire) { |
|
||||||
winston.warn('store does not currently push forward expirations on GET'); |
|
||||||
} |
|
||||||
}); |
|
||||||
}; |
|
||||||
|
|
||||||
module.exports = MemcachedDocumentStore; |
module.exports = MemcachedDocumentStore; |
||||||
|
Loading…
Reference in new issue