Comparing version
43
index.js
'use strict'; | ||
module.exports = function(){ | ||
this.cache = {}; | ||
this.cache = Object.create(null); | ||
@@ -29,15 +29,25 @@ /** | ||
/** | ||
* Устанавливает ключ | ||
* | ||
* | ||
* @param string key | ||
* @param integer time | ||
* @param mixed value | ||
* | ||
* @return object | ||
*/ | ||
this.set = (key,time,value) => { | ||
this.set = (key,time,value,fn) => { | ||
this.remove(key); | ||
if (!value) { | ||
value = time; | ||
time = 60; | ||
} | ||
time *= 1000; | ||
this.cache[key] = { | ||
return this.cache[key] = { | ||
timeout: setTimeout(this._remove(key),time), | ||
time: time, | ||
value: value | ||
value: value, | ||
clear: fn | ||
}; | ||
@@ -81,3 +91,3 @@ }; | ||
return value; | ||
return value || null; | ||
} | ||
@@ -102,4 +112,10 @@ | ||
clearInterval(this.cache[key].timeout); | ||
let cache = this.cache[key]; | ||
clearInterval(cache.timeout); | ||
if (cache.clear) { | ||
cache.clear(); | ||
} | ||
delete this.cache[key]; | ||
@@ -118,2 +134,15 @@ | ||
}; | ||
/** | ||
* Очищает хранилище | ||
* | ||
* @return object | ||
*/ | ||
this.clear = () => { | ||
for (let key in this.cache) { | ||
this.remove(key); | ||
} | ||
return this; | ||
}; | ||
}; |
{ | ||
"name": "cache-io", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Memory cache for node js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -21,4 +21,4 @@ # cache-io | ||
```javascript | ||
cache.set('keyName',expire,data); | ||
// return null | ||
cache.set('keyName',expire,data,callback); | ||
// return value || null | ||
``` | ||
@@ -47,2 +47,3 @@ Обновить значения истечения кэша | ||
``` | ||
# cache-io | ||
###English | ||
@@ -49,0 +50,0 @@ Memory cache for node js |
4682
9.01%120
23.71%91
1.11%