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