Skip to content
This repository has been archived by the owner on Dec 15, 2023. It is now read-only.

Add option to expire #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Logs
logs
*.log

*.DS_Store
# Runtime data
pids
*.pid
Expand Down
16 changes: 16 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ module.exports = function(config) {
* @param {String} namespace The namespace to use for storing in Redis
* @returns {{get: get, save: save, all: all, allById: allById}}
*/
function isInt(n) {
if (isNaN(n) | n === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a double pipe operator for logical OR: ||

return false;
}
return (typeof x === 'number') && ( x % 1 === 0);
}

function getStorageObj(client, namespace) {
return {
get: function(id, cb) {
Expand All @@ -47,6 +54,15 @@ function getStorageObj(client, namespace) {
}

client.hset(namespace, object.id, JSON.stringify(object), cb);

if (object.expire) {
// check for integer
if (!isInt(object.expire)) {
return cb(new Error('The \"expire\" property must be a positive integer', {}));
} else {
client.EXPIRE(namespace, parseInt(object.expire));
}
}
},
remove: function(id, cb) {
client.hdel(namespace, [id], cb);
Expand Down