@reddb-io/client 1.30.1 → 1.31.0

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.
@@ -114,15 +114,41 @@ export interface CachePutOptions {
114
114
 
115
115
  export type CacheExistsStatus = 'present' | 'absent' | 'maybe'
116
116
 
117
+ export interface CacheEntryResult {
118
+ key: string
119
+ value: Uint8Array | null
120
+ size_bytes: number | null
121
+ ttl_ms: number | null
122
+ expires_at_unix_ms: number | null
123
+ version: number | null
124
+ stale: boolean
125
+ }
126
+
127
+ export interface CacheHitResult extends CacheEntryResult {
128
+ value: Uint8Array
129
+ size_bytes: number
130
+ }
131
+
132
+ export interface CachePutEntry extends CachePutOptions {
133
+ key: string
134
+ value: Uint8Array | string
135
+ }
136
+
117
137
  export class CacheClient {
118
138
  get(namespace: string, key: string): Promise<Uint8Array | null>
139
+ getEntry(namespace: string, key: string): Promise<CacheHitResult | null>
140
+ getMany(namespace: string, keys: string[]): Promise<CacheEntryResult[]>
119
141
  put(
120
142
  namespace: string,
121
143
  key: string,
122
144
  value: Uint8Array | string,
123
145
  opts?: CachePutOptions,
124
146
  ): Promise<void>
147
+ putMany(namespace: string, entries: CachePutEntry[]): Promise<number>
125
148
  exists(namespace: string, key: string): Promise<CacheExistsStatus>
149
+ expire(namespace: string, key: string, ttl_ms: number): Promise<boolean>
150
+ expireAt(namespace: string, key: string, expires_at_unix_ms: number): Promise<boolean>
151
+ persist(namespace: string, key: string): Promise<boolean>
126
152
  invalidate(namespace: string, key: string): Promise<void>
127
153
  invalidatePrefix(namespace: string, prefix: string): Promise<number>
128
154
  invalidateTags(namespace: string, tags: string[]): Promise<number>
package/index.d.ts CHANGED
@@ -110,15 +110,41 @@ export interface CachePutOptions {
110
110
 
111
111
  export type CacheExistsStatus = 'present' | 'absent' | 'maybe'
112
112
 
113
+ export interface CacheEntryResult {
114
+ key: string
115
+ value: Uint8Array | null
116
+ size_bytes: number | null
117
+ ttl_ms: number | null
118
+ expires_at_unix_ms: number | null
119
+ version: number | null
120
+ stale: boolean
121
+ }
122
+
123
+ export interface CacheHitResult extends CacheEntryResult {
124
+ value: Uint8Array
125
+ size_bytes: number
126
+ }
127
+
128
+ export interface CachePutEntry extends CachePutOptions {
129
+ key: string
130
+ value: Uint8Array | Buffer | string
131
+ }
132
+
113
133
  export class CacheClient {
114
134
  get(namespace: string, key: string): Promise<Uint8Array | null>
135
+ getEntry(namespace: string, key: string): Promise<CacheHitResult | null>
136
+ getMany(namespace: string, keys: string[]): Promise<CacheEntryResult[]>
115
137
  put(
116
138
  namespace: string,
117
139
  key: string,
118
140
  value: Uint8Array | Buffer | string,
119
141
  opts?: CachePutOptions,
120
142
  ): Promise<void>
143
+ putMany(namespace: string, entries: CachePutEntry[]): Promise<number>
121
144
  exists(namespace: string, key: string): Promise<CacheExistsStatus>
145
+ expire(namespace: string, key: string, ttl_ms: number): Promise<boolean>
146
+ expireAt(namespace: string, key: string, expires_at_unix_ms: number): Promise<boolean>
147
+ persist(namespace: string, key: string): Promise<boolean>
122
148
  invalidate(namespace: string, key: string): Promise<void>
123
149
  invalidatePrefix(namespace: string, prefix: string): Promise<number>
124
150
  invalidateTags(namespace: string, tags: string[]): Promise<number>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reddb-io/client",
3
- "version": "1.30.1",
3
+ "version": "1.31.0",
4
4
  "description": "Thin remote-only RedDB driver. Pure JavaScript — no install scripts, no native binary. Speaks RedWire/gRPC/HTTP. Embedded URIs (memory://, file://, red:///path) are rejected — use @reddb-io/sdk for those.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/cache.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
- * Cache client — exposes cache.{get,put,exists,invalidate,invalidatePrefix,
3
- * invalidateTags,flushNamespace} via the underlying HTTP transport.
2
+ * Cache client — exposes exact-key reads/writes, batching, TTL management,
3
+ * and invalidation through the underlying HTTP transport.
4
4
  *
5
5
  * The HTTP data-plane endpoints under /cache/ns/* authorize the caller's
6
6
  * tenant and cache namespace. Invalidation is local to the addressed server.
@@ -22,9 +22,35 @@ export class CacheClient {
22
22
  * @returns {Promise<Uint8Array | null>}
23
23
  */
24
24
  async get(namespace, key) {
25
+ const entry = await this.getEntry(namespace, key)
26
+ return entry?.value ?? null
27
+ }
28
+
29
+ /**
30
+ * Fetch a cached value together with its TTL and version metadata.
31
+ * Returns null on a miss.
32
+ * @param {string} namespace
33
+ * @param {string} key
34
+ * @returns {Promise<{key:string,value:Uint8Array,size_bytes:number,ttl_ms:number|null,expires_at_unix_ms:number|null,version:number|null,stale:boolean}|null>}
35
+ */
36
+ async getEntry(namespace, key) {
25
37
  const result = await this._client.call('cache.get', { namespace, key })
26
38
  if (result == null || result.value == null) return null
27
- return base64ToBytes(result.value)
39
+ return { ...result, value: base64ToBytes(result.value) }
40
+ }
41
+
42
+ /**
43
+ * Fetch several keys in one round trip. Results preserve input order.
44
+ * @param {string} namespace
45
+ * @param {string[]} keys
46
+ * @returns {Promise<Array<{key:string,value:Uint8Array|null,size_bytes:number|null,ttl_ms:number|null,expires_at_unix_ms:number|null,version:number|null,stale:boolean}>>}
47
+ */
48
+ async getMany(namespace, keys) {
49
+ const result = await this._client.call('cache.mget', { namespace, keys })
50
+ return (result?.entries ?? []).map((entry) => ({
51
+ ...entry,
52
+ value: entry.value == null ? null : base64ToBytes(entry.value),
53
+ }))
28
54
  }
29
55
 
30
56
  /**
@@ -48,6 +74,23 @@ export class CacheClient {
48
74
  })
49
75
  }
50
76
 
77
+ /**
78
+ * Store several entries in one round trip. A batch accepts at most 256
79
+ * entries and 8 MiB of decoded values.
80
+ * @param {string} namespace
81
+ * @param {Array<{key:string,value:Uint8Array|Buffer|string,ttl_ms?:number,tags?:string[],policy?:object}>} entries
82
+ * @returns {Promise<number>}
83
+ */
84
+ async putMany(namespace, entries) {
85
+ const encoded = entries.map(({ key, value, ...opts }) => ({
86
+ key,
87
+ value: bytesToBase64(value),
88
+ ...opts,
89
+ }))
90
+ const result = await this._client.call('cache.mput', { namespace, entries: encoded })
91
+ return result?.stored ?? 0
92
+ }
93
+
51
94
  /**
52
95
  * Check whether a key is present.
53
96
  * @param {string} namespace
@@ -59,6 +102,28 @@ export class CacheClient {
59
102
  return result?.status ?? 'maybe'
60
103
  }
61
104
 
105
+ /** Set or replace a key's relative TTL without rewriting its value. */
106
+ async expire(namespace, key, ttl_ms) {
107
+ const result = await this._client.call('cache.expiry', { namespace, key, ttl_ms })
108
+ return result?.updated ?? false
109
+ }
110
+
111
+ /** Set or replace a key's absolute expiry deadline. */
112
+ async expireAt(namespace, key, expires_at_unix_ms) {
113
+ const result = await this._client.call('cache.expiry', {
114
+ namespace,
115
+ key,
116
+ expires_at_unix_ms,
117
+ })
118
+ return result?.updated ?? false
119
+ }
120
+
121
+ /** Remove expiry from a key without rewriting its value. */
122
+ async persist(namespace, key) {
123
+ const result = await this._client.call('cache.expiry', { namespace, key, persist: true })
124
+ return result?.updated ?? false
125
+ }
126
+
62
127
  /**
63
128
  * Remove a single entry.
64
129
  * @param {string} namespace
package/src/http.js CHANGED
@@ -24,7 +24,10 @@
24
24
  *
25
25
  * cache.get → GET /cache/ns/:ns/:key
26
26
  * cache.put → PUT /cache/ns/:ns/:key
27
+ * cache.mget → POST /cache/ns/:ns/mget
28
+ * cache.mput → POST /cache/ns/:ns/mput
27
29
  * cache.exists → GET /cache/ns/:ns/:key/exists
30
+ * cache.expiry → PATCH /cache/ns/:ns/:key/ttl
28
31
  * cache.invalidate → DELETE /cache/ns/:ns/:key
29
32
  * cache.invalidate_prefix → DELETE /cache/ns/:ns?prefix=...
30
33
  * cache.invalidate_tags → POST /cache/ns/:ns/invalidate-tags (body: {tags})
@@ -386,10 +389,25 @@ const ROUTES = {
386
389
  url: `${base}/cache/ns/${encodeURIComponent(namespace)}/${encodeURIComponent(key)}`,
387
390
  init: { method: 'PUT', body: JSON.stringify(serializeJsonValue({ value, ttl_ms, tags, policy })) },
388
391
  }),
392
+ 'cache.mget': (base, { namespace, keys }) => ({
393
+ url: `${base}/cache/ns/${encodeURIComponent(namespace)}/mget`,
394
+ init: { method: 'POST', body: JSON.stringify({ keys }) },
395
+ }),
396
+ 'cache.mput': (base, { namespace, entries }) => ({
397
+ url: `${base}/cache/ns/${encodeURIComponent(namespace)}/mput`,
398
+ init: { method: 'POST', body: JSON.stringify(serializeJsonValue({ entries })) },
399
+ }),
389
400
  'cache.exists': (base, { namespace, key }) => ({
390
401
  url: `${base}/cache/ns/${encodeURIComponent(namespace)}/${encodeURIComponent(key)}/exists`,
391
402
  init: { method: 'GET' },
392
403
  }),
404
+ 'cache.expiry': (base, { namespace, key, ttl_ms, expires_at_unix_ms, persist }) => ({
405
+ url: `${base}/cache/ns/${encodeURIComponent(namespace)}/${encodeURIComponent(key)}/ttl`,
406
+ init: {
407
+ method: 'PATCH',
408
+ body: JSON.stringify(serializeJsonValue({ ttl_ms, expires_at_unix_ms, persist })),
409
+ },
410
+ }),
393
411
  'cache.invalidate': (base, { namespace, key }) => ({
394
412
  url: `${base}/cache/ns/${encodeURIComponent(namespace)}/${encodeURIComponent(key)}`,
395
413
  init: { method: 'DELETE' },