Skip to content

Commit

Permalink
- Fixed expires should be stored as number
Browse files Browse the repository at this point in the history
- Fixed bug in cluster mode
- Update unit test now using assert strict
  • Loading branch information
aalfiann committed Dec 30, 2020
1 parent c74ea35 commit 95278d9
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 18 deletions.
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ FileStore.prototype.get = function get(key, fn) {
if (Fs.existsSync(cacheFile)) {
data = Fs.readFileSync(cacheFile);
data = JSON.parse(data);
self.cache[key] = data.expire; //ensures cache sync in all clusters.
} else {
return fn(null, null);
}

if (!this.cache[key]) {
return fn(null, null);
}

if (!data) return fn(null, data);
if (data.expire < Date.now()) {
Expand Down Expand Up @@ -85,7 +83,7 @@ FileStore.prototype.set = function set(key, val, ttl, fn) {
try {
data = {
value: JSON.stringify(val),
expire: JSON.stringify(Date.now() + ttl)
expire: Date.now() + ttl
};
} catch (e) {
return fn(e);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "recacheman-file",
"version": "0.2.4",
"version": "0.2.5",
"description": "File caching library for Node.JS and also cache engine for cacheman",
"author": "Taron Foxworth <taronfoxworth@gmail.com>",
"contributors": [
Expand Down
56 changes: 56 additions & 0 deletions test/test-multi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var assert = require('assert');
var Path = require('path');
var Cache = require('../');

describe('cacheman-file-multi', function() {
var cache1,
cache2;

before(function(done) {
cache1 = new Cache({
tmpDir: Path.join(process.cwd(), 'temp')
}, {});
cache2 = new Cache({
tmpDir: Path.join(process.cwd(), 'temp')
}, {});
done();
});
after(function(done) {
cache1.clear('test');
cache2.clear('test');
done();
});

it('should store items', function(done) {
cache1.set('test1', {
a: 1
}, function(err) {
if (err)
return done(err);
cache1.get('test1', function(err, data) {
if (err)
return done(err);
assert.strictEqual(data.a, 1);
done();
});
});
});
it('should get stored items', function(done) {
cache1.get('test1', function(err, data) {
if (err)
return done(err);
assert.strictEqual(data.a, 1);
cache2.get('test1', function(err, data) {
if (err)
return done(err);
try {
assert.notStrictEqual(data, null)
assert.strictEqual(data.a, 1)
done()
} catch (e) {
done(e)
}
});
});
});
});
26 changes: 13 additions & 13 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('cacheman-file', function() {
if (err) return done(err);
cache.get('test1', function(err, data) {
if (err) return done(err);
assert.equal(data.a, 1);
assert.strictEqual(data.a, 1);
done();
});
});
Expand Down Expand Up @@ -88,12 +88,12 @@ describe('cacheman-file', function() {
// compare cached key against sanitized key
var lastKey = Object.keys(cache.cache).pop();
var sanitizedKey = sanitize(key);
assert.equal(sanitizedKey, lastKey);
assert.strictEqual(sanitizedKey, lastKey);

// check functionality, along the way
cache.get(key, function(err, data) {
if (err) return done(err);
assert.equal(data.a, 1);
assert.strictEqual(data.a, 1);
done();
});
});
Expand All @@ -105,12 +105,12 @@ describe('cacheman-file', function() {
if (err) return done(err);
cache.get('test5', function(err, data) {
if (err) return done(err);
assert.equal(data, value);
assert.strictEqual(data, value);
cache.del('test5', function(err) {
if (err) return done(err);
cache.get('test5', function(err, data) {
if (err) return done(err);
assert.equal(data, null);
assert.strictEqual(data, null);
done();
});
});
Expand All @@ -124,12 +124,12 @@ describe('cacheman-file', function() {
if (err) return done(err);
cache.get('test6', function(err, data) {
if (err) return done(err);
assert.equal(data, value);
assert.strictEqual(data, value);
cache.clear('', function(err) {
if (err) return done(err);
cache.get('test6', function(err, data) {
if (err) return done(err);
assert.equal(data, null);
assert.strictEqual(data, null);
done();
});
});
Expand All @@ -146,7 +146,7 @@ describe('cacheman-file', function() {
setTimeout(function() {
cache.get('test1', function(err, data) {
if (err) return done(err);
assert.equal(data, null);
assert.strictEqual(data, null);
done();
});
}, 1100);
Expand All @@ -161,15 +161,15 @@ describe('cacheman-file', function() {
];

cache.set('test1', items[0], function (err) {
assert.deepEqual(null, err);
assert.deepStrictEqual(null, err);
cache.set('test2', items[1], function (err) {
assert.deepEqual(null, err);
assert.deepStrictEqual(null, err);
cache.set('test3', items[2], function (err) {
assert.deepEqual(null, err);
assert.deepStrictEqual(null, err);

cache.getAll(function (err, results) {
assert.deepEqual(null, err);
assert.deepEqual(items, results);
assert.deepStrictEqual(null, err);
assert.deepStrictEqual(items, results);
done();
});
});
Expand Down

0 comments on commit 95278d9

Please sign in to comment.