/*!
 * connect-memcached
 * Copyright(c) 2012 Michał Thoma <michal@balor.pl>
 * MIT Licensed
 */

var Memcached = require('memcached');
var oneDay = 86400;

/**
 * Return the `MemcachedStore` extending `connect`'s session Store.
 *
 * @param {object} connect
 * @return {Function}
 * @api public
 */

module.exports = function(connect) {
	var Store = connect.session.Store;

	/**
	 * Initialize MemcachedStore with the given `options`.
	 *
	 * @param {Object} options
	 * @api public
	 */
	function MemcachedStore(options) {
		options = options || {};
		Store.call(this, options);
		if (!options.hosts) {
			options.hosts = '127.0.0.1:11211';
		}

		if (options.prefix) {
			this.prefix = options.prefix;
		}

		this.client = new Memcached(options.hosts, options);
	}

	MemcachedStore.prototype.__proto__ = Store.prototype;

	/**
	 * A string prefixed to every memcached key, in case you want to share servers
	 * with something generating its own keys.
	 * @api private
	 */
	MemcachedStore.prototype.prefix = '';

	/**
	 * Translates the given `sid` into a memcached key, optionally with prefix.
	 *
	 * @param {String} sid
	 * @api private
	 */
	MemcachedStore.prototype.getKey = function getKey(sid) {
		return this.prefix + sid;
	};

	/**
	 * Attempt to fetch session by the given `sid`.
	 *
	 * @param {String} sid
	 * @param {Function} fn
	 * @api public
	 */
	MemcachedStore.prototype.get = function(sid, fn) {
		sid = this.getKey(sid);

		this.client.get(sid, function(err, data) {
			try {
				if (!data) {
					return fn();
				}
				fn(null, JSON.parse(data.toString()));
			} catch (err) {
				fn(err);
			}
		});
	};

	/**
	 * Commit the given `sess` object associated with the given `sid`.
	 *
	 * @param {String} sid
	 * @param {Session} sess
	 * @param {Function} fn
	 * @api public
	 */
	MemcachedStore.prototype.set = function(sid, sess, fn) {
		sid = this.getKey(sid);

		try {
			var maxAge = sess.cookie.maxAge;
			var ttl = 'number' == typeof maxAge ? maxAge / 1000 | 0 : oneDay;
			var sess = JSON.stringify(sess);

			this.client.set(sid, sess, ttl, function() {
				fn && fn.apply(this, arguments);
			});
		} catch (err) {
			fn && fn(err);
		}
	};

	/**
	 * Destroy the session associated with the given `sid`.
	 *
	 * @param {String} sid
	 * @param {Function} fn
	 * @api public
	 */
	MemcachedStore.prototype.destroy = function(sid, fn) {
		sid = this.getKey(sid);
		this.client.del(sid, fn);
	};

	/**
	 * Fetch number of sessions.
	 *
	 * @param {Function} fn
	 * @api public
	 */
	MemcachedStore.prototype.length = function(fn) {
		this.client.items(fn);
	};

	/**
	 * Clear all sessions.
	 *
	 * @param {Function} fn
	 * @api public
	 */
	MemcachedStore.prototype.clear = function(fn) {
		this.client.flush(fn);
	};

	return MemcachedStore;
};

@@ -293,17 +293,16 @@
 lic%0A */%0A
-%0A
 module.e
@@ -573,16 +573,81 @@
 tions);%0A
+%0A%09%09this.prefix = options.prefix %7C%7C '';%0A%09%09if (!options.client) %7B%0A%09
 %09%09if (!o
@@ -662,16 +662,17 @@
 osts) %7B%0A
+%09
 %09%09%09optio
@@ -707,25 +707,23 @@
 ;%0A%09%09
+%09
 %7D%0A%0A%09%09
-if (
+%09
 options.
 pref
@@ -722,63 +722,8 @@
 ons.
-prefix) %7B%0A%09%09%09this.prefix = options.prefix;%0A%09%09%7D%0A%0A%09%09this.
 clie
@@ -764,16 +764,53 @@
 options)
+;%0A%09%09%7D%0A%0A%09%09this.client = options.client
 ;%0A%09%7D%0A%0A%09M

use the provided client, if given