Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'deleteOnTimeout' configuration #114

Open
wants to merge 3 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ which should print

* Returns all the cache keys

### deleteOnTimeout = function(bool)

* Turns on or off automatic delete on TTL expiration
* Defaults to on

### exportJson = function()

* Returns a JSON string representing all the cache data
Expand Down
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function Cache () {
var _missCount = 0;
var _size = 0;
var _debug = false;
var _deleteOnTimeout = true;

this.put = function(key, value, time, timeoutCallback) {
if (_debug) {
Expand All @@ -32,7 +33,13 @@ function Cache () {

if (!isNaN(record.expire)) {
record.timeout = setTimeout(function() {
_del(key);
if (_deleteOnTimeout) {
_del(key);
} else {
var record = _cache[key];
delete record.expire;
delete record.timeout;
}
if (timeoutCallback) {
timeoutCallback(key, value);
}
Expand Down Expand Up @@ -128,6 +135,10 @@ function Cache () {
return Object.keys(_cache);
};

this.deleteOnTimeout = function(bool) {
_deleteOnTimeout = bool;
}

this.exportJson = function() {
var plainJsCache = {};

Expand Down