Store.tryClaim
Claim replay keys atomically
Records the first use of a replay key until its expiration time.
Usage
import { } from 'mppx'
const = .()
const = .() + 60_000
const = await .(, 'request:abc123', )
.()
true const = await .(, 'request:abc123', )
.()
falseStore.tryClaim uses the store's optional optimized tryClaim operation when present. Otherwise, it falls back to AtomicStore.update. Expired replay markers can be claimed again; legacy non-marker values remain claimed.
Support replay claims in a custom store
AtomicStore accepts an optional tryClaim fast path. The fallback stores a ReplayMarker, so include that type in a custom store's item map when you don't provide the fast path.
type TryClaim<itemMap extends StoreItemMap = StoreItemMap> = <
key extends keyof itemMap & string,
>(key: key, expires: number) => boolean | Promise<boolean>
type ReplayMarker = {
expires: number
type: 'mppx:replay'
}Compose a native claim implementation
Spread a Redis or Upstash adapter into Store.from, then add a native tryClaim operation. The wrapper preserves the fast path and applies keyPrefix to claim keys.
import { Store } from 'mppx'
const adapter = Store.upstash({
del: (key) => redis.del(key),
get: (key) => redis.get(key),
set: (key, value) => redis.set(key, value),
update: (key, fn) => atomicUpdate(redis, key, fn),
})
const store = Store.from(
{
...adapter,
async tryClaim(key, expires) {
const result = await redis.set(key, { expires, type: 'mppx:replay' }, {
nx: true,
pxat: expires,
})
return result === 'OK'
},
},
{ keyPrefix: 'mppx:' },
)Use an absolute Unix-millisecond expiry such as Upstash pxat or Redis PXAT, not a relative duration.
Return type
type ReturnType = boolean | Promise<boolean>Returns true when this call records the key and false when an unexpired claim already exists.
Parameters
expires
- Type:
number
Unix timestamp in milliseconds when the replay claim expires.
key
- Type:
string
Store key to claim. Typed stores constrain this value to their item-map keys.
store
- Type:
Store.AtomicStore
Atomic store used to persist the replay marker. Implement store.tryClaim as a single insert-if-absent-with-expiry operation when your backend supports it.