Skip to content

Commit

Permalink
Creating reset() & wiring it into clear() and constructor()
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Oct 9, 2017
1 parent 05dff7f commit 9764b4f
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 63 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ Removes item from cache
const staleItem = cache.remove("myKey");
```

## reset
### Method

Resets the cache to it's original state
return {Object} LRU instance
**Example**
```javascript
cache.reset();
```
## set
### Method
Expand Down
47 changes: 26 additions & 21 deletions lib/tiny-lru.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,19 @@
"use strict";

(function (global) {
let next = typeof process !== "undefined" ? process.nextTick : arg => setTimeout(arg, 1);
const next = typeof process !== "undefined" ? process.nextTick : arg => setTimeout(arg, 1),
keys = typeof Reflect !== "undefined" ? Reflect.ownKeys : Object.keys;

class LRU {
constructor (max) {
this.cache = {};
this.first = null;
this.last = null;
this.length = 0;
this.max = max;
this.notify = false;
this.onchange = () => {};
this.update = arg => {
let obj = JSON.parse(arg);

Object.keys(obj).forEach(i => {
this[i] = obj[i];
});
};

return this.reset();
}

clear (silent = false) {
this.cache = {};
this.first = null;
this.last = null;
this.length = 0;
this.reset();

if (!silent && this.notify) {
next(this.onchange("clear", this.dump()));
Expand Down Expand Up @@ -82,6 +70,8 @@
return key in this.cache;
}

onchange () {}

remove (k, silent = false) {
let key = typeof k !== "string" ? k.toString() : k,
cached = this.cache[key];
Expand Down Expand Up @@ -126,6 +116,15 @@
return cached;
}

reset () {
this.cache = Object.create(null);
this.first = null;
this.last = null;
this.length = 0;

return this;
}

set (key, value) {
let first, item;

Expand Down Expand Up @@ -171,6 +170,14 @@

return this;
}

update (arg) {
const obj = JSON.parse(arg);

keys(obj).forEach(i => {
this[i] = obj[i];
});
}
}

function factory (max = 1000) {
Expand All @@ -180,10 +187,8 @@
// Node, AMD & window supported
if (typeof exports !== "undefined") {
module.exports = factory;
} else if (typeof define === "function" && define.amd) {
define(function () {
return factory;
});
} else if (typeof define === "function" && define.amd !== void 0) {
define(() => factory);
} else {
global.lru = factory;
}
Expand Down
48 changes: 29 additions & 19 deletions lib/tiny-lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,25 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
(function (global) {
var next = typeof process !== "undefined" ? process.nextTick : function (arg) {
return setTimeout(arg, 1);
};
},
keys = typeof Reflect !== "undefined" ? Reflect.ownKeys : Object.keys;

var LRU = function () {
function LRU(max) {
var _this = this;

_classCallCheck(this, LRU);

this.cache = {};
this.first = null;
this.last = null;
this.length = 0;
this.max = max;
this.notify = false;
this.onchange = function () {};
this.update = function (arg) {
var obj = JSON.parse(arg);

Object.keys(obj).forEach(function (i) {
_this[i] = obj[i];
});
};
return this.reset();
}

_createClass(LRU, [{
key: "clear",
value: function clear() {
var silent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;

this.cache = {};
this.first = null;
this.last = null;
this.length = 0;
this.reset();

if (!silent && this.notify) {
next(this.onchange("clear", this.dump()));
Expand Down Expand Up @@ -102,6 +88,9 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
value: function has(key) {
return key in this.cache;
}
}, {
key: "onchange",
value: function onchange() {}
}, {
key: "remove",
value: function remove(k) {
Expand Down Expand Up @@ -149,6 +138,16 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons

return cached;
}
}, {
key: "reset",
value: function reset() {
this.cache = Object.create(null);
this.first = null;
this.last = null;
this.length = 0;

return this;
}
}, {
key: "set",
value: function set(key, value) {
Expand Down Expand Up @@ -197,6 +196,17 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons

return this;
}
}, {
key: "update",
value: function update(arg) {
var _this = this;

var obj = JSON.parse(arg);

keys(obj).forEach(function (i) {
_this[i] = obj[i];
});
}
}]);

return LRU;
Expand All @@ -211,7 +221,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
// Node, AMD & window supported
if (typeof exports !== "undefined") {
module.exports = factory;
} else if (typeof define === "function" && define.amd) {
} else if (typeof define === "function" && define.amd !== void 0) {
define(function () {
return factory;
});
Expand Down
2 changes: 1 addition & 1 deletion lib/tiny-lru.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 9764b4f

@avoidwork
Copy link
Owner Author

Choose a reason for hiding this comment

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

Making reset() public was a mistake; it introduced 2 methods that were slightly different. It should've remained in lexical scope with a call(). See comments on d0cdcfe

Please sign in to comment.