"use strict";

const jetpack = require('fs-jetpack');
const extend = require('extend');

const path = require('path');
const util = require('util');

const DEFAULT_OPTIONS = {
	'cacheDir': process.cwd() + '/cache',
	'cacheExpire': 3600 * 1000, // 1hr
	'prefix': 'cs',
	'fixCacheExpire': false,
	'rejectOnNull': false
};

let FileCacheSimple = function(options) {
	this._options = (options) ? extend(true, DEFAULT_OPTIONS, options) : DEFAULT_OPTIONS;
	this._fs = jetpack.cwd(this._options.cacheDir);
};

FileCacheSimple.prototype.set = function(key, value, fixCacheExpire) {
	let cacheFile = util.format('%s.%s.json', this._options.prefix, key);
	let cacheObj = {
		'cachedOn': (new Date()).toISOString(),
		'content': value
	};
	if(this._options.fixCacheExpire || fixCacheExpire)
		cacheObj.cacheExpire = (fixCacheExpire) ? fixCacheExpire : this._options.cacheExpire;
	this._fs.writeAsync(cacheFile, cacheObj);
};

FileCacheSimple.prototype.get = function(key) {
	let self = this;
	return new Promise(function(resolve, reject) {
		let cacheFile = util.format('%s.%s.json', self._options.prefix, key);
		self._fs.readAsync(cacheFile, 'jsonWithDates')
			.then(function(cache) {
				if(!cache || cache === null) {
					if(self._options.rejectOnNull)
						return reject();
					return resolve(null);
				}

				let currentTime = (new Date()).getTime();
				let cacheExpire = (cache.cacheExpire) ? cache.cacheExpire : self._options.cacheExpire;
				if(currentTime - cache.cachedOn >= cacheExpire) {
					return resolve(null);
				}
				return resolve(cache.content);
			})
			.catch(reject);
	});
};

FileCacheSimple.prototype.remove = function(key) {
	let cacheFile = util.format('%s.%s.json', this._options.prefix, key);
	this._fs.remove(cacheFile);
};

exports = module.exports = FileCacheSimple;

"use strict";

const jetpack = require('fs-jetpack');
const extend = require('extend');

const path = require('path');
const util = require('util');

const DEFAULT_OPTIONS = {
	'cacheDir': process.cwd() + '/cache',
	'cacheExpire': 3600 * 1000, // 1hr
	'prefix': 'cs',
	'fixCacheExpire': false,
	'rejectOnNull': false
};

let FileCacheSimple = function(options) {
	this._options = (options) ? extend(true, DEFAULT_OPTIONS, options) : DEFAULT_OPTIONS;
	this._fs = jetpack.cwd(this._options.cacheDir);
};

FileCacheSimple.prototype.set = function(key, value, fixCacheExpire) {
	let cacheFile = util.format('%s.%s.json', this._options.prefix, key);
	let cacheObj = {
		'cachedOn': (new Date()).toISOString(),
		'content': value
	};
	if(this._options.fixCacheExpire || fixCacheExpire)
		cacheObj.cacheExpire = (fixCacheExpire) ? fixCacheExpire : this._options.cacheExpire;
	return this._fs.writeAsync(cacheFile, cacheObj);
};

FileCacheSimple.prototype.get = function(key) {
	let self = this;
	return new Promise(function(resolve, reject) {
		let cacheFile = util.format('%s.%s.json', self._options.prefix, key);
		self._fs.readAsync(cacheFile, 'jsonWithDates')
			.then(function(cache) {
				if(!cache || cache === null) {
					if(self._options.rejectOnNull)
						return reject();
					return resolve(null);
				}

				let currentTime = (new Date()).getTime();
				let cacheExpire = (cache.cacheExpire) ? cache.cacheExpire : self._options.cacheExpire;
				if(currentTime - cache.cachedOn >= cacheExpire) {
					return resolve(null);
				}
				return resolve(cache.content);
			})
			.catch(reject);
	});
};

FileCacheSimple.prototype.remove = function(key) {
	let cacheFile = util.format('%s.%s.json', this._options.prefix, key);
	return this._fs.removeAsync(cacheFile);
};

exports = module.exports = FileCacheSimple;

Return promises on remove and write