fast-ttl-cache 0.0.6 → 0.0.8

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 CHANGED
@@ -48,10 +48,6 @@ cache.size; // return 0
48
48
 
49
49
  - Get the value of the key from cache, return null if the key is not exists or has been expired.
50
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.
54
-
55
51
  ```FastTTLCache.prototype.size```
56
52
 
57
53
  - 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.
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: () => TTLCache
22
+ default: () => FastTTLCache
23
23
  });
24
24
  module.exports = __toCommonJS(index_exports);
25
- var TTLCache = class {
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.del(key);
67
+ this.del(item);
68
68
  return null;
69
69
  }
70
70
  return item.value;
@@ -76,74 +76,73 @@ var TTLCache = class {
76
76
  */
77
77
  put(key, value) {
78
78
  if (this.size === 0) {
79
- this.store.set(key, {
79
+ const item2 = {
80
80
  key,
81
81
  value,
82
- pre: null,
82
+ prev: null,
83
83
  next: null,
84
84
  time: Date.now()
85
- });
86
- this.head = this.tail = key;
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 curItem = this.store.get(key);
91
- curItem.value = value;
92
- curItem.time = Date.now();
93
- this.moveToTail(key);
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 curTail = this.store.get(this.tail);
97
- const newItem = {
97
+ const item = {
98
98
  key,
99
99
  value,
100
- pre: curTail,
100
+ prev: this.tail,
101
101
  next: null,
102
102
  time: Date.now()
103
103
  };
104
- curTail.next = newItem;
105
- this.store.set(key, newItem);
106
- this.tail = key;
104
+ this.tail.next = item;
105
+ this.tail = item;
106
+ this.store.set(key, item);
107
107
  if (this.size > this.capacity) {
108
108
  this.del(this.head);
109
109
  }
110
110
  }
111
111
  /**
112
112
  * 移除节点
113
- * @param key
113
+ * @param item
114
114
  */
115
- del(key) {
115
+ del(item) {
116
+ const key = item.key;
116
117
  if (!this.store.has(key)) return false;
117
- const curItem = this.store.get(key);
118
118
  if (this.size === 1) {
119
119
  this.head = this.tail = null;
120
- } else if (this.head === key) {
121
- this.head = curItem.next.key;
122
- curItem.next.pre = null;
123
- } else if (this.tail === key) {
124
- this.tail = curItem.pre.key;
125
- curItem.pre.next = null;
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
- curItem.pre.next = curItem.next;
128
- curItem.next.pre = curItem.pre;
127
+ item.prev.next = item.next;
128
+ item.next.prev = item.prev;
129
129
  }
130
130
  this.store.delete(key);
131
131
  return true;
132
132
  }
133
133
  /**
134
134
  * 将节点移动到队尾,队尾的节点一定是最后一个更新的
135
- * @param key
135
+ * @param item
136
136
  */
137
- moveToTail(key) {
137
+ moveToTail(item) {
138
+ const key = item.key;
138
139
  if (!this.store.has(key)) return;
139
- if (this.tail === key) return;
140
- const curItem = this.store.get(key);
141
- this.del(key);
142
- const curTail = this.store.get(this.tail);
143
- curTail.next = curItem;
144
- curItem.pre = curTail;
145
- curItem.next = null;
146
- this.tail = key;
147
- 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);
148
147
  }
149
148
  };
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/index.mjs
2
- var TTLCache = class {
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.del(key);
44
+ this.del(item);
45
45
  return null;
46
46
  }
47
47
  return item.value;
@@ -53,77 +53,76 @@ var TTLCache = class {
53
53
  */
54
54
  put(key, value) {
55
55
  if (this.size === 0) {
56
- this.store.set(key, {
56
+ const item2 = {
57
57
  key,
58
58
  value,
59
- pre: null,
59
+ prev: null,
60
60
  next: null,
61
61
  time: Date.now()
62
- });
63
- this.head = this.tail = key;
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 curItem = this.store.get(key);
68
- curItem.value = value;
69
- curItem.time = Date.now();
70
- this.moveToTail(key);
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 curTail = this.store.get(this.tail);
74
- const newItem = {
74
+ const item = {
75
75
  key,
76
76
  value,
77
- pre: curTail,
77
+ prev: this.tail,
78
78
  next: null,
79
79
  time: Date.now()
80
80
  };
81
- curTail.next = newItem;
82
- this.store.set(key, newItem);
83
- this.tail = key;
81
+ this.tail.next = item;
82
+ this.tail = item;
83
+ this.store.set(key, item);
84
84
  if (this.size > this.capacity) {
85
85
  this.del(this.head);
86
86
  }
87
87
  }
88
88
  /**
89
89
  * 移除节点
90
- * @param key
90
+ * @param item
91
91
  */
92
- del(key) {
92
+ del(item) {
93
+ const key = item.key;
93
94
  if (!this.store.has(key)) return false;
94
- const curItem = this.store.get(key);
95
95
  if (this.size === 1) {
96
96
  this.head = this.tail = null;
97
- } else if (this.head === key) {
98
- this.head = curItem.next.key;
99
- curItem.next.pre = null;
100
- } else if (this.tail === key) {
101
- this.tail = curItem.pre.key;
102
- curItem.pre.next = null;
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
- curItem.pre.next = curItem.next;
105
- curItem.next.pre = curItem.pre;
104
+ item.prev.next = item.next;
105
+ item.next.prev = item.prev;
106
106
  }
107
107
  this.store.delete(key);
108
108
  return true;
109
109
  }
110
110
  /**
111
111
  * 将节点移动到队尾,队尾的节点一定是最后一个更新的
112
- * @param key
112
+ * @param item
113
113
  */
114
- moveToTail(key) {
114
+ moveToTail(item) {
115
+ const key = item.key;
115
116
  if (!this.store.has(key)) return;
116
- if (this.tail === key) return;
117
- const curItem = this.store.get(key);
118
- this.del(key);
119
- const curTail = this.store.get(this.tail);
120
- curTail.next = curItem;
121
- curItem.pre = curTail;
122
- curItem.next = null;
123
- this.tail = key;
124
- 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);
125
124
  }
126
125
  };
127
126
  export {
128
- TTLCache as default
127
+ FastTTLCache as default
129
128
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-ttl-cache",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "ttl cache with capacity support use no timer",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",