@nxtedition/cache 2.1.11 → 2.1.12
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 +22 -26
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -98,7 +98,6 @@ export class Cache extends EventEmi
|
|
|
98
98
|
#stale
|
|
99
99
|
#serializer
|
|
100
100
|
|
|
101
|
-
#lockOrigin = 0
|
|
102
101
|
#lockId = null
|
|
103
102
|
#lockArray = null
|
|
104
103
|
#lockSet
|
|
@@ -202,14 +201,8 @@ export class Cache extends EventEmi
|
|
|
202
201
|
}
|
|
203
202
|
}
|
|
204
203
|
|
|
205
|
-
const sab = getOrCreate(`__@nxtedition/cache/${location}`, 8 + 1024)
|
|
206
|
-
|
|
207
|
-
const originArray = new BigInt64Array(sab, 0, 1)
|
|
208
|
-
Atomics.compareExchange(originArray, 0, 0n, BigInt(Date.now()))
|
|
209
|
-
|
|
210
|
-
this.#lockOrigin = Number(originArray[0])
|
|
211
204
|
this.#lockId = randomUUID()
|
|
212
|
-
this.#lockArray = new Int32Array(
|
|
205
|
+
this.#lockArray = new Int32Array(getOrCreate(`__@nxtedition/cache/${location}`, 8 + 1024))
|
|
213
206
|
this.#lockSet = new Set()
|
|
214
207
|
}
|
|
215
208
|
|
|
@@ -531,13 +524,20 @@ export class Cache extends EventEmi
|
|
|
531
524
|
if (this.#lockId != null) {
|
|
532
525
|
// Another process holds the lock — wait for the value instead of calling valueSelector.
|
|
533
526
|
// Use deferred pattern so #waitForValue can check promise identity to detect delete+get races.
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
if (
|
|
527
|
+
let lockIdx = -1
|
|
528
|
+
let lockVal = 0
|
|
529
|
+
if (this.#lockArray) {
|
|
530
|
+
lockIdx = HASHER.h32(key) % this.#lockArray.length
|
|
531
|
+
lockVal = Atomics.load(this.#lockArray, lockIdx)
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
const lockRes = this.#tryAcquireLock(key, lockIdx)
|
|
535
|
+
|
|
536
|
+
if (typeof lockRes === 'number') {
|
|
537
537
|
const { promise, resolve, reject } = Promise.withResolvers ()
|
|
538
538
|
promise.catch(noop)
|
|
539
539
|
this.#dedupe.set(key, promise)
|
|
540
|
-
this.#waitForValue(args, key, lockIdx,
|
|
540
|
+
this.#waitForValue(args, key, lockIdx, lockVal, lockRes, promise).then(resolve, reject)
|
|
541
541
|
return { async: true, value: promise }
|
|
542
542
|
}
|
|
543
543
|
}
|
|
@@ -690,7 +690,7 @@ export class Cache extends EventEmi
|
|
|
690
690
|
|
|
691
691
|
if (this.#lockSet && this.#lockArray) {
|
|
692
692
|
for (const idx of this.#lockSet) {
|
|
693
|
-
Atomics.
|
|
693
|
+
Atomics.add(this.#lockArray, idx, 1)
|
|
694
694
|
Atomics.notify(this.#lockArray, idx)
|
|
695
695
|
}
|
|
696
696
|
this.#lockSet.clear()
|
|
@@ -784,8 +784,8 @@ export class Cache extends EventEmi
|
|
|
784
784
|
}
|
|
785
785
|
|
|
786
786
|
if (row.lock_owner === this.#lockId) {
|
|
787
|
-
if (this.#lockArray
|
|
788
|
-
Atomics.
|
|
787
|
+
if (this.#lockArray) {
|
|
788
|
+
Atomics.add(this.#lockArray, lockIdx, 1)
|
|
789
789
|
}
|
|
790
790
|
return 'acquired'
|
|
791
791
|
}
|
|
@@ -797,8 +797,8 @@ export class Cache extends EventEmi
|
|
|
797
797
|
// process wins if multiple try to steal concurrently.
|
|
798
798
|
const stealResult = this.#lockStealQuery .run(now, this.#lockId, key, lockedAt)
|
|
799
799
|
if (stealResult.changes === 1) {
|
|
800
|
-
if (this.#lockArray
|
|
801
|
-
Atomics.
|
|
800
|
+
if (this.#lockArray) {
|
|
801
|
+
Atomics.add(this.#lockArray, lockIdx, 1)
|
|
802
802
|
}
|
|
803
803
|
return 'acquired'
|
|
804
804
|
}
|
|
@@ -844,6 +844,7 @@ export class Cache extends EventEmi
|
|
|
844
844
|
args ,
|
|
845
845
|
key ,
|
|
846
846
|
lockIdx ,
|
|
847
|
+
lockVal ,
|
|
847
848
|
lockedAt ,
|
|
848
849
|
selfPromise ,
|
|
849
850
|
) {
|
|
@@ -856,12 +857,7 @@ export class Cache extends EventEmi
|
|
|
856
857
|
)
|
|
857
858
|
|
|
858
859
|
if (this.#lockArray != null && lockIdx >= 0) {
|
|
859
|
-
const { async, value } = Atomics.waitAsync(
|
|
860
|
-
this.#lockArray,
|
|
861
|
-
lockIdx,
|
|
862
|
-
lockedAt - this.#lockOrigin,
|
|
863
|
-
waitTime,
|
|
864
|
-
)
|
|
860
|
+
const { async, value } = Atomics.waitAsync(this.#lockArray, lockIdx, lockVal, waitTime)
|
|
865
861
|
if (async) {
|
|
866
862
|
await value
|
|
867
863
|
}
|
|
@@ -885,10 +881,10 @@ export class Cache extends EventEmi
|
|
|
885
881
|
}
|
|
886
882
|
|
|
887
883
|
// Try to acquire lock for takeover
|
|
888
|
-
const
|
|
889
|
-
if (typeof
|
|
884
|
+
const lockRes = this.#tryAcquireLock(key, lockIdx)
|
|
885
|
+
if (typeof lockRes === 'number') {
|
|
890
886
|
// Another process holds the lock — wait for them
|
|
891
|
-
lockedAt =
|
|
887
|
+
lockedAt = lockRes
|
|
892
888
|
} else {
|
|
893
889
|
// We acquired the lock or lock is unavailable — do the work
|
|
894
890
|
break
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/cache",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.12",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"tsd": "^0.33.0",
|
|
31
31
|
"typescript": "^5.9.3"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "89b5cbdea4feddf44ef7955ba2b60105dea996f0",
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@nxtedition/shared": "^5.1.10",
|
|
36
36
|
"xxhash-wasm": "^1.1.0"
|