fast-ttl-cache 0.0.5 → 0.0.7
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 +41 -41
- package/dist/index.mjs +41 -41
- 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
|
@@ -19,10 +19,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
// src/index.mjs
|
|
20
20
|
var index_exports = {};
|
|
21
21
|
__export(index_exports, {
|
|
22
|
-
default: () =>
|
|
22
|
+
default: () => FastTTLCache
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(index_exports);
|
|
25
|
-
var
|
|
25
|
+
var FastTTLCache = class {
|
|
26
26
|
/**
|
|
27
27
|
* 构造函数
|
|
28
28
|
* @param options 配置选项,包含ttl(过期时间)和capacity(容量)
|
|
@@ -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(item);
|
|
68
68
|
return null;
|
|
69
69
|
}
|
|
70
70
|
return item.value;
|
|
@@ -76,73 +76,73 @@ var TTLCache = class {
|
|
|
76
76
|
*/
|
|
77
77
|
put(key, value) {
|
|
78
78
|
if (this.size === 0) {
|
|
79
|
-
|
|
79
|
+
const item2 = {
|
|
80
80
|
key,
|
|
81
81
|
value,
|
|
82
|
-
|
|
82
|
+
prev: null,
|
|
83
83
|
next: null,
|
|
84
84
|
time: Date.now()
|
|
85
|
-
}
|
|
86
|
-
this.head = this.tail =
|
|
85
|
+
};
|
|
86
|
+
this.head = this.tail = item2;
|
|
87
|
+
this.store.set(key, item2);
|
|
87
88
|
return;
|
|
88
89
|
}
|
|
89
90
|
if (this.store.has(key)) {
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
this.moveToTail(
|
|
91
|
+
const item2 = this.store.get(key);
|
|
92
|
+
item2.value = value;
|
|
93
|
+
item2.time = Date.now();
|
|
94
|
+
this.moveToTail(item2);
|
|
94
95
|
return;
|
|
95
96
|
}
|
|
96
|
-
const
|
|
97
|
-
const newItem = {
|
|
97
|
+
const item = {
|
|
98
98
|
key,
|
|
99
99
|
value,
|
|
100
|
-
|
|
100
|
+
prev: this.tail,
|
|
101
101
|
next: null,
|
|
102
102
|
time: Date.now()
|
|
103
103
|
};
|
|
104
|
-
|
|
105
|
-
this.
|
|
106
|
-
this.
|
|
104
|
+
this.tail.next = item;
|
|
105
|
+
this.tail = item;
|
|
106
|
+
this.store.set(key, item);
|
|
107
107
|
if (this.size > this.capacity) {
|
|
108
|
-
this.
|
|
108
|
+
this.del(this.head);
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
111
|
/**
|
|
112
112
|
* 移除节点
|
|
113
|
-
* @param
|
|
113
|
+
* @param item
|
|
114
114
|
*/
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
115
|
+
del(item) {
|
|
116
|
+
const key = item.key;
|
|
117
|
+
if (!this.store.has(key)) return false;
|
|
118
118
|
if (this.size === 1) {
|
|
119
119
|
this.head = this.tail = null;
|
|
120
|
-
} else if (this.head ===
|
|
121
|
-
this.head =
|
|
122
|
-
|
|
123
|
-
} else if (this.tail ===
|
|
124
|
-
this.tail =
|
|
125
|
-
|
|
120
|
+
} else if (this.head === item) {
|
|
121
|
+
this.head = item.next;
|
|
122
|
+
this.head.prev = null;
|
|
123
|
+
} else if (this.tail === item) {
|
|
124
|
+
this.tail = item.prev;
|
|
125
|
+
this.tail.next = null;
|
|
126
126
|
} else {
|
|
127
|
-
|
|
128
|
-
|
|
127
|
+
item.prev.next = item.next;
|
|
128
|
+
item.next.prev = item.prev;
|
|
129
129
|
}
|
|
130
130
|
this.store.delete(key);
|
|
131
|
+
return true;
|
|
131
132
|
}
|
|
132
133
|
/**
|
|
133
134
|
* 将节点移动到队尾,队尾的节点一定是最后一个更新的
|
|
134
|
-
* @param
|
|
135
|
+
* @param item
|
|
135
136
|
*/
|
|
136
|
-
moveToTail(
|
|
137
|
+
moveToTail(item) {
|
|
138
|
+
const key = item.key;
|
|
137
139
|
if (!this.store.has(key)) return;
|
|
138
|
-
if (this.tail ===
|
|
139
|
-
|
|
140
|
-
this.
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
this.tail = key;
|
|
146
|
-
this.store.set(key, curItem);
|
|
140
|
+
if (this.tail === item) return;
|
|
141
|
+
this.del(item);
|
|
142
|
+
this.tail.next = item;
|
|
143
|
+
item.prev = this.tail;
|
|
144
|
+
item.next = null;
|
|
145
|
+
this.tail = item;
|
|
146
|
+
this.store.set(key, item);
|
|
147
147
|
}
|
|
148
148
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/index.mjs
|
|
2
|
-
var
|
|
2
|
+
var FastTTLCache = class {
|
|
3
3
|
/**
|
|
4
4
|
* 构造函数
|
|
5
5
|
* @param options 配置选项,包含ttl(过期时间)和capacity(容量)
|
|
@@ -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(item);
|
|
45
45
|
return null;
|
|
46
46
|
}
|
|
47
47
|
return item.value;
|
|
@@ -53,76 +53,76 @@ var TTLCache = class {
|
|
|
53
53
|
*/
|
|
54
54
|
put(key, value) {
|
|
55
55
|
if (this.size === 0) {
|
|
56
|
-
|
|
56
|
+
const item2 = {
|
|
57
57
|
key,
|
|
58
58
|
value,
|
|
59
|
-
|
|
59
|
+
prev: null,
|
|
60
60
|
next: null,
|
|
61
61
|
time: Date.now()
|
|
62
|
-
}
|
|
63
|
-
this.head = this.tail =
|
|
62
|
+
};
|
|
63
|
+
this.head = this.tail = item2;
|
|
64
|
+
this.store.set(key, item2);
|
|
64
65
|
return;
|
|
65
66
|
}
|
|
66
67
|
if (this.store.has(key)) {
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
this.moveToTail(
|
|
68
|
+
const item2 = this.store.get(key);
|
|
69
|
+
item2.value = value;
|
|
70
|
+
item2.time = Date.now();
|
|
71
|
+
this.moveToTail(item2);
|
|
71
72
|
return;
|
|
72
73
|
}
|
|
73
|
-
const
|
|
74
|
-
const newItem = {
|
|
74
|
+
const item = {
|
|
75
75
|
key,
|
|
76
76
|
value,
|
|
77
|
-
|
|
77
|
+
prev: this.tail,
|
|
78
78
|
next: null,
|
|
79
79
|
time: Date.now()
|
|
80
80
|
};
|
|
81
|
-
|
|
82
|
-
this.
|
|
83
|
-
this.
|
|
81
|
+
this.tail.next = item;
|
|
82
|
+
this.tail = item;
|
|
83
|
+
this.store.set(key, item);
|
|
84
84
|
if (this.size > this.capacity) {
|
|
85
|
-
this.
|
|
85
|
+
this.del(this.head);
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
88
|
/**
|
|
89
89
|
* 移除节点
|
|
90
|
-
* @param
|
|
90
|
+
* @param item
|
|
91
91
|
*/
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
92
|
+
del(item) {
|
|
93
|
+
const key = item.key;
|
|
94
|
+
if (!this.store.has(key)) return false;
|
|
95
95
|
if (this.size === 1) {
|
|
96
96
|
this.head = this.tail = null;
|
|
97
|
-
} else if (this.head ===
|
|
98
|
-
this.head =
|
|
99
|
-
|
|
100
|
-
} else if (this.tail ===
|
|
101
|
-
this.tail =
|
|
102
|
-
|
|
97
|
+
} else if (this.head === item) {
|
|
98
|
+
this.head = item.next;
|
|
99
|
+
this.head.prev = null;
|
|
100
|
+
} else if (this.tail === item) {
|
|
101
|
+
this.tail = item.prev;
|
|
102
|
+
this.tail.next = null;
|
|
103
103
|
} else {
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
item.prev.next = item.next;
|
|
105
|
+
item.next.prev = item.prev;
|
|
106
106
|
}
|
|
107
107
|
this.store.delete(key);
|
|
108
|
+
return true;
|
|
108
109
|
}
|
|
109
110
|
/**
|
|
110
111
|
* 将节点移动到队尾,队尾的节点一定是最后一个更新的
|
|
111
|
-
* @param
|
|
112
|
+
* @param item
|
|
112
113
|
*/
|
|
113
|
-
moveToTail(
|
|
114
|
+
moveToTail(item) {
|
|
115
|
+
const key = item.key;
|
|
114
116
|
if (!this.store.has(key)) return;
|
|
115
|
-
if (this.tail ===
|
|
116
|
-
|
|
117
|
-
this.
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
this.tail = key;
|
|
123
|
-
this.store.set(key, curItem);
|
|
117
|
+
if (this.tail === item) return;
|
|
118
|
+
this.del(item);
|
|
119
|
+
this.tail.next = item;
|
|
120
|
+
item.prev = this.tail;
|
|
121
|
+
item.next = null;
|
|
122
|
+
this.tail = item;
|
|
123
|
+
this.store.set(key, item);
|
|
124
124
|
}
|
|
125
125
|
};
|
|
126
126
|
export {
|
|
127
|
-
|
|
127
|
+
FastTTLCache as default
|
|
128
128
|
};
|