Skip to content

Commit

Permalink
Removing LRUItem class, creating clone() & dump(), cloning valu…
Browse files Browse the repository at this point in the history
…es on `set()` & `get()` so the cache is pristine, adding tests
  • Loading branch information
avoidwork committed Nov 14, 2016
1 parent 7202699 commit 52df3a9
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 40 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@ Least Recently Used cache for Client or Server.
var cache = lru(500);
```

## clone
### Method

Clones a value; called when setting cache value

return {Mixed} Clone of input

**Example**

```javascript
cache.clone({abc: true});
```

## dump
### Method

Produces a dump of the cache as JSON or a clone

param {String} string Defaults to `true`
return {Mixed} String or Object

**Example**

```javascript
cache.dump(); // JSON
cache.dump(false); // Object
```

## evict
### Method

Expand Down
36 changes: 24 additions & 12 deletions lib/tiny-lru.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,10 @@
* @copyright 2016
* @license BSD-3-Clause
* @link https://github.com/avoidwork/tiny-lru
* @version 1.1.0
* @version 1.2.0
*/
(function (global) {

class LRUItem {
constructor (value) {
this.next = null;
this.previous = null;
this.value = value;
}
}

class LRU {
constructor (max) {
this.cache = {};
Expand All @@ -26,6 +18,18 @@ class LRU {
this.length = 0;
}

clone (arg) {
let output;

if (typeof arg !== "function") {
output = JSON.parse(JSON.stringify(arg, null, 0));
} else {
output = arg;
}

return output;
}

delete (key) {
return this.remove(key);
}
Expand All @@ -38,12 +42,16 @@ class LRU {
return this;
}

dump (string = true) {
return string ? JSON.stringify(this, null, 0) : this.clone(this);
}

get (key) {
let cached = this.cache[key],
output;

if (cached) {
output = cached.value;
output = this.clone(cached.value);
this.set(key, cached.value);
}

Expand Down Expand Up @@ -85,9 +93,13 @@ class LRU {
let obj = this.remove(key);

if (!obj) {
obj = new LRUItem(value);
obj = {
next: null,
previous: null,
value: this.clone(value)
};
} else {
obj.value = value;
obj.value = this.clone(value);
}

obj.next = null;
Expand Down
44 changes: 30 additions & 14 deletions lib/tiny-lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,9 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
* @copyright 2016
* @license BSD-3-Clause
* @link https://github.com/avoidwork/tiny-lru
* @version 1.1.0
* @version 1.2.0
*/
(function (global) {
var LRUItem = function LRUItem(value) {
_classCallCheck(this, LRUItem);

this.next = null;
this.previous = null;
this.value = value;
};

var LRU = function () {
function LRU(max) {
_classCallCheck(this, LRU);
Expand All @@ -34,6 +26,19 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}

_createClass(LRU, [{
key: "clone",
value: function clone(arg) {
var output = void 0;

if (typeof arg !== "function") {
output = JSON.parse(JSON.stringify(arg, null, 0));
} else {
output = arg;
}

return output;
}
}, {
key: "delete",
value: function _delete(key) {
return this.remove(key);
Expand All @@ -47,14 +52,21 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons

return this;
}
}, {
key: "dump",
value: function dump() {
var string = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;

return string ? JSON.stringify(this, null, 0) : this.clone(this);
}
}, {
key: "get",
value: function get(key) {
var cached = this.cache[key],
output = undefined;
output = void 0;

if (cached) {
output = cached.value;
output = this.clone(cached.value);
this.set(key, cached.value);
}

Expand Down Expand Up @@ -99,9 +111,13 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
var obj = this.remove(key);

if (!obj) {
obj = new LRUItem(value);
obj = {
next: null,
previous: null,
value: this.clone(value)
};
} else {
obj.value = value;
obj.value = this.clone(value);
}

obj.next = null;
Expand Down Expand Up @@ -130,7 +146,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}();

function factory() {
var max = arguments.length <= 0 || arguments[0] === undefined ? 1000 : arguments[0];
var max = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1000;

return new LRU(max);
}
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.

0 comments on commit 52df3a9

Please sign in to comment.