Skip to content
This repository has been archived by the owner on Mar 6, 2018. It is now read-only.

Commit

Permalink
do not return the if the value is expired
Browse files Browse the repository at this point in the history
  • Loading branch information
jfromaniello committed Dec 23, 2013
1 parent ac89f9c commit 0a09f8f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ MongoStore.prototype.set = function (key, value, lifetime, callback) {

MongoStore.prototype.get = function (key, callback) {
var _id = this.options.prefix + key;
var collection = this._collection;

this._collection.findOne({ _id: _id }, function (err, doc) {
collection.findOne({ _id: _id }, function (err, doc) {
if (err) {
typeof callback == 'function' && callback(err, null);
} else {
var data;
if (doc && doc.expires < new Date()) {
collection.remove({ _id: _id }, {w: 0});
return callback();
}
if (doc) {
data = doc.data;
data.lastRequest = new Date(data.lastRequest);
Expand Down
37 changes: 36 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ getDb.init('default', 'mongodb://localhost/test_brute_express_mongo');
var mongoStore, collection;

describe('MongoStore', function () {
before(function (done) {
beforeEach(function (done) {
mongoStore = new MongoStore(function (callback) {
getDb(function (db) {
collection = db.collection('api_limits');
Expand Down Expand Up @@ -42,6 +42,41 @@ describe('MongoStore', function () {
});
});

it('should return undef if expired', function (done) {
mongoStore.set('foo', { bar: 123 }, 0, function (err) {
if (err) return done(err);
setTimeout(function () {

mongoStore.get('foo', function (err, data) {
if (err) return done(err);
expect(data).to.be(undefined);
done();
});

}, 200);
});
});

it('should delete the doc if expired', function (done) {
mongoStore.set('foo', { bar: 123 }, 0, function (err) {
if (err) return done(err);
setTimeout(function () {
mongoStore.get('foo', function (err, data) {

setTimeout(function () {

collection.findOne({ _id: 'foo' }, function (err, d) {
expect(d).to.be(null);
done();
});

}, 100);

});
}, 100);
});
});


it('should be able to reset', function (done) {
mongoStore.set('foo', {bar:123}, 1000, function (err) {
Expand Down

0 comments on commit 0a09f8f

Please sign in to comment.