fast-ttl-cache 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -5
- package/dist/index.js +6 -5
- package/dist/index.mjs +6 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,20 +37,24 @@ cache.size; // return 0
|
|
|
37
37
|
## API
|
|
38
38
|
```FastTTLCache(options) consturctor```
|
|
39
39
|
|
|
40
|
-
options.ttl
|
|
41
|
-
options.capacity
|
|
40
|
+
- ```options.ttl```: number of millseconds, defaults to Infinity
|
|
41
|
+
- ```options.capacity```: number of max capacity, defaults to Infinity
|
|
42
42
|
|
|
43
43
|
```FastTTLCache.prototype.put(key, value)```
|
|
44
44
|
|
|
45
|
-
Add or update the value into cache with key and timestamp.
|
|
45
|
+
- Add or update the value into cache with key and timestamp.
|
|
46
46
|
|
|
47
47
|
```FastTTLCache.prototype.get(key)```
|
|
48
48
|
|
|
49
|
-
Get the value of the key from cache, return null if the key is not exists or has been expired.
|
|
49
|
+
- Get the value of the key from cache, return null if the key is not exists or has been expired.
|
|
50
|
+
|
|
51
|
+
```FastTTLCache.prototype.del(key)```
|
|
52
|
+
|
|
53
|
+
- delete key and it's value from cache, return false if the key is not exists, otherwise return true.
|
|
50
54
|
|
|
51
55
|
```FastTTLCache.prototype.size```
|
|
52
56
|
|
|
53
|
-
return the current size of cache.
|
|
57
|
+
- return the current size of cache, note because of the lazy deletion mechanism, it's not the exact number of cache items that are valid.
|
|
54
58
|
|
|
55
59
|
## License
|
|
56
60
|
MIT
|
package/dist/index.js
CHANGED
|
@@ -64,7 +64,7 @@ var TTLCache = class {
|
|
|
64
64
|
const item = this.store.get(key);
|
|
65
65
|
if (!item) return null;
|
|
66
66
|
if (Date.now() - item.time > this.ttl) {
|
|
67
|
-
this.
|
|
67
|
+
this.del(key);
|
|
68
68
|
return null;
|
|
69
69
|
}
|
|
70
70
|
return item.value;
|
|
@@ -105,15 +105,15 @@ var TTLCache = class {
|
|
|
105
105
|
this.store.set(key, newItem);
|
|
106
106
|
this.tail = key;
|
|
107
107
|
if (this.size > this.capacity) {
|
|
108
|
-
this.
|
|
108
|
+
this.del(this.head);
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
111
|
/**
|
|
112
112
|
* 移除节点
|
|
113
113
|
* @param key
|
|
114
114
|
*/
|
|
115
|
-
|
|
116
|
-
if (!this.store.has(key)) return;
|
|
115
|
+
del(key) {
|
|
116
|
+
if (!this.store.has(key)) return false;
|
|
117
117
|
const curItem = this.store.get(key);
|
|
118
118
|
if (this.size === 1) {
|
|
119
119
|
this.head = this.tail = null;
|
|
@@ -128,6 +128,7 @@ var TTLCache = class {
|
|
|
128
128
|
curItem.next.pre = curItem.pre;
|
|
129
129
|
}
|
|
130
130
|
this.store.delete(key);
|
|
131
|
+
return true;
|
|
131
132
|
}
|
|
132
133
|
/**
|
|
133
134
|
* 将节点移动到队尾,队尾的节点一定是最后一个更新的
|
|
@@ -137,7 +138,7 @@ var TTLCache = class {
|
|
|
137
138
|
if (!this.store.has(key)) return;
|
|
138
139
|
if (this.tail === key) return;
|
|
139
140
|
const curItem = this.store.get(key);
|
|
140
|
-
this.
|
|
141
|
+
this.del(key);
|
|
141
142
|
const curTail = this.store.get(this.tail);
|
|
142
143
|
curTail.next = curItem;
|
|
143
144
|
curItem.pre = curTail;
|
package/dist/index.mjs
CHANGED
|
@@ -41,7 +41,7 @@ var TTLCache = class {
|
|
|
41
41
|
const item = this.store.get(key);
|
|
42
42
|
if (!item) return null;
|
|
43
43
|
if (Date.now() - item.time > this.ttl) {
|
|
44
|
-
this.
|
|
44
|
+
this.del(key);
|
|
45
45
|
return null;
|
|
46
46
|
}
|
|
47
47
|
return item.value;
|
|
@@ -82,15 +82,15 @@ var TTLCache = class {
|
|
|
82
82
|
this.store.set(key, newItem);
|
|
83
83
|
this.tail = key;
|
|
84
84
|
if (this.size > this.capacity) {
|
|
85
|
-
this.
|
|
85
|
+
this.del(this.head);
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
88
|
/**
|
|
89
89
|
* 移除节点
|
|
90
90
|
* @param key
|
|
91
91
|
*/
|
|
92
|
-
|
|
93
|
-
if (!this.store.has(key)) return;
|
|
92
|
+
del(key) {
|
|
93
|
+
if (!this.store.has(key)) return false;
|
|
94
94
|
const curItem = this.store.get(key);
|
|
95
95
|
if (this.size === 1) {
|
|
96
96
|
this.head = this.tail = null;
|
|
@@ -105,6 +105,7 @@ var TTLCache = class {
|
|
|
105
105
|
curItem.next.pre = curItem.pre;
|
|
106
106
|
}
|
|
107
107
|
this.store.delete(key);
|
|
108
|
+
return true;
|
|
108
109
|
}
|
|
109
110
|
/**
|
|
110
111
|
* 将节点移动到队尾,队尾的节点一定是最后一个更新的
|
|
@@ -114,7 +115,7 @@ var TTLCache = class {
|
|
|
114
115
|
if (!this.store.has(key)) return;
|
|
115
116
|
if (this.tail === key) return;
|
|
116
117
|
const curItem = this.store.get(key);
|
|
117
|
-
this.
|
|
118
|
+
this.del(key);
|
|
118
119
|
const curTail = this.store.get(this.tail);
|
|
119
120
|
curTail.next = curItem;
|
|
120
121
|
curItem.pre = curTail;
|