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

Separated variables for debug and stats updating #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 9 additions & 4 deletions 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 _countStats = false;

this.put = function(key, value, time, timeoutCallback) {
if (_debug) {
Expand Down Expand Up @@ -75,7 +76,7 @@ function Cache () {
}
_size = 0;
_cache = Object.create(null);
if (_debug) {
if (_countStats) {
_hitCount = 0;
_missCount = 0;
}
Expand All @@ -85,15 +86,15 @@ function Cache () {
var data = _cache[key];
if (typeof data != "undefined") {
if (isNaN(data.expire) || data.expire >= Date.now()) {
if (_debug) _hitCount++;
if (_countStats) _hitCount++;
return data.value;
} else {
// free some space
if (_debug) _missCount++;
if (_countStats) _missCount++;
_size--;
delete _cache[key];
}
} else if (_debug) {
} else if (_countStats) {
_missCount++;
}
return null;
Expand All @@ -115,6 +116,10 @@ function Cache () {
this.debug = function(bool) {
_debug = bool;
};

this.countStats = function(bool) {
_countStats = bool;
};

this.hits = function() {
return _hitCount;
Expand Down