fast-ttl-cache 0.0.3 → 0.0.5

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
@@ -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/dist/index.js ADDED
@@ -0,0 +1,148 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/index.mjs
20
+ var index_exports = {};
21
+ __export(index_exports, {
22
+ default: () => TTLCache
23
+ });
24
+ module.exports = __toCommonJS(index_exports);
25
+ var TTLCache = class {
26
+ /**
27
+ * 构造函数
28
+ * @param options 配置选项,包含ttl(过期时间)和capacity(容量)
29
+ */
30
+ constructor(options = {}) {
31
+ this.ttl = options.ttl || Infinity;
32
+ this.capacity = options.capacity || Infinity;
33
+ this.store = /* @__PURE__ */ new Map();
34
+ this.head = this.tail = null;
35
+ this.size = 0;
36
+ Object.defineProperties(this, {
37
+ size: {
38
+ get() {
39
+ return this.store.size;
40
+ },
41
+ configurable: false
42
+ },
43
+ store: {
44
+ configurable: false,
45
+ enumerable: false,
46
+ writable: false
47
+ },
48
+ head: {
49
+ configurable: false,
50
+ enumerable: false
51
+ },
52
+ tail: {
53
+ configurable: false,
54
+ enumerable: false
55
+ }
56
+ });
57
+ }
58
+ /**
59
+ * 获取缓存,惰性删除
60
+ * @param key 缓存键
61
+ * @returns 如果缓存存在且未过期返回值,否则返回null
62
+ */
63
+ get(key) {
64
+ const item = this.store.get(key);
65
+ if (!item) return null;
66
+ if (Date.now() - item.time > this.ttl) {
67
+ this.removeItem(key);
68
+ return null;
69
+ }
70
+ return item.value;
71
+ }
72
+ /**
73
+ * 设置缓存,包含已存在或新增
74
+ * @param key 缓存键
75
+ * @param value 缓存值
76
+ */
77
+ put(key, value) {
78
+ if (this.size === 0) {
79
+ this.store.set(key, {
80
+ key,
81
+ value,
82
+ pre: null,
83
+ next: null,
84
+ time: Date.now()
85
+ });
86
+ this.head = this.tail = key;
87
+ return;
88
+ }
89
+ 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);
94
+ return;
95
+ }
96
+ const curTail = this.store.get(this.tail);
97
+ const newItem = {
98
+ key,
99
+ value,
100
+ pre: curTail,
101
+ next: null,
102
+ time: Date.now()
103
+ };
104
+ curTail.next = newItem;
105
+ this.store.set(key, newItem);
106
+ this.tail = key;
107
+ if (this.size > this.capacity) {
108
+ this.removeItem(this.head);
109
+ }
110
+ }
111
+ /**
112
+ * 移除节点
113
+ * @param key
114
+ */
115
+ removeItem(key) {
116
+ if (!this.store.has(key)) return;
117
+ const curItem = this.store.get(key);
118
+ if (this.size === 1) {
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;
126
+ } else {
127
+ curItem.pre.next = curItem.next;
128
+ curItem.next.pre = curItem.pre;
129
+ }
130
+ this.store.delete(key);
131
+ }
132
+ /**
133
+ * 将节点移动到队尾,队尾的节点一定是最后一个更新的
134
+ * @param key
135
+ */
136
+ moveToTail(key) {
137
+ if (!this.store.has(key)) return;
138
+ if (this.tail === key) return;
139
+ const curItem = this.store.get(key);
140
+ this.removeItem(key);
141
+ const curTail = this.store.get(this.tail);
142
+ curTail.next = curItem;
143
+ curItem.pre = curTail;
144
+ curItem.next = null;
145
+ this.tail = key;
146
+ this.store.set(key, curItem);
147
+ }
148
+ };
@@ -1,5 +1,5 @@
1
-
2
- export default class TTLCache {
1
+ // src/index.mjs
2
+ var TTLCache = class {
3
3
  /**
4
4
  * 构造函数
5
5
  * @param options 配置选项,包含ttl(过期时间)和capacity(容量)
@@ -7,14 +7,29 @@ export default class TTLCache {
7
7
  constructor(options = {}) {
8
8
  this.ttl = options.ttl || Infinity;
9
9
  this.capacity = options.capacity || Infinity;
10
- this.store = new Map();
10
+ this.store = /* @__PURE__ */ new Map();
11
11
  this.head = this.tail = null;
12
12
  this.size = 0;
13
- Object.defineProperty(this, 'size', {
14
- get() {
15
- return this.store.size;
13
+ Object.defineProperties(this, {
14
+ size: {
15
+ get() {
16
+ return this.store.size;
17
+ },
18
+ configurable: false
16
19
  },
17
- configurable: false
20
+ store: {
21
+ configurable: false,
22
+ enumerable: false,
23
+ writable: false
24
+ },
25
+ head: {
26
+ configurable: false,
27
+ enumerable: false
28
+ },
29
+ tail: {
30
+ configurable: false,
31
+ enumerable: false
32
+ }
18
33
  });
19
34
  }
20
35
  /**
@@ -108,4 +123,6 @@ export default class TTLCache {
108
123
  this.store.set(key, curItem);
109
124
  }
110
125
  };
111
-
126
+ export {
127
+ TTLCache as default
128
+ };
package/package.json CHANGED
@@ -1,13 +1,30 @@
1
1
  {
2
2
  "name": "fast-ttl-cache",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "ttl cache with capacity support use no timer",
5
- "module": "./index.mjs",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
6
7
  "exports": {
7
8
  ".": {
8
- "import": "./index.mjs"
9
+ "require": "./dist/index.js",
10
+ "import": "./dist/index.mjs"
9
11
  }
10
12
  },
13
+ "scripts": {
14
+ "build": "tsup src/ --format cjs,esm",
15
+ "prepublishOnly": "npm run build"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/xincici/fast-ttl-cache"
20
+ },
11
21
  "author": "linye<llxy8687@foxmail.com>",
12
- "license": "MIT"
22
+ "license": "MIT",
23
+ "devDependencies": {
24
+ "tsup": "^8.5.0",
25
+ "typescript": "^5.9.2"
26
+ },
27
+ "files": [
28
+ "dist/"
29
+ ]
13
30
  }
package/test.mjs DELETED
@@ -1,36 +0,0 @@
1
-
2
- import TTLCache from './index.mjs';
3
-
4
- const sleep = ms => new Promise(res => setTimeout(res, ms));
5
-
6
- const c = new TTLCache({
7
- ttl: 1000,
8
- capacity: 3,
9
- });
10
-
11
- c.put('a', 'aaaa');
12
- c.put('a', 'aaaaaa');
13
-
14
- await sleep(200);
15
- c.put('b', 'bbbb');
16
-
17
- await sleep(500);
18
- c.put('c', 'cccc');
19
-
20
- await sleep(500);
21
- c.put('b', 'bbbbbb');
22
-
23
- await sleep(600);
24
- c.put('d', 'dddd');
25
-
26
- console.log('b', c.get('b'));
27
-
28
- await sleep(600);
29
- c.put('e', 'eeee');
30
-
31
- console.log(c);
32
- console.log(c.store.keys());
33
- console.log('b', c.get('b'));
34
- console.log('c', c.get('c'));
35
- console.log('a', c.get('a'));
36
- console.log('e', c.get('e'));