fast-ttl-cache 0.0.1 → 0.0.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 yelin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,52 @@
1
+
2
+
3
+ > Written with [StackEdit](https://stackedit.io/).
4
+
5
+ # fast-ttl-cache
6
+
7
+ An in-memory cache implemented in Node.js, featuring configurable ttl and a maximum size. It uses a doubly linked list data structure and lazy deletion for expired data, all without relying on any JavaScript timer methods.
8
+
9
+ ## Installation
10
+
11
+ ```shell
12
+ npm install fast-ttl-cache --save
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```javascript
18
+ import FastTTLCache from 'fast-ttl-cache';
19
+
20
+ const cache = new FastTTLCache({
21
+ ttl: 5 * 1000, // ttl in millseconds, get an outdated data will return null and delete it
22
+ capacity: 1000, // max capacity, When the capacity is exceeded, the least recently updated data will be removed.
23
+ });
24
+
25
+ cache.put('key1', 'value1');
26
+ cache.put('key2', 'value2');
27
+ cache.get('key2'); // return value2
28
+
29
+ // wait for 5 seconds
30
+ cache.get('key1'); // return null and key1 will be removed
31
+ cache.size; // return 1, key2 is outdated but is not been removed yet
32
+
33
+ cache.get('key2'); // return null and key2 will be removed
34
+ cache.size; // return 0
35
+ ```
36
+
37
+ ## API
38
+ ``` FastTTLCache(options) consturctor```
39
+ options.ttl: number of millseconds, defaults to Infinity
40
+ options.capacity: number of max capacity, defaults to Infinity
41
+
42
+ ```FastTTLCache.prototype.put(key, value)```
43
+ Add or update the value into cache with key and timestamp.
44
+
45
+ ```FastTTLCache.prototype.get(key)```
46
+ Get the value of the key from cache, return null if the key is not exists or has been expired.
47
+
48
+ ``` FastTTLCache.prototype.size```
49
+ return the current size of cache.
50
+
51
+ ## License
52
+ MIT
package/index.mjs CHANGED
@@ -22,7 +22,7 @@ export default class TTLCache {
22
22
  * @param key 缓存键
23
23
  * @returns 如果缓存存在且未过期返回值,否则返回null
24
24
  */
25
- getToken(key) {
25
+ get(key) {
26
26
  const item = this.store.get(key);
27
27
  if (!item) return null;
28
28
  if (Date.now() - item.time > this.ttl) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-ttl-cache",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "ttl cache with capacity support use no timer",
5
5
  "type": "module",
6
6
  "module": "index.mjs",
package/test.mjs CHANGED
@@ -23,14 +23,14 @@ c.put('b', 'bbbbbb');
23
23
  await sleep(600);
24
24
  c.put('d', 'dddd');
25
25
 
26
- console.log('b', c.getToken('b'));
26
+ console.log('b', c.get('b'));
27
27
 
28
28
  await sleep(600);
29
29
  c.put('e', 'eeee');
30
30
 
31
31
  console.log(c);
32
32
  console.log(c.store.keys());
33
- console.log('b', c.getToken('b'));
34
- console.log('c', c.getToken('c'));
35
- console.log('a', c.getToken('a'));
36
- console.log('e', c.getToken('e'));
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'));