@twin.org/core 0.9.2-next.9 → 0.9.3-next.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/dist/es/helpers/jsonHelper.js +1 -2
- package/dist/es/helpers/jsonHelper.js.map +1 -1
- package/dist/es/index.js +2 -0
- package/dist/es/index.js.map +1 -1
- package/dist/es/models/coerceType.js +5 -1
- package/dist/es/models/coerceType.js.map +1 -1
- package/dist/es/utils/coerce.js +28 -1
- package/dist/es/utils/coerce.js.map +1 -1
- package/dist/es/utils/is.js +27 -4
- package/dist/es/utils/is.js.map +1 -1
- package/dist/es/utils/lfuCache.js +380 -0
- package/dist/es/utils/lfuCache.js.map +1 -0
- package/dist/es/utils/lruCache.js +271 -0
- package/dist/es/utils/lruCache.js.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/models/coerceType.d.ts +4 -0
- package/dist/types/utils/coerce.d.ts +6 -0
- package/dist/types/utils/lfuCache.d.ts +100 -0
- package/dist/types/utils/lruCache.d.ts +97 -0
- package/docs/changelog.md +148 -0
- package/docs/reference/classes/Coerce.md +28 -0
- package/docs/reference/classes/LfuCache.md +265 -0
- package/docs/reference/classes/LruCache.md +262 -0
- package/docs/reference/index.md +2 -0
- package/docs/reference/variables/CoerceType.md +6 -0
- package/package.json +2 -2
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
# Class: LruCache\<T\>
|
|
2
|
+
|
|
3
|
+
A fixed-capacity LRU cache with time-to-idle eviction.
|
|
4
|
+
|
|
5
|
+
Entries are removed in two ways:
|
|
6
|
+
- Capacity eviction: when the cache is full the least-recently-used entry is removed first.
|
|
7
|
+
- TTI eviction: a background timer sweeps idle entries every ttiMs milliseconds.
|
|
8
|
+
The timer only runs while there are entries; it stops automatically when the cache empties.
|
|
9
|
+
|
|
10
|
+
`get` and `set` both update an entry's LRU position and reset its idle timer.
|
|
11
|
+
`has` is a pure peek it evicts idle entries but does not refresh a live entry's TTI.
|
|
12
|
+
Call `destroy` when the cache is no longer needed to stop the background timer.
|
|
13
|
+
|
|
14
|
+
## Type Parameters
|
|
15
|
+
|
|
16
|
+
### T
|
|
17
|
+
|
|
18
|
+
`T` = `unknown`
|
|
19
|
+
|
|
20
|
+
## Constructors
|
|
21
|
+
|
|
22
|
+
### Constructor
|
|
23
|
+
|
|
24
|
+
> **new LruCache**\<`T`\>(`options?`): `LruCache`\<`T`\>
|
|
25
|
+
|
|
26
|
+
Create a new instance of LruCache.
|
|
27
|
+
|
|
28
|
+
#### Parameters
|
|
29
|
+
|
|
30
|
+
##### options?
|
|
31
|
+
|
|
32
|
+
The cache options.
|
|
33
|
+
|
|
34
|
+
###### capacity?
|
|
35
|
+
|
|
36
|
+
`number`
|
|
37
|
+
|
|
38
|
+
Maximum number of entries. Defaults to 1000. Must be a positive integer.
|
|
39
|
+
|
|
40
|
+
###### ttiMs?
|
|
41
|
+
|
|
42
|
+
`number`
|
|
43
|
+
|
|
44
|
+
Time-to-idle in milliseconds. Defaults to 10000. Must be a positive integer.
|
|
45
|
+
|
|
46
|
+
###### mutexTimeoutMs?
|
|
47
|
+
|
|
48
|
+
`number`
|
|
49
|
+
|
|
50
|
+
Maximum time in milliseconds to wait for getOrSet mutex acquisition.
|
|
51
|
+
|
|
52
|
+
#### Returns
|
|
53
|
+
|
|
54
|
+
`LruCache`\<`T`\>
|
|
55
|
+
|
|
56
|
+
#### Throws
|
|
57
|
+
|
|
58
|
+
ValidationError if capacity or ttiMs is not a positive integer.
|
|
59
|
+
|
|
60
|
+
## Properties
|
|
61
|
+
|
|
62
|
+
### CLASS\_NAME {#class_name}
|
|
63
|
+
|
|
64
|
+
> `readonly` `static` **CLASS\_NAME**: `string`
|
|
65
|
+
|
|
66
|
+
Runtime name for the class.
|
|
67
|
+
|
|
68
|
+
***
|
|
69
|
+
|
|
70
|
+
### DEFAULT\_CAPACITY {#default_capacity}
|
|
71
|
+
|
|
72
|
+
> `readonly` `static` **DEFAULT\_CAPACITY**: `1000` = `1000`
|
|
73
|
+
|
|
74
|
+
Default capacity.
|
|
75
|
+
|
|
76
|
+
***
|
|
77
|
+
|
|
78
|
+
### DEFAULT\_TTI\_MS {#default_tti_ms}
|
|
79
|
+
|
|
80
|
+
> `readonly` `static` **DEFAULT\_TTI\_MS**: `10000` = `10000`
|
|
81
|
+
|
|
82
|
+
Default time-to-idle in milliseconds.
|
|
83
|
+
|
|
84
|
+
## Methods
|
|
85
|
+
|
|
86
|
+
### count() {#count}
|
|
87
|
+
|
|
88
|
+
> **count**(): `number`
|
|
89
|
+
|
|
90
|
+
The number of entries currently held in the cache.
|
|
91
|
+
|
|
92
|
+
#### Returns
|
|
93
|
+
|
|
94
|
+
`number`
|
|
95
|
+
|
|
96
|
+
The number of entries in the cache.
|
|
97
|
+
|
|
98
|
+
***
|
|
99
|
+
|
|
100
|
+
### get() {#get}
|
|
101
|
+
|
|
102
|
+
> **get**(`key`): `T` \| `undefined`
|
|
103
|
+
|
|
104
|
+
Get a value from the cache.
|
|
105
|
+
Returns undefined if the key is absent or the entry has idled out.
|
|
106
|
+
A successful hit resets the entry's idle timer and moves it to most-recently-used.
|
|
107
|
+
|
|
108
|
+
#### Parameters
|
|
109
|
+
|
|
110
|
+
##### key
|
|
111
|
+
|
|
112
|
+
`string`
|
|
113
|
+
|
|
114
|
+
The key to retrieve.
|
|
115
|
+
|
|
116
|
+
#### Returns
|
|
117
|
+
|
|
118
|
+
`T` \| `undefined`
|
|
119
|
+
|
|
120
|
+
The cached value, or undefined on a miss or idle eviction.
|
|
121
|
+
|
|
122
|
+
***
|
|
123
|
+
|
|
124
|
+
### set() {#set}
|
|
125
|
+
|
|
126
|
+
> **set**(`key`, `value`): `void`
|
|
127
|
+
|
|
128
|
+
Store a value in the cache.
|
|
129
|
+
If the key already exists its value and idle timer are refreshed.
|
|
130
|
+
When the cache is at capacity, idle entries are swept first; if it is still full the
|
|
131
|
+
least-recently-used entry is evicted.
|
|
132
|
+
|
|
133
|
+
#### Parameters
|
|
134
|
+
|
|
135
|
+
##### key
|
|
136
|
+
|
|
137
|
+
`string`
|
|
138
|
+
|
|
139
|
+
The key to store.
|
|
140
|
+
|
|
141
|
+
##### value
|
|
142
|
+
|
|
143
|
+
`T`
|
|
144
|
+
|
|
145
|
+
The value to cache.
|
|
146
|
+
|
|
147
|
+
#### Returns
|
|
148
|
+
|
|
149
|
+
`void`
|
|
150
|
+
|
|
151
|
+
***
|
|
152
|
+
|
|
153
|
+
### getOrSet() {#getorset}
|
|
154
|
+
|
|
155
|
+
> **getOrSet**(`key`, `valueFactory`): `Promise`\<`T`\>
|
|
156
|
+
|
|
157
|
+
Atomically get an existing value or create and store it once using an async factory.
|
|
158
|
+
Concurrent calls for the same key are serialized via a mutex.
|
|
159
|
+
|
|
160
|
+
#### Parameters
|
|
161
|
+
|
|
162
|
+
##### key
|
|
163
|
+
|
|
164
|
+
`string`
|
|
165
|
+
|
|
166
|
+
The key to get or create.
|
|
167
|
+
|
|
168
|
+
##### valueFactory
|
|
169
|
+
|
|
170
|
+
() => `Promise`\<`T`\>
|
|
171
|
+
|
|
172
|
+
Async callback used to build a value when the key is absent.
|
|
173
|
+
|
|
174
|
+
#### Returns
|
|
175
|
+
|
|
176
|
+
`Promise`\<`T`\>
|
|
177
|
+
|
|
178
|
+
The existing or newly created value.
|
|
179
|
+
|
|
180
|
+
***
|
|
181
|
+
|
|
182
|
+
### has() {#has}
|
|
183
|
+
|
|
184
|
+
> **has**(`key`): `boolean`
|
|
185
|
+
|
|
186
|
+
Check whether a key exists in the cache and has not idled out.
|
|
187
|
+
Idle entries are evicted on peek, but a live entry's TTI is not reset.
|
|
188
|
+
|
|
189
|
+
#### Parameters
|
|
190
|
+
|
|
191
|
+
##### key
|
|
192
|
+
|
|
193
|
+
`string`
|
|
194
|
+
|
|
195
|
+
The key to test.
|
|
196
|
+
|
|
197
|
+
#### Returns
|
|
198
|
+
|
|
199
|
+
`boolean`
|
|
200
|
+
|
|
201
|
+
True if the key is present and not idle.
|
|
202
|
+
|
|
203
|
+
***
|
|
204
|
+
|
|
205
|
+
### keys() {#keys}
|
|
206
|
+
|
|
207
|
+
> **keys**(): `string`[]
|
|
208
|
+
|
|
209
|
+
Return all keys for entries that have not idled out.
|
|
210
|
+
Idle entries encountered during iteration are evicted.
|
|
211
|
+
|
|
212
|
+
#### Returns
|
|
213
|
+
|
|
214
|
+
`string`[]
|
|
215
|
+
|
|
216
|
+
An array of live keys in least-recently-used to most-recently-used order.
|
|
217
|
+
|
|
218
|
+
***
|
|
219
|
+
|
|
220
|
+
### delete() {#delete}
|
|
221
|
+
|
|
222
|
+
> **delete**(`key`): `void`
|
|
223
|
+
|
|
224
|
+
Remove an entry from the cache.
|
|
225
|
+
Cancels the background timer if the cache becomes empty.
|
|
226
|
+
|
|
227
|
+
#### Parameters
|
|
228
|
+
|
|
229
|
+
##### key
|
|
230
|
+
|
|
231
|
+
`string`
|
|
232
|
+
|
|
233
|
+
The key to remove.
|
|
234
|
+
|
|
235
|
+
#### Returns
|
|
236
|
+
|
|
237
|
+
`void`
|
|
238
|
+
|
|
239
|
+
***
|
|
240
|
+
|
|
241
|
+
### clear() {#clear}
|
|
242
|
+
|
|
243
|
+
> **clear**(): `void`
|
|
244
|
+
|
|
245
|
+
Remove all entries from the cache and cancel the background timer.
|
|
246
|
+
|
|
247
|
+
#### Returns
|
|
248
|
+
|
|
249
|
+
`void`
|
|
250
|
+
|
|
251
|
+
***
|
|
252
|
+
|
|
253
|
+
### destroy() {#destroy}
|
|
254
|
+
|
|
255
|
+
> **destroy**(): `void`
|
|
256
|
+
|
|
257
|
+
Stop the background idle-sweep timer and release all entries.
|
|
258
|
+
The cache must not be used after this call.
|
|
259
|
+
|
|
260
|
+
#### Returns
|
|
261
|
+
|
|
262
|
+
`void`
|
package/docs/reference/index.md
CHANGED
|
@@ -40,6 +40,8 @@
|
|
|
40
40
|
- [Guards](classes/Guards.md)
|
|
41
41
|
- [I18n](classes/I18n.md)
|
|
42
42
|
- [Is](classes/Is.md)
|
|
43
|
+
- [LfuCache](classes/LfuCache.md)
|
|
44
|
+
- [LruCache](classes/LruCache.md)
|
|
43
45
|
- [Mutex](classes/Mutex.md)
|
|
44
46
|
- [SharedObjectBuffer](classes/SharedObjectBuffer.md)
|
|
45
47
|
- [SharedStore](classes/SharedStore.md)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/core",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.3-next.1",
|
|
4
4
|
"description": "Helper methods/classes for data type checking/validation/guarding/error handling",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"node": ">=24.0.0"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@twin.org/nameof": "0.9.
|
|
17
|
+
"@twin.org/nameof": "0.9.3-next.1",
|
|
18
18
|
"intl-messageformat": "11.2.13",
|
|
19
19
|
"rfc6902": "5.3.0"
|
|
20
20
|
},
|