@nxtedition/cache 2.1.4 → 2.1.6
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/lib/index.js +13 -3
- package/lib/memory.js +10 -7
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -96,6 +96,7 @@ export class Cache extends EventEmi
|
|
|
96
96
|
#lockMean = 5
|
|
97
97
|
#lockTimeout = 10
|
|
98
98
|
#lockMinTimeout = 1
|
|
99
|
+
#lockMaxTimeout = 1_000
|
|
99
100
|
|
|
100
101
|
#location
|
|
101
102
|
#database = null
|
|
@@ -196,7 +197,7 @@ export class Cache extends EventEmi
|
|
|
196
197
|
|
|
197
198
|
for (let n = 0; opts?.database !== null && opts?.database !== false; n++) {
|
|
198
199
|
try {
|
|
199
|
-
const maxSize = opts?.database?.maxSize ??
|
|
200
|
+
const maxSize = opts?.database?.maxSize ?? 128 * 1024 * 1024
|
|
200
201
|
const timeout = opts?.database?.timeout ?? 20
|
|
201
202
|
|
|
202
203
|
this.#database ??= new DatabaseSync(location, { timeout })
|
|
@@ -709,13 +710,22 @@ export class Cache extends EventEmi
|
|
|
709
710
|
// EMA of mean and variance (Welford-style), clamped to [10ms, 1s].
|
|
710
711
|
// Timeout = mean * 1.2 + 3 * stddev: 20% base margin plus 3 standard
|
|
711
712
|
// deviations to accommodate timing variability.
|
|
713
|
+
// Winsorize input at mean + 5σ so a single extreme outlier can't blow up the stats.
|
|
712
714
|
const alpha = 0.2
|
|
713
|
-
const
|
|
715
|
+
const clamped = Math.min(
|
|
716
|
+
duration,
|
|
717
|
+
this.#lockMean + 5 * Math.sqrt(this.#lockVar),
|
|
718
|
+
this.#lockMaxTimeout,
|
|
719
|
+
)
|
|
720
|
+
const diff = clamped - this.#lockMean
|
|
714
721
|
this.#lockMean += alpha * diff
|
|
715
722
|
this.#lockVar = (1 - alpha) * (this.#lockVar + alpha * diff * diff)
|
|
716
723
|
this.#lockTimeout = Math.max(
|
|
717
724
|
this.#lockMinTimeout,
|
|
718
|
-
Math.min(
|
|
725
|
+
Math.min(
|
|
726
|
+
this.#lockMaxTimeout,
|
|
727
|
+
Math.ceil(this.#lockMean * 1.2 + Math.sqrt(this.#lockVar) * 3),
|
|
728
|
+
),
|
|
719
729
|
)
|
|
720
730
|
}
|
|
721
731
|
|
package/lib/memory.js
CHANGED
|
@@ -13,7 +13,10 @@
|
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
let fastNow = Date.now()
|
|
17
|
+
setInterval(() => {
|
|
18
|
+
fastNow = Date.now()
|
|
19
|
+
}, 1e3).unref()
|
|
17
20
|
|
|
18
21
|
export class MemoryCache {
|
|
19
22
|
#map = new Map()
|
|
@@ -25,7 +28,6 @@ export class MemoryCache {
|
|
|
25
28
|
#size = 0
|
|
26
29
|
#count = 0
|
|
27
30
|
|
|
28
|
-
#counter = 0
|
|
29
31
|
#cork = 0
|
|
30
32
|
|
|
31
33
|
constructor(opts ) {
|
|
@@ -42,8 +44,6 @@ export class MemoryCache {
|
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
set(key , entry ) {
|
|
45
|
-
this.#counter = (this.#counter + 1) & maxInt
|
|
46
|
-
|
|
47
47
|
const existing = this.#map.get(key)
|
|
48
48
|
if (existing != null) {
|
|
49
49
|
this.#delete(existing)
|
|
@@ -52,7 +52,7 @@ export class MemoryCache {
|
|
|
52
52
|
this.#map.set(key, entry)
|
|
53
53
|
entry.key = key
|
|
54
54
|
entry.index = this.#arr.push(entry) - 1
|
|
55
|
-
entry.counter =
|
|
55
|
+
entry.counter = fastNow
|
|
56
56
|
|
|
57
57
|
this.#size += entry.size ?? 0
|
|
58
58
|
this.#count += 1
|
|
@@ -76,8 +76,7 @@ export class MemoryCache {
|
|
|
76
76
|
get(key ) {
|
|
77
77
|
const entry = this.#map.get(key)
|
|
78
78
|
if (entry != null) {
|
|
79
|
-
|
|
80
|
-
entry.counter = this.#counter
|
|
79
|
+
entry.counter = fastNow
|
|
81
80
|
}
|
|
82
81
|
return entry
|
|
83
82
|
}
|
|
@@ -121,6 +120,10 @@ export class MemoryCache {
|
|
|
121
120
|
}
|
|
122
121
|
|
|
123
122
|
#prune() {
|
|
123
|
+
if (this.#cork > 0) {
|
|
124
|
+
return
|
|
125
|
+
}
|
|
126
|
+
|
|
124
127
|
while (this.#arr.length > 0 && (this.#size > this.#maxSize || this.#count > this.#maxCount)) {
|
|
125
128
|
const e1 = this.#arr[(Math.random() * this.#arr.length) | 0]
|
|
126
129
|
const e2 = this.#arr[(Math.random() * this.#arr.length) | 0]
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/cache",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -30,5 +30,5 @@
|
|
|
30
30
|
"tsd": "^0.33.0",
|
|
31
31
|
"typescript": "^5.9.3"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "a7202d8b4d3decaff213576df560e8cd5775db37"
|
|
34
34
|
}
|