fast-ttl-cache 0.0.3 → 0.0.4

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.
Files changed (3) hide show
  1. package/README.md +2 -2
  2. package/index.mjs +31 -6
  3. package/package.json +5 -1
package/README.md CHANGED
@@ -35,7 +35,7 @@ cache.size; // return 0
35
35
  ```
36
36
 
37
37
  ## API
38
- ``` FastTTLCache(options) consturctor```
38
+ ```FastTTLCache(options) consturctor```
39
39
 
40
40
  options.ttl: number of millseconds, defaults to Infinity
41
41
  options.capacity: number of max capacity, defaults to Infinity
@@ -48,7 +48,7 @@ Add or update the value into cache with key and timestamp.
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.size```
51
+ ```FastTTLCache.prototype.size```
52
52
 
53
53
  return the current size of cache.
54
54
 
package/index.mjs CHANGED
@@ -17,6 +17,7 @@ export default class TTLCache {
17
17
  configurable: false
18
18
  });
19
19
  }
20
+
20
21
  /**
21
22
  * 获取缓存,惰性删除
22
23
  * @param key 缓存键
@@ -25,70 +26,85 @@ export default class TTLCache {
25
26
  get(key) {
26
27
  const item = this.store.get(key);
27
28
  if (!item) return null;
29
+ // 检查缓存是否过期,过期则删除
28
30
  if (Date.now() - item.time > this.ttl) {
29
31
  this.removeItem(key);
30
32
  return null;
31
33
  }
32
34
  return item.value;
33
35
  }
36
+
34
37
  /**
35
38
  * 设置缓存,包含已存在或新增
36
39
  * @param key 缓存键
37
40
  * @param value 缓存值
38
41
  */
39
42
  put(key, value) {
43
+ // 缓存为空时的处理
40
44
  if (this.size === 0) {
41
45
  this.store.set(key, {
42
46
  key,
43
47
  value,
44
48
  pre: null,
45
49
  next: null,
46
- time: Date.now()
50
+ time: Date.now(),
47
51
  });
48
52
  this.head = this.tail = key;
49
53
  return;
50
54
  }
55
+
56
+ // 数据已存在时的处理
51
57
  if (this.store.has(key)) {
58
+ // 更新节点的值和时间戳
52
59
  const curItem = this.store.get(key);
53
60
  curItem.value = value;
54
61
  curItem.time = Date.now();
62
+
63
+ // 移动到尾部
55
64
  this.moveToTail(key);
56
65
  return;
57
66
  }
67
+
68
+ // 新数据插入尾部
58
69
  const curTail = this.store.get(this.tail);
59
70
  const newItem = {
60
71
  key,
61
72
  value,
62
73
  pre: curTail,
63
74
  next: null,
64
- time: Date.now()
75
+ time: Date.now(),
65
76
  };
66
77
  curTail.next = newItem;
67
78
  this.store.set(key, newItem);
68
79
  this.tail = key;
80
+
81
+ // 超出容量时,删除最久未更新的节点(头部节点)
69
82
  if (this.size > this.capacity) {
70
83
  this.removeItem(this.head);
71
84
  }
72
85
  }
86
+
73
87
  /**
74
88
  * 移除节点
75
89
  * @param key
76
90
  */
77
91
  removeItem(key) {
78
92
  if (!this.store.has(key)) return;
93
+ // 获取当前节点
79
94
  const curItem = this.store.get(key);
80
- if (this.size === 1) {
95
+ if (this.size === 1) { // 只有一个节点的情况
81
96
  this.head = this.tail = null;
82
- } else if (this.head === key) {
97
+ } else if (this.head === key) { // 处理头部节点的情况
83
98
  this.head = curItem.next.key;
84
99
  curItem.next.pre = null;
85
- } else if (this.tail === key) {
100
+ } else if (this.tail === key) { // 处理尾部节点的情况
86
101
  this.tail = curItem.pre.key;
87
102
  curItem.pre.next = null;
88
- } else {
103
+ } else { // 处理中间节点的情况
89
104
  curItem.pre.next = curItem.next;
90
105
  curItem.next.pre = curItem.pre;
91
106
  }
107
+ // 从 store 里删除节点
92
108
  this.store.delete(key);
93
109
  }
94
110
  /**
@@ -97,14 +113,23 @@ export default class TTLCache {
97
113
  */
98
114
  moveToTail(key) {
99
115
  if (!this.store.has(key)) return;
116
+ // 当前已经是尾部节点了
100
117
  if (this.tail === key) return;
118
+
119
+ // 获取当前节点
101
120
  const curItem = this.store.get(key);
121
+ // 先移除节点
102
122
  this.removeItem(key);
123
+
124
+ // 获取队尾节点
103
125
  const curTail = this.store.get(this.tail);
126
+
127
+ // 将节点移动到队尾
104
128
  curTail.next = curItem;
105
129
  curItem.pre = curTail;
106
130
  curItem.next = null;
107
131
  this.tail = key;
132
+ // 设置缓存
108
133
  this.store.set(key, curItem);
109
134
  }
110
135
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-ttl-cache",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "ttl cache with capacity support use no timer",
5
5
  "module": "./index.mjs",
6
6
  "exports": {
@@ -8,6 +8,10 @@
8
8
  "import": "./index.mjs"
9
9
  }
10
10
  },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/xincici/fast-ttl-cache"
14
+ },
11
15
  "author": "linye<llxy8687@foxmail.com>",
12
16
  "license": "MIT"
13
17
  }