fast-map-cache 1.0.1
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 +21 -0
- package/README.md +175 -0
- package/README_zh-CN.md +175 -0
- package/dist/main.d.mts +122 -0
- package/dist/main.d.ts +122 -0
- package/dist/main.js +376 -0
- package/dist/main.mjs +327 -0
- package/package.json +95 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 crper (crper@outlook.com)
|
|
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,175 @@
|
|
|
1
|
+
[简体中文](README_zh-CN.md) | English
|
|
2
|
+
|
|
3
|
+
# Fast Map Cache
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/fast-map-cache)
|
|
6
|
+
[](https://www.npmjs.com/package/fast-map-cache)
|
|
7
|
+
[](https://github.com/crper/fast-map-cache/blob/main/LICENSE)
|
|
8
|
+
[](https://github.com/crper/fast-map-cache/actions/workflows/test.yml)
|
|
9
|
+
|
|
10
|
+
A high-performance in-memory LRU cache for Node.js and browsers, implemented with a `Map` and a doubly linked list to achieve **O(1)** time complexity for all core operations.
|
|
11
|
+
|
|
12
|
+
## ✨ Performance Highlights
|
|
13
|
+
|
|
14
|
+
In real-world scenarios, `fast-map-cache` demonstrates significant performance gains by preventing expensive operations:
|
|
15
|
+
|
|
16
|
+
- **🚀 Proven Performance**: Delivers a **2x to 3x** performance boost in real-world I/O and CPU-bound scenarios by preventing expensive operations.
|
|
17
|
+
|
|
18
|
+
For a detailed analysis, see the full [**Performance Report**](./docs/performance-report.md).
|
|
19
|
+
|
|
20
|
+
## Features
|
|
21
|
+
|
|
22
|
+
- **🚀 High Performance**: O(1) time complexity for `get`, `set`, and `delete`.
|
|
23
|
+
- **🛡️ LRU Strategy**: Automatically evicts the least recently used items when the cache is full.
|
|
24
|
+
- **⏱️ TTL Support**: `FastCacheWithTTL` provides support for time-to-live expiration of cache items.
|
|
25
|
+
- **💪 Type-Safe**: Written entirely in TypeScript with zero dependencies.
|
|
26
|
+
- **🌐 Universal**: Works in both Node.js and browser environments.
|
|
27
|
+
- **📊 Stats Tracking**: Built-in tracking for hits, misses, and hit rate.
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
# pnpm (recommended)
|
|
33
|
+
pnpm add fast-map-cache
|
|
34
|
+
|
|
35
|
+
# npm
|
|
36
|
+
npm install fast-map-cache
|
|
37
|
+
|
|
38
|
+
# yarn
|
|
39
|
+
yarn add fast-map-cache
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Quick Start
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { createCache } from 'fast-map-cache'
|
|
46
|
+
|
|
47
|
+
// Create a cache with a maximum size of 2
|
|
48
|
+
const cache = createCache<string, number>(2)
|
|
49
|
+
|
|
50
|
+
cache.set('a', 1)
|
|
51
|
+
cache.set('b', 2)
|
|
52
|
+
|
|
53
|
+
console.log(cache.get('a')) // Output: 1
|
|
54
|
+
|
|
55
|
+
// Adding 'c' will evict the least recently used item ('b')
|
|
56
|
+
cache.set('c', 3)
|
|
57
|
+
|
|
58
|
+
console.log(cache.get('b')) // Output: undefined
|
|
59
|
+
console.log(cache.size) // Output: 2
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Usage with TTL
|
|
63
|
+
|
|
64
|
+
For caches that require items to expire after a certain period, use `createCacheWithTTL`.
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { createCacheWithTTL } from 'fast-map-cache'
|
|
68
|
+
|
|
69
|
+
const cache = createCacheWithTTL<string, string>({
|
|
70
|
+
maxSize: 100,
|
|
71
|
+
ttl: 5000, // 5 seconds
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
cache.set('key', 'value')
|
|
75
|
+
|
|
76
|
+
setTimeout(() => {
|
|
77
|
+
console.log(cache.get('key')) // Output: undefined
|
|
78
|
+
}, 6000)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### ❗ Important Note for Node.js Users
|
|
82
|
+
|
|
83
|
+
When using `FastCacheWithTTL` with the `autoCleanup: true` option in a Node.js environment, a `setInterval` is used to periodically clean up expired items. This timer will prevent the Node.js process from exiting gracefully on its own.
|
|
84
|
+
|
|
85
|
+
**You must manually call the `destroy()` method before your application exits to clear the timer.**
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
const cache = createCacheWithTTL({ maxSize: 100, autoCleanup: true, ttl: 60000 })
|
|
89
|
+
|
|
90
|
+
// ... your application logic ...
|
|
91
|
+
|
|
92
|
+
// Before shutting down your application:
|
|
93
|
+
cache.destroy()
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
For more advanced use cases, including batch operations and presets, please see the full example file in [`examples/01-basic-example.ts`](./examples/01-basic-example.ts). You can run it directly with `pnpm run:example`.
|
|
97
|
+
|
|
98
|
+
## API Reference
|
|
99
|
+
|
|
100
|
+
The cache instance, created by `createCache` or `createCacheWithTTL`, implements the `IFastCache` interface.
|
|
101
|
+
|
|
102
|
+
### `get(key: K): V | undefined`
|
|
103
|
+
|
|
104
|
+
Retrieves the value for a given key. Returns `undefined` if the key does not exist or has expired. This operation marks the item as recently used.
|
|
105
|
+
|
|
106
|
+
### `set(key: K, value: V): void`
|
|
107
|
+
|
|
108
|
+
Adds or updates a key-value pair. If the key already exists, its value is updated and it is marked as recently used. If the cache is full, the least recently used item is evicted.
|
|
109
|
+
|
|
110
|
+
> **Note**: While JavaScript's `Map` allows any type as a key, it is recommended to use primitive types (`string`, `number`, `symbol`, `bigint`) for better performance and predictability.
|
|
111
|
+
|
|
112
|
+
### `delete(key: K): boolean`
|
|
113
|
+
|
|
114
|
+
Deletes a key-value pair. Returns `true` if the key existed and was deleted, `false` otherwise.
|
|
115
|
+
|
|
116
|
+
### `has(key: K): boolean`
|
|
117
|
+
|
|
118
|
+
Checks if a key exists in the cache without updating its "recently used" status. Note: For TTL caches, this will lazily delete the item if it's found to be expired, and then return `false`.
|
|
119
|
+
|
|
120
|
+
### `clear(): void`
|
|
121
|
+
|
|
122
|
+
Clears all items from the cache.
|
|
123
|
+
|
|
124
|
+
### `getMany(keys: K[]): Map<K, V>`
|
|
125
|
+
|
|
126
|
+
Retrieves multiple values for an array of keys. Returns a `Map` containing the found key-value pairs.
|
|
127
|
+
|
|
128
|
+
### `setMany(entries: [K, V][]): void`
|
|
129
|
+
|
|
130
|
+
Adds or updates multiple key-value pairs from an array of entries.
|
|
131
|
+
|
|
132
|
+
### `size: number` (getter)
|
|
133
|
+
|
|
134
|
+
Returns the current number of items in the cache.
|
|
135
|
+
|
|
136
|
+
### `capacity: number` (getter)
|
|
137
|
+
|
|
138
|
+
Returns the maximum number of items the cache can hold.
|
|
139
|
+
|
|
140
|
+
### `getStats(): CacheStats`
|
|
141
|
+
|
|
142
|
+
Returns an object with cache statistics:
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
{
|
|
146
|
+
hits: number;
|
|
147
|
+
misses: number;
|
|
148
|
+
hitRate: number; // A value between 0 and 1
|
|
149
|
+
size: number;
|
|
150
|
+
capacity: number;
|
|
151
|
+
expired?: number; // Only for TTL caches
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### `cleanup(): number` (For `FastCacheWithTTL` only)
|
|
156
|
+
|
|
157
|
+
Manually triggers the cleanup of expired items. Returns the number of items that were removed.
|
|
158
|
+
|
|
159
|
+
### `destroy(): void` (For `FastCacheWithTTL` only)
|
|
160
|
+
|
|
161
|
+
Clears the automatic cleanup timer if `autoCleanup` was enabled. **Crucial for graceful shutdown in Node.js.**
|
|
162
|
+
|
|
163
|
+
## Benchmark
|
|
164
|
+
|
|
165
|
+
This library is designed for high performance in real-world scenarios. The core value of a cache is not just the raw speed of its `get`/`set` operations, but its ability to prevent expensive computations or network requests.
|
|
166
|
+
|
|
167
|
+
Our benchmarks show that in realistic I/O-bound and CPU-bound scenarios, `fast-map-cache` provides a **2x to 3x performance boost on average**. The actual improvement depends heavily on the cost of the operation being cached.
|
|
168
|
+
|
|
169
|
+
## Contributing
|
|
170
|
+
|
|
171
|
+
Contributions are welcome! Please see the [Contributing Guide](CONTRIBUTING.md) for details on how to get started.
|
|
172
|
+
|
|
173
|
+
## License
|
|
174
|
+
|
|
175
|
+
This project is licensed under the [MIT License](LICENSE).
|
package/README_zh-CN.md
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
[简体中文](README_zh-CN.md) | [English](README.md)
|
|
2
|
+
|
|
3
|
+
# Fast Map Cache
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/fast-map-cache)
|
|
6
|
+
[](https://www.npmjs.com/package/fast-map-cache)
|
|
7
|
+
[](https://github.com/crper/fast-map-cache/blob/main/LICENSE)
|
|
8
|
+
[](https://github.com/crper/fast-map-cache/actions/workflows/test.yml)
|
|
9
|
+
|
|
10
|
+
一个为 Node.js 和浏览器设计的高性能内存 LRU 缓存库。通过 `Map` 和双向链表实现,所有核心操作的时间复杂度均为 **O(1)**。
|
|
11
|
+
|
|
12
|
+
## ✨ 性能亮点
|
|
13
|
+
|
|
14
|
+
在真实业务场景中,`fast-map-cache` 通过有效避免高成本操作,展现出显著的性能优势:
|
|
15
|
+
|
|
16
|
+
- **🚀 可靠性能**: 在真实的 I/O 和 CPU 密集型场景中,通过避免高成本操作,带来 **2-3 倍** 的性能提升。
|
|
17
|
+
|
|
18
|
+
详细分析请参阅完整的 [**性能报告**](./docs/performance-report.md)。
|
|
19
|
+
|
|
20
|
+
## 功能特性
|
|
21
|
+
|
|
22
|
+
- **🚀 高性能**: `get`, `set`, `delete` 等核心操作的时间复杂度均为 O(1)。
|
|
23
|
+
- **🛡️ LRU 策略**: 缓存满时,自动淘汰最久未被使用的数据。
|
|
24
|
+
- **⏱️ TTL 支持**: `FastCacheWithTTL` 类提供了对缓存项生命周期 (TTL) 的支持。
|
|
25
|
+
- **💪 类型安全**: 完全使用 TypeScript 编写,零外部依赖。
|
|
26
|
+
- **🌐 环境通用**: 同时支持 Node.js 和浏览器环境。
|
|
27
|
+
- **📊 状态追踪**: 内置命中、未命中和命中率的统计功能。
|
|
28
|
+
|
|
29
|
+
## 安装
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
# pnpm (推荐)
|
|
33
|
+
pnpm add fast-map-cache
|
|
34
|
+
|
|
35
|
+
# npm
|
|
36
|
+
npm install fast-map-cache
|
|
37
|
+
|
|
38
|
+
# yarn
|
|
39
|
+
yarn add fast-map-cache
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## 快速上手
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { createCache } from 'fast-map-cache'
|
|
46
|
+
|
|
47
|
+
// 创建一个最大容量为 2 的缓存实例
|
|
48
|
+
const cache = createCache<string, number>(2)
|
|
49
|
+
|
|
50
|
+
cache.set('a', 1)
|
|
51
|
+
cache.set('b', 2)
|
|
52
|
+
|
|
53
|
+
console.log(cache.get('a')) // 输出: 1
|
|
54
|
+
|
|
55
|
+
// 添加 'c' 会淘汰最久未使用的 'b'
|
|
56
|
+
cache.set('c', 3)
|
|
57
|
+
|
|
58
|
+
console.log(cache.get('b')) // 输出: undefined
|
|
59
|
+
console.log(cache.size) // 输出: 2
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
关于更高级的用法(例如批量操作和预设配置),请参阅完整的示例文件:[`examples/01-basic-example.ts`](./examples/01-basic-example.ts)。您可以直接通过 `pnpm run:example` 命令运行它。
|
|
63
|
+
|
|
64
|
+
### 带 TTL 的用法
|
|
65
|
+
|
|
66
|
+
如果缓存项需要在一定时间后过期,请使用 `createCacheWithTTL`。
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
import { createCacheWithTTL } from 'fast-map-cache'
|
|
70
|
+
|
|
71
|
+
const cache = createCacheWithTTL<string, string>({
|
|
72
|
+
maxSize: 100,
|
|
73
|
+
ttl: 5000, // 5 秒
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
cache.set('key', 'value')
|
|
77
|
+
|
|
78
|
+
setTimeout(() => {
|
|
79
|
+
console.log(cache.get('key')) // 输出: undefined
|
|
80
|
+
}, 6000)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### ❗ Node.js 用户重要提示
|
|
84
|
+
|
|
85
|
+
在 Node.js 环境下使用 `FastCacheWithTTL` 并开启 `autoCleanup: true` 选项时,系统会使用 `setInterval` 定时清理过期缓存。这个定时器会阻止 Node.js 进程正常退出。
|
|
86
|
+
|
|
87
|
+
**您必须在程序退出前手动调用 `destroy()` 方法来清除该定时器。**
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
const cache = createCacheWithTTL({ maxSize: 100, autoCleanup: true, ttl: 60000 })
|
|
91
|
+
|
|
92
|
+
// ... 你的应用逻辑 ...
|
|
93
|
+
|
|
94
|
+
// 在应用关闭前:
|
|
95
|
+
cache.destroy()
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## API 参考
|
|
99
|
+
|
|
100
|
+
由 `createCache` 或 `createCacheWithTTL` 创建的缓存实例均实现了 `IFastCache` 接口。
|
|
101
|
+
|
|
102
|
+
### `get(key: K): V | undefined`
|
|
103
|
+
|
|
104
|
+
根据键获取值。如果键不存在或已过期,则返回 `undefined`。此操作会将访问项标记为最近使用。
|
|
105
|
+
|
|
106
|
+
### `set(key: K, value: V): void`
|
|
107
|
+
|
|
108
|
+
添加或更新一个键值对。如果键已存在,则更新其值并将其标记为最近使用。如果缓存已满,则淘汰最久未使用的项。
|
|
109
|
+
|
|
110
|
+
> **注意**:虽然 JavaScript 的 `Map` 允许任何类型作为键,但为了获得最佳的性能和可预测性,推荐使用原始类型 (`string`, `number`, `symbol`, `bigint`)。
|
|
111
|
+
|
|
112
|
+
### `delete(key: K): boolean`
|
|
113
|
+
|
|
114
|
+
删除一个键值对。如果键存在且被成功删除,返回 `true`,否则返回 `false`。
|
|
115
|
+
|
|
116
|
+
### `has(key: K): boolean`
|
|
117
|
+
|
|
118
|
+
检查一个键是否存在于缓存中,此操作**不会**更新其"最近使用"状态。注意:对于带 TTL 的缓存,如果发现该项已过期,会懒惰地删除它,然后返回 `false`。
|
|
119
|
+
|
|
120
|
+
### `clear(): void`
|
|
121
|
+
|
|
122
|
+
清空缓存中的所有项目。
|
|
123
|
+
|
|
124
|
+
### `getMany(keys: K[]): Map<K, V>`
|
|
125
|
+
|
|
126
|
+
根据键数组批量获取值。返回一个包含已找到的键值对的 `Map`。
|
|
127
|
+
|
|
128
|
+
### `setMany(entries: [K, V][]): void`
|
|
129
|
+
|
|
130
|
+
通过一个条目数组批量添加或更新键值对。
|
|
131
|
+
|
|
132
|
+
### `size: number` (getter)
|
|
133
|
+
|
|
134
|
+
返回缓存中当前的项目数量。
|
|
135
|
+
|
|
136
|
+
### `capacity: number` (getter)
|
|
137
|
+
|
|
138
|
+
返回缓存的最大容量。
|
|
139
|
+
|
|
140
|
+
### `getStats(): CacheStats`
|
|
141
|
+
|
|
142
|
+
返回一个包含缓存统计信息的对象:
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
{
|
|
146
|
+
hits: number; // 命中次数
|
|
147
|
+
misses: number; // 未命中次数
|
|
148
|
+
hitRate: number; // 命中率 (0 到 1 之间)
|
|
149
|
+
size: number; // 当前大小
|
|
150
|
+
capacity: number; // 最大容量
|
|
151
|
+
expired?: number; // 已过期数量 (仅限 TTL 缓存)
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### `cleanup(): number` (仅 `FastCacheWithTTL` 可用)
|
|
156
|
+
|
|
157
|
+
手动触发过期项目的清理。返回被移除的项目数量。
|
|
158
|
+
|
|
159
|
+
### `destroy(): void` (仅 `FastCacheWithTTL` 可用)
|
|
160
|
+
|
|
161
|
+
如果开启了 `autoCleanup`,此方法用于清除自动清理定时器。**这对于在 Node.js 中实现优雅停机至关重要。**
|
|
162
|
+
|
|
163
|
+
## 性能基准
|
|
164
|
+
|
|
165
|
+
本库为真实场景下的高性能而设计。缓存的核心价值不仅在于 `get`/`set` 操作的原始速度,更在于它能够有效避免昂贵的计算或网络请求。
|
|
166
|
+
|
|
167
|
+
我们的基准测试显示,在模拟真实世界的 I/O 密集和 CPU 密集场景下,`fast-map-cache` 平均能带来 **2 到 3 倍的性能提升**。实际提升效果取决于被缓存操作的成本。
|
|
168
|
+
|
|
169
|
+
## 贡献
|
|
170
|
+
|
|
171
|
+
欢迎任何形式的贡献!请参阅 [贡献指南](CONTRIBUTING.md) 以了解如何开始。
|
|
172
|
+
|
|
173
|
+
## 许可证
|
|
174
|
+
|
|
175
|
+
本项目采用 [MIT 许可证](LICENSE)。
|
package/dist/main.d.mts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 基础类型定义
|
|
4
|
+
*/
|
|
5
|
+
type CacheKey = string | number | symbol | bigint;
|
|
6
|
+
interface CacheStats {
|
|
7
|
+
hits: number;
|
|
8
|
+
misses: number;
|
|
9
|
+
hitRate: number;
|
|
10
|
+
size: number;
|
|
11
|
+
capacity: number;
|
|
12
|
+
expired?: number;
|
|
13
|
+
}
|
|
14
|
+
interface CacheOptions {
|
|
15
|
+
maxSize: number;
|
|
16
|
+
ttl?: number;
|
|
17
|
+
autoCleanup?: boolean;
|
|
18
|
+
cleanupInterval?: number;
|
|
19
|
+
}
|
|
20
|
+
interface IFastCache<K extends CacheKey, V> {
|
|
21
|
+
get(key: K): V | undefined;
|
|
22
|
+
set(key: K, value: V): void;
|
|
23
|
+
delete(key: K): boolean;
|
|
24
|
+
clear(): void;
|
|
25
|
+
has(key: K): boolean;
|
|
26
|
+
readonly size: number;
|
|
27
|
+
readonly capacity: number;
|
|
28
|
+
setMany(entries: [K, V][]): void;
|
|
29
|
+
getMany(keys: K[]): Map<K, V>;
|
|
30
|
+
getStats(): CacheStats;
|
|
31
|
+
cleanup?(): number;
|
|
32
|
+
}
|
|
33
|
+
interface CacheNode<K extends CacheKey, V> {
|
|
34
|
+
key: K;
|
|
35
|
+
value: V;
|
|
36
|
+
prev: CacheNode<K, V> | null;
|
|
37
|
+
next: CacheNode<K, V> | null;
|
|
38
|
+
timestamp?: number;
|
|
39
|
+
timePrev?: CacheNode<K, V> | null;
|
|
40
|
+
timeNext?: CacheNode<K, V> | null;
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/fast-cache.d.ts
|
|
44
|
+
/**
|
|
45
|
+
* 高性能 LRU 缓存实现
|
|
46
|
+
* 使用 Map + 双向链表实现 O(1) 复杂度的所有操作
|
|
47
|
+
*/
|
|
48
|
+
declare class FastCache<K extends CacheKey, V> implements IFastCache<K, V> {
|
|
49
|
+
protected readonly cache: Map<K, CacheNode<K, V>>;
|
|
50
|
+
protected readonly head: CacheNode<K, V>;
|
|
51
|
+
protected readonly tail: CacheNode<K, V>;
|
|
52
|
+
protected readonly maxSize: number;
|
|
53
|
+
protected hits: number;
|
|
54
|
+
protected misses: number;
|
|
55
|
+
constructor(maxSize: number);
|
|
56
|
+
get(key: K): V | undefined;
|
|
57
|
+
set(key: K, value: V): void;
|
|
58
|
+
delete(key: K): boolean;
|
|
59
|
+
clear(): void;
|
|
60
|
+
has(key: K): boolean;
|
|
61
|
+
get size(): number;
|
|
62
|
+
get capacity(): number;
|
|
63
|
+
setMany(entries: [K, V][]): void;
|
|
64
|
+
getMany(keys: K[]): Map<K, V>;
|
|
65
|
+
getStats(): CacheStats;
|
|
66
|
+
protected addToHead(node: CacheNode<K, V>): void;
|
|
67
|
+
protected removeNode(node: CacheNode<K, V>): void;
|
|
68
|
+
protected moveToHead(node: CacheNode<K, V>): void;
|
|
69
|
+
protected removeTail(): void;
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/fast-cache-ttl.d.ts
|
|
73
|
+
/**
|
|
74
|
+
* 带 TTL (Time To Live) 支持的高性能缓存实现。
|
|
75
|
+
* 继承自 FastCache,并增加了一个按时间排序的独立链表,
|
|
76
|
+
* 以实现 O(M) 复杂度的过期项目清理(M 为过期数量)。
|
|
77
|
+
*
|
|
78
|
+
* @important 在 Node.js 环境下使用 `autoCleanup: true` 时,
|
|
79
|
+
* 必须在程序退出前手动调用 `destroy()` 方法来清理定时器,
|
|
80
|
+
* 否则定时器会阻止 Node.js 进程正常退出。
|
|
81
|
+
*/
|
|
82
|
+
declare class FastCacheWithTTL<K extends CacheKey, V> extends FastCache<K, V> {
|
|
83
|
+
private readonly ttl;
|
|
84
|
+
private readonly autoCleanup;
|
|
85
|
+
private cleanupTimer;
|
|
86
|
+
private readonly timeHead;
|
|
87
|
+
private readonly timeTail;
|
|
88
|
+
private expired;
|
|
89
|
+
constructor(options: CacheOptions);
|
|
90
|
+
get(key: K): V | undefined;
|
|
91
|
+
set(key: K, value: V): void;
|
|
92
|
+
delete(key: K): boolean;
|
|
93
|
+
clear(): void;
|
|
94
|
+
has(key: K): boolean;
|
|
95
|
+
get size(): number;
|
|
96
|
+
get capacity(): number;
|
|
97
|
+
setMany(entries: [K, V][]): void;
|
|
98
|
+
getMany(keys: K[]): Map<K, V>;
|
|
99
|
+
getStats(): CacheStats;
|
|
100
|
+
cleanup(): number;
|
|
101
|
+
/**
|
|
102
|
+
* 清理定时器,防止在 Node.js 环境中进程无法正常退出。
|
|
103
|
+
* 如果开启了 `autoCleanup`,你应当在应用关闭前调用此方法。
|
|
104
|
+
*/
|
|
105
|
+
destroy(): void;
|
|
106
|
+
private isExpired;
|
|
107
|
+
private _addToTimeListTail;
|
|
108
|
+
private _removeFromTimeList;
|
|
109
|
+
private _moveToTimeListTail;
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region src/index.d.ts
|
|
113
|
+
declare function createCache<K extends CacheKey, V>(maxSize: number): FastCache<K, V>;
|
|
114
|
+
declare function createCacheWithTTL<K extends CacheKey, V>(options: CacheOptions): FastCacheWithTTL<K, V>;
|
|
115
|
+
declare const CachePresets: {
|
|
116
|
+
readonly apiCache: <T>(maxSize?: number) => FastCache<string, T>;
|
|
117
|
+
readonly computeCache: <T>(maxSize?: number) => FastCache<string, T>;
|
|
118
|
+
readonly sessionCache: <T>(maxSize?: number, ttl?: number) => FastCacheWithTTL<string, T>;
|
|
119
|
+
readonly tempCache: <T>(maxSize?: number, ttl?: number) => FastCacheWithTTL<string, T>;
|
|
120
|
+
};
|
|
121
|
+
//#endregion
|
|
122
|
+
export { CacheKey, CacheNode, CacheOptions, CachePresets, CacheStats, FastCache, FastCacheWithTTL, IFastCache, createCache, createCacheWithTTL };
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 基础类型定义
|
|
4
|
+
*/
|
|
5
|
+
type CacheKey = string | number | symbol | bigint;
|
|
6
|
+
interface CacheStats {
|
|
7
|
+
hits: number;
|
|
8
|
+
misses: number;
|
|
9
|
+
hitRate: number;
|
|
10
|
+
size: number;
|
|
11
|
+
capacity: number;
|
|
12
|
+
expired?: number;
|
|
13
|
+
}
|
|
14
|
+
interface CacheOptions {
|
|
15
|
+
maxSize: number;
|
|
16
|
+
ttl?: number;
|
|
17
|
+
autoCleanup?: boolean;
|
|
18
|
+
cleanupInterval?: number;
|
|
19
|
+
}
|
|
20
|
+
interface IFastCache<K extends CacheKey, V> {
|
|
21
|
+
get(key: K): V | undefined;
|
|
22
|
+
set(key: K, value: V): void;
|
|
23
|
+
delete(key: K): boolean;
|
|
24
|
+
clear(): void;
|
|
25
|
+
has(key: K): boolean;
|
|
26
|
+
readonly size: number;
|
|
27
|
+
readonly capacity: number;
|
|
28
|
+
setMany(entries: [K, V][]): void;
|
|
29
|
+
getMany(keys: K[]): Map<K, V>;
|
|
30
|
+
getStats(): CacheStats;
|
|
31
|
+
cleanup?(): number;
|
|
32
|
+
}
|
|
33
|
+
interface CacheNode<K extends CacheKey, V> {
|
|
34
|
+
key: K;
|
|
35
|
+
value: V;
|
|
36
|
+
prev: CacheNode<K, V> | null;
|
|
37
|
+
next: CacheNode<K, V> | null;
|
|
38
|
+
timestamp?: number;
|
|
39
|
+
timePrev?: CacheNode<K, V> | null;
|
|
40
|
+
timeNext?: CacheNode<K, V> | null;
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/fast-cache.d.ts
|
|
44
|
+
/**
|
|
45
|
+
* 高性能 LRU 缓存实现
|
|
46
|
+
* 使用 Map + 双向链表实现 O(1) 复杂度的所有操作
|
|
47
|
+
*/
|
|
48
|
+
declare class FastCache<K extends CacheKey, V> implements IFastCache<K, V> {
|
|
49
|
+
protected readonly cache: Map<K, CacheNode<K, V>>;
|
|
50
|
+
protected readonly head: CacheNode<K, V>;
|
|
51
|
+
protected readonly tail: CacheNode<K, V>;
|
|
52
|
+
protected readonly maxSize: number;
|
|
53
|
+
protected hits: number;
|
|
54
|
+
protected misses: number;
|
|
55
|
+
constructor(maxSize: number);
|
|
56
|
+
get(key: K): V | undefined;
|
|
57
|
+
set(key: K, value: V): void;
|
|
58
|
+
delete(key: K): boolean;
|
|
59
|
+
clear(): void;
|
|
60
|
+
has(key: K): boolean;
|
|
61
|
+
get size(): number;
|
|
62
|
+
get capacity(): number;
|
|
63
|
+
setMany(entries: [K, V][]): void;
|
|
64
|
+
getMany(keys: K[]): Map<K, V>;
|
|
65
|
+
getStats(): CacheStats;
|
|
66
|
+
protected addToHead(node: CacheNode<K, V>): void;
|
|
67
|
+
protected removeNode(node: CacheNode<K, V>): void;
|
|
68
|
+
protected moveToHead(node: CacheNode<K, V>): void;
|
|
69
|
+
protected removeTail(): void;
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/fast-cache-ttl.d.ts
|
|
73
|
+
/**
|
|
74
|
+
* 带 TTL (Time To Live) 支持的高性能缓存实现。
|
|
75
|
+
* 继承自 FastCache,并增加了一个按时间排序的独立链表,
|
|
76
|
+
* 以实现 O(M) 复杂度的过期项目清理(M 为过期数量)。
|
|
77
|
+
*
|
|
78
|
+
* @important 在 Node.js 环境下使用 `autoCleanup: true` 时,
|
|
79
|
+
* 必须在程序退出前手动调用 `destroy()` 方法来清理定时器,
|
|
80
|
+
* 否则定时器会阻止 Node.js 进程正常退出。
|
|
81
|
+
*/
|
|
82
|
+
declare class FastCacheWithTTL<K extends CacheKey, V> extends FastCache<K, V> {
|
|
83
|
+
private readonly ttl;
|
|
84
|
+
private readonly autoCleanup;
|
|
85
|
+
private cleanupTimer;
|
|
86
|
+
private readonly timeHead;
|
|
87
|
+
private readonly timeTail;
|
|
88
|
+
private expired;
|
|
89
|
+
constructor(options: CacheOptions);
|
|
90
|
+
get(key: K): V | undefined;
|
|
91
|
+
set(key: K, value: V): void;
|
|
92
|
+
delete(key: K): boolean;
|
|
93
|
+
clear(): void;
|
|
94
|
+
has(key: K): boolean;
|
|
95
|
+
get size(): number;
|
|
96
|
+
get capacity(): number;
|
|
97
|
+
setMany(entries: [K, V][]): void;
|
|
98
|
+
getMany(keys: K[]): Map<K, V>;
|
|
99
|
+
getStats(): CacheStats;
|
|
100
|
+
cleanup(): number;
|
|
101
|
+
/**
|
|
102
|
+
* 清理定时器,防止在 Node.js 环境中进程无法正常退出。
|
|
103
|
+
* 如果开启了 `autoCleanup`,你应当在应用关闭前调用此方法。
|
|
104
|
+
*/
|
|
105
|
+
destroy(): void;
|
|
106
|
+
private isExpired;
|
|
107
|
+
private _addToTimeListTail;
|
|
108
|
+
private _removeFromTimeList;
|
|
109
|
+
private _moveToTimeListTail;
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region src/index.d.ts
|
|
113
|
+
declare function createCache<K extends CacheKey, V>(maxSize: number): FastCache<K, V>;
|
|
114
|
+
declare function createCacheWithTTL<K extends CacheKey, V>(options: CacheOptions): FastCacheWithTTL<K, V>;
|
|
115
|
+
declare const CachePresets: {
|
|
116
|
+
readonly apiCache: <T>(maxSize?: number) => FastCache<string, T>;
|
|
117
|
+
readonly computeCache: <T>(maxSize?: number) => FastCache<string, T>;
|
|
118
|
+
readonly sessionCache: <T>(maxSize?: number, ttl?: number) => FastCacheWithTTL<string, T>;
|
|
119
|
+
readonly tempCache: <T>(maxSize?: number, ttl?: number) => FastCacheWithTTL<string, T>;
|
|
120
|
+
};
|
|
121
|
+
//#endregion
|
|
122
|
+
export { CacheKey, CacheNode, CacheOptions, CachePresets, CacheStats, FastCache, FastCacheWithTTL, IFastCache, createCache, createCacheWithTTL };
|