@sigil-dev/cache 0.0.0-stage → 0.9.31
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/README.md +22 -3
- package/index.ts +229 -0
- package/memory.ts +81 -0
- package/package.json +33 -4
- package/redis.ts +104 -0
- package/table.ts +139 -0
package/README.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @sigil-dev/cache
|
|
2
|
+
|
|
3
|
+
One cache for a Grimoire app, in memory, Redis (or Valkey, Dragonfly, KeyDB) or a table of your own database.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
sigil add cache
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
// sigil.config.ts
|
|
11
|
+
plugins: [cache()] // in memory, per worker
|
|
12
|
+
plugins: [cache(redis())] // REDIS_URL, read when the server starts
|
|
13
|
+
plugins: [tome(), cache(table())] // a tome table; `sigil db generate` creates it
|
|
14
|
+
|
|
15
|
+
// a route: locals.cache; anywhere else on the server: useCache()
|
|
16
|
+
const index = await locals.cache.remember("index", "1h", buildIndex);
|
|
17
|
+
await locals.cache.increment(`login:${ip}`, "1m");
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`remember` builds once however many ask at once, and past its ttl answers the old value while one call rebuilds it, across workers too. Values go through JSON in every store. `increment` is atomic in all three.
|
|
21
|
+
|
|
22
|
+
Docs: https://sigil.cane1712.dev/docs/reference/cache
|
package/index.ts
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { type Duration, type GrimoirePlugin, ms } from "@sigil-dev/grimoire";
|
|
2
|
+
import { memory } from "./memory.ts";
|
|
3
|
+
|
|
4
|
+
export { type MemoryOptions, memory } from "./memory.ts";
|
|
5
|
+
export { type RedisOptions, redis } from "./redis.ts";
|
|
6
|
+
export { cacheTable, type TableOptions, table } from "./table.ts";
|
|
7
|
+
export type { Duration };
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Where a cache keeps its entries. Values arrive as strings, already
|
|
11
|
+
* encoded; a store only keeps them and counts.
|
|
12
|
+
*/
|
|
13
|
+
export interface CacheStore {
|
|
14
|
+
/** "memory", "redis", "table": named in warnings. */
|
|
15
|
+
readonly kind: string;
|
|
16
|
+
/** Whether every process sees the same entries. memory() does not. */
|
|
17
|
+
readonly shared: boolean;
|
|
18
|
+
get(key: string): Promise<string | undefined>;
|
|
19
|
+
/** Overwrites. No `ttl` keeps it until deleted or evicted. */
|
|
20
|
+
set(key: string, value: string, ttl?: number): Promise<void>;
|
|
21
|
+
/** Sets only when absent (or expired). True when it did: a lock. */
|
|
22
|
+
add(key: string, value: string, ttl: number): Promise<boolean>;
|
|
23
|
+
/** True when there was an entry. */
|
|
24
|
+
delete(key: string): Promise<boolean>;
|
|
25
|
+
/**
|
|
26
|
+
* One more for `key` in a fixed window of `window` ms, starting with
|
|
27
|
+
* the first: the count so far. Atomic, so two processes never both see 1.
|
|
28
|
+
*/
|
|
29
|
+
increment(key: string, window: number): Promise<number>;
|
|
30
|
+
/** Everything this store keeps for the app. For tests and `sigil cache clear`. */
|
|
31
|
+
clear(): Promise<void>;
|
|
32
|
+
/** Opened when the server starts. */
|
|
33
|
+
open?(): Promise<void>;
|
|
34
|
+
close?(): Promise<void>;
|
|
35
|
+
/** What the plugin adds for the store: table() brings a table. */
|
|
36
|
+
readonly plugin?: Partial<GrimoirePlugin> & {
|
|
37
|
+
tables?: Record<string, unknown>;
|
|
38
|
+
database?: string;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface RememberOptions {
|
|
43
|
+
/**
|
|
44
|
+
* How long past `ttl` the old value is still served while one call
|
|
45
|
+
* rebuilds it in the background. Default: as long as `ttl`. 0 makes
|
|
46
|
+
* every caller after `ttl` wait for the rebuild.
|
|
47
|
+
*/
|
|
48
|
+
staleFor?: Duration;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The cache: `locals.cache` in a route, `useCache()` anywhere else on the server. */
|
|
52
|
+
export interface Cache {
|
|
53
|
+
/** The value, or undefined when there is none or it expired. */
|
|
54
|
+
get<T = unknown>(key: string): Promise<T | undefined>;
|
|
55
|
+
/** Stores a JSON value. No `ttl` keeps it until deleted or evicted. */
|
|
56
|
+
set(key: string, value: unknown, ttl?: Duration): Promise<void>;
|
|
57
|
+
/** Stores only when there is no value: true when it did. */
|
|
58
|
+
add(key: string, value: unknown, ttl: Duration): Promise<boolean>;
|
|
59
|
+
delete(key: string): Promise<boolean>;
|
|
60
|
+
/** Counts calls in a fixed window: `increment("login:1.2.3.4", "1m")`. */
|
|
61
|
+
increment(key: string, window: Duration): Promise<number>;
|
|
62
|
+
/**
|
|
63
|
+
* The value, built by `build` when there is none. Once built it is
|
|
64
|
+
* always answered at once: past `ttl` the old value is served while one
|
|
65
|
+
* call rebuilds it in the background.
|
|
66
|
+
*/
|
|
67
|
+
remember<T>(
|
|
68
|
+
key: string,
|
|
69
|
+
ttl: Duration,
|
|
70
|
+
build: () => T | Promise<T>,
|
|
71
|
+
options?: RememberOptions,
|
|
72
|
+
): Promise<T>;
|
|
73
|
+
/** Everything in the cache. */
|
|
74
|
+
clear(): Promise<void>;
|
|
75
|
+
readonly store: CacheStore;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export type CachePlugin = GrimoirePlugin<{ cache: Cache }> & Cache;
|
|
79
|
+
|
|
80
|
+
/** An entry as stored: the value, and until when it is fresh (remember only). */
|
|
81
|
+
interface Entry {
|
|
82
|
+
v: unknown;
|
|
83
|
+
f?: number;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const encode = (value: unknown, fresh?: number): string => {
|
|
87
|
+
const json = JSON.stringify(
|
|
88
|
+
fresh === undefined ? { v: value } : { v: value, f: fresh },
|
|
89
|
+
);
|
|
90
|
+
if (json === undefined) throw new Error("cache: the value is not JSON");
|
|
91
|
+
return json;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const decode = (raw: string | undefined): Entry | undefined => {
|
|
95
|
+
if (raw === undefined) return undefined;
|
|
96
|
+
try {
|
|
97
|
+
const e = JSON.parse(raw) as Entry;
|
|
98
|
+
return e && typeof e === "object" && "v" in e ? e : undefined;
|
|
99
|
+
} catch {
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
let current: Cache | undefined;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The app's cache, for code that has no `locals`: a job, a websocket
|
|
108
|
+
* handler, a script. Throws when no cache plugin was made.
|
|
109
|
+
*/
|
|
110
|
+
export function useCache(): Cache {
|
|
111
|
+
if (!current) {
|
|
112
|
+
throw new Error(
|
|
113
|
+
"cache: no cache plugin. Add one in sigil.config.ts: plugins: [cache()].",
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
return current;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* A cache for the app, kept in `store`: memory() by default, redis() or
|
|
121
|
+
* table() to share it between processes.
|
|
122
|
+
*
|
|
123
|
+
* ```ts
|
|
124
|
+
* // sigil.config.ts
|
|
125
|
+
* plugins: [cache(redis())]
|
|
126
|
+
* // a route
|
|
127
|
+
* const index = await locals.cache.remember("index", "1h", buildIndex);
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
export function cache(store: CacheStore = memory()): CachePlugin {
|
|
131
|
+
const ttlOf = (d: Duration | undefined) =>
|
|
132
|
+
d === undefined ? undefined : ms(d, "cache");
|
|
133
|
+
// One rebuild per key per process; another process may rebuild too,
|
|
134
|
+
// unless it lost the lock below.
|
|
135
|
+
const building = new Map<string, Promise<unknown>>();
|
|
136
|
+
|
|
137
|
+
const rebuild = <T>(
|
|
138
|
+
key: string,
|
|
139
|
+
fresh: number,
|
|
140
|
+
hard: number,
|
|
141
|
+
build: () => T | Promise<T>,
|
|
142
|
+
): Promise<T> => {
|
|
143
|
+
const running = building.get(key);
|
|
144
|
+
if (running) return running as Promise<T>;
|
|
145
|
+
const job = (async () => {
|
|
146
|
+
const value = await build();
|
|
147
|
+
await store.set(key, encode(value, Date.now() + fresh), hard);
|
|
148
|
+
return value;
|
|
149
|
+
})().finally(() => building.delete(key));
|
|
150
|
+
building.set(key, job);
|
|
151
|
+
return job;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const api: Cache = {
|
|
155
|
+
store,
|
|
156
|
+
async get<T>(key: string) {
|
|
157
|
+
return decode(await store.get(key))?.v as T | undefined;
|
|
158
|
+
},
|
|
159
|
+
async set(key, value, ttl) {
|
|
160
|
+
await store.set(key, encode(value), ttlOf(ttl));
|
|
161
|
+
},
|
|
162
|
+
add(key, value, ttl) {
|
|
163
|
+
return store.add(key, encode(value), ms(ttl, "cache"));
|
|
164
|
+
},
|
|
165
|
+
delete: (key) => store.delete(key),
|
|
166
|
+
increment: (key, window) => store.increment(key, ms(window, "cache")),
|
|
167
|
+
async remember<T>(
|
|
168
|
+
key: string,
|
|
169
|
+
ttl: Duration,
|
|
170
|
+
build: () => T | Promise<T>,
|
|
171
|
+
options: RememberOptions = {},
|
|
172
|
+
): Promise<T> {
|
|
173
|
+
const fresh = ms(ttl, "cache");
|
|
174
|
+
const stale =
|
|
175
|
+
options.staleFor === undefined ? fresh : ms(options.staleFor, "cache");
|
|
176
|
+
const hard = fresh + stale;
|
|
177
|
+
const entry = decode(await store.get(key));
|
|
178
|
+
if (!entry) return rebuild(key, fresh, hard, build);
|
|
179
|
+
if (entry.f === undefined || entry.f > Date.now()) return entry.v as T;
|
|
180
|
+
// Stale: answer now, and rebuild once, across processes too. The
|
|
181
|
+
// lock outlives a slow build at most by its own expiry.
|
|
182
|
+
if (!building.has(key)) {
|
|
183
|
+
void store
|
|
184
|
+
.add(
|
|
185
|
+
`${key}\u0000lock`,
|
|
186
|
+
"1",
|
|
187
|
+
Math.max(1_000, Math.min(fresh, 30_000)),
|
|
188
|
+
)
|
|
189
|
+
.then((won) => {
|
|
190
|
+
if (!won) return;
|
|
191
|
+
return rebuild(key, fresh, hard, build).then(() =>
|
|
192
|
+
store.delete(`${key}\u0000lock`),
|
|
193
|
+
);
|
|
194
|
+
})
|
|
195
|
+
.catch((error) => {
|
|
196
|
+
console.warn(
|
|
197
|
+
`[cache] rebuilding "${key}" failed; the old value stays: ${(error as Error).message ?? error}`,
|
|
198
|
+
);
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
return entry.v as T;
|
|
202
|
+
},
|
|
203
|
+
clear: () => store.clear(),
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const extra = store.plugin ?? {};
|
|
207
|
+
const plugin = {
|
|
208
|
+
...extra,
|
|
209
|
+
name: "cache",
|
|
210
|
+
...api,
|
|
211
|
+
async services() {
|
|
212
|
+
await store.open?.();
|
|
213
|
+
return { cache: api };
|
|
214
|
+
},
|
|
215
|
+
async onStop() {
|
|
216
|
+
await store.close?.();
|
|
217
|
+
},
|
|
218
|
+
onCoordinatorStart() {
|
|
219
|
+
if (!store.shared) {
|
|
220
|
+
console.warn(
|
|
221
|
+
`[cache] the ${store.kind} cache is per process, and this app runs several: ` +
|
|
222
|
+
"each worker has its own entries and counts. Use redis() or table() to share them.",
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
} as CachePlugin;
|
|
227
|
+
current = api;
|
|
228
|
+
return plugin;
|
|
229
|
+
}
|
package/memory.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { CacheStore } from "./index.ts";
|
|
2
|
+
|
|
3
|
+
export interface MemoryOptions {
|
|
4
|
+
/** The most entries kept; the least recently used goes first. Default 10 000. */
|
|
5
|
+
max?: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface Slot {
|
|
9
|
+
value: string;
|
|
10
|
+
/** Epoch ms, or Infinity. */
|
|
11
|
+
expires: number;
|
|
12
|
+
/** increment()'s count, for a window key. */
|
|
13
|
+
count: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Entries in this process's memory. Fast and free, and every worker has its
|
|
18
|
+
* own: under `sigil start --scale` use redis() or table().
|
|
19
|
+
*/
|
|
20
|
+
export function memory(options: MemoryOptions = {}): CacheStore {
|
|
21
|
+
const max = options.max ?? 10_000;
|
|
22
|
+
// A Map keeps insertion order: deleting and setting again moves a key to
|
|
23
|
+
// the end, so the first key is the least recently used.
|
|
24
|
+
const slots = new Map<string, Slot>();
|
|
25
|
+
|
|
26
|
+
const live = (key: string): Slot | undefined => {
|
|
27
|
+
const slot = slots.get(key);
|
|
28
|
+
if (!slot) return undefined;
|
|
29
|
+
if (slot.expires <= Date.now()) {
|
|
30
|
+
slots.delete(key);
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
slots.delete(key);
|
|
34
|
+
slots.set(key, slot);
|
|
35
|
+
return slot;
|
|
36
|
+
};
|
|
37
|
+
const put = (key: string, slot: Slot) => {
|
|
38
|
+
slots.delete(key);
|
|
39
|
+
slots.set(key, slot);
|
|
40
|
+
while (slots.size > max) {
|
|
41
|
+
const oldest = slots.keys().next().value;
|
|
42
|
+
if (oldest === undefined) break;
|
|
43
|
+
slots.delete(oldest);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const until = (ttl?: number) =>
|
|
47
|
+
ttl === undefined ? Number.POSITIVE_INFINITY : Date.now() + ttl;
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
kind: "memory",
|
|
51
|
+
shared: false,
|
|
52
|
+
async get(key) {
|
|
53
|
+
return live(key)?.value;
|
|
54
|
+
},
|
|
55
|
+
async set(key, value, ttl) {
|
|
56
|
+
put(key, { value, expires: until(ttl), count: 0 });
|
|
57
|
+
},
|
|
58
|
+
async add(key, value, ttl) {
|
|
59
|
+
if (live(key)) return false;
|
|
60
|
+
put(key, { value, expires: until(ttl), count: 0 });
|
|
61
|
+
return true;
|
|
62
|
+
},
|
|
63
|
+
async delete(key) {
|
|
64
|
+
const had = live(key) !== undefined;
|
|
65
|
+
slots.delete(key);
|
|
66
|
+
return had;
|
|
67
|
+
},
|
|
68
|
+
async increment(key, window) {
|
|
69
|
+
const slot = live(key);
|
|
70
|
+
if (slot) {
|
|
71
|
+
slot.count++;
|
|
72
|
+
return slot.count;
|
|
73
|
+
}
|
|
74
|
+
put(key, { value: "", expires: until(window), count: 1 });
|
|
75
|
+
return 1;
|
|
76
|
+
},
|
|
77
|
+
async clear() {
|
|
78
|
+
slots.clear();
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sigil-dev/cache",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"description": "A cache for Grimoire: memory, Redis (or Valkey) or a table, with remember, counters and locks",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"homepage": "https://sigil.cane1712.dev/docs/reference/cache",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://git.cane1712.dev/sigil/sigil.git",
|
|
9
|
+
"directory": "packages/cache"
|
|
10
|
+
},
|
|
11
|
+
"module": "index.ts",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"private": false,
|
|
14
|
+
"version": "0.9.31",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"grimoire",
|
|
17
|
+
"sigil",
|
|
18
|
+
"cache",
|
|
19
|
+
"redis",
|
|
20
|
+
"valkey"
|
|
21
|
+
],
|
|
22
|
+
"files": [
|
|
23
|
+
"*.ts",
|
|
24
|
+
"README.md"
|
|
25
|
+
],
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/bun": "latest"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"typescript": "^5",
|
|
31
|
+
"@sigil-dev/grimoire": "0.9.31",
|
|
32
|
+
"@sigil-dev/plugin-sql": "0.9.31",
|
|
33
|
+
"@sigil-dev/tome": "0.9.31"
|
|
34
|
+
}
|
|
35
|
+
}
|
package/redis.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type { RedisClient } from "bun";
|
|
2
|
+
import type { CacheStore } from "./index.ts";
|
|
3
|
+
|
|
4
|
+
export interface RedisOptions {
|
|
5
|
+
/** The variable the URL is read from when the server starts. Default `REDIS_URL`. */
|
|
6
|
+
env?: string;
|
|
7
|
+
/** The URL itself, instead of `env`. */
|
|
8
|
+
url?: string;
|
|
9
|
+
/** Put in front of every key, so apps can share a server. Default `"cache:"`. */
|
|
10
|
+
prefix?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** INCR, and the window's expiry with the first one, in one step: a crash between the two cannot leave a counter that never resets. */
|
|
14
|
+
const INCREMENT = `local n = redis.call('INCR', KEYS[1])
|
|
15
|
+
if n == 1 then redis.call('PEXPIRE', KEYS[1], ARGV[1]) end
|
|
16
|
+
return n`;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Entries in Redis, or anything that speaks its protocol: Valkey,
|
|
20
|
+
* Dragonfly, KeyDB. Shared by every process. The URL is read when the
|
|
21
|
+
* server starts, not when the config is loaded, so a build needs none.
|
|
22
|
+
*/
|
|
23
|
+
export function redis(options: RedisOptions = {}): CacheStore {
|
|
24
|
+
const env = options.url ? undefined : (options.env ?? "REDIS_URL");
|
|
25
|
+
const prefix = options.prefix ?? "cache:";
|
|
26
|
+
let client: RedisClient | undefined;
|
|
27
|
+
|
|
28
|
+
const connect = (): RedisClient => {
|
|
29
|
+
if (client) return client;
|
|
30
|
+
const url = options.url ?? process.env[env!];
|
|
31
|
+
if (!url) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
`cache: redis() reads its URL from ${env}, which is not set.`,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
const { RedisClient: Client } = require("bun") as typeof import("bun");
|
|
37
|
+
client = new Client(url);
|
|
38
|
+
return client;
|
|
39
|
+
};
|
|
40
|
+
const k = (key: string) => prefix + key;
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
kind: "redis",
|
|
44
|
+
shared: true,
|
|
45
|
+
plugin: env ? { requiredEnv: [env] } : {},
|
|
46
|
+
async open() {
|
|
47
|
+
await connect().send("PING", []);
|
|
48
|
+
},
|
|
49
|
+
async close() {
|
|
50
|
+
client?.close();
|
|
51
|
+
client = undefined;
|
|
52
|
+
},
|
|
53
|
+
async get(key) {
|
|
54
|
+
const value = await connect().get(k(key));
|
|
55
|
+
return value ?? undefined;
|
|
56
|
+
},
|
|
57
|
+
async set(key, value, ttl) {
|
|
58
|
+
await connect().send(
|
|
59
|
+
"SET",
|
|
60
|
+
ttl === undefined
|
|
61
|
+
? [k(key), value]
|
|
62
|
+
: [k(key), value, "PX", String(Math.max(1, Math.round(ttl)))],
|
|
63
|
+
);
|
|
64
|
+
},
|
|
65
|
+
async add(key, value, ttl) {
|
|
66
|
+
const reply = await connect().send("SET", [
|
|
67
|
+
k(key),
|
|
68
|
+
value,
|
|
69
|
+
"NX",
|
|
70
|
+
"PX",
|
|
71
|
+
String(Math.max(1, Math.round(ttl))),
|
|
72
|
+
]);
|
|
73
|
+
return reply === "OK";
|
|
74
|
+
},
|
|
75
|
+
async delete(key) {
|
|
76
|
+
return Number(await connect().send("DEL", [k(key)])) > 0;
|
|
77
|
+
},
|
|
78
|
+
async increment(key, window) {
|
|
79
|
+
const n = await connect().send("EVAL", [
|
|
80
|
+
INCREMENT,
|
|
81
|
+
"1",
|
|
82
|
+
k(key),
|
|
83
|
+
String(Math.max(1, Math.round(window))),
|
|
84
|
+
]);
|
|
85
|
+
return Number(n);
|
|
86
|
+
},
|
|
87
|
+
async clear() {
|
|
88
|
+
// Only this app's keys: SCAN by prefix, never FLUSHDB.
|
|
89
|
+
const c = connect();
|
|
90
|
+
let cursor = "0";
|
|
91
|
+
do {
|
|
92
|
+
const [next, keys] = (await c.send("SCAN", [
|
|
93
|
+
cursor,
|
|
94
|
+
"MATCH",
|
|
95
|
+
`${prefix}*`,
|
|
96
|
+
"COUNT",
|
|
97
|
+
"500",
|
|
98
|
+
])) as [string, string[]];
|
|
99
|
+
if (keys.length) await c.send("DEL", keys);
|
|
100
|
+
cursor = String(next);
|
|
101
|
+
} while (cursor !== "0");
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
}
|
package/table.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { table as defineTable, t, tomeFor } from "@sigil-dev/tome";
|
|
2
|
+
import type { CacheStore } from "./index.ts";
|
|
3
|
+
|
|
4
|
+
export interface TableOptions {
|
|
5
|
+
/** The plugin-sql database whose tome holds the table. Default "db". */
|
|
6
|
+
database?: string;
|
|
7
|
+
/** The table's name. Default "sigil_cache". */
|
|
8
|
+
name?: string;
|
|
9
|
+
/** How often expired rows are deleted. Default every 10 minutes. */
|
|
10
|
+
sweepEvery?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const made = new Map<string, ReturnType<typeof define>>();
|
|
14
|
+
|
|
15
|
+
function define(name: string) {
|
|
16
|
+
return defineTable(name, {
|
|
17
|
+
key: t.text().primaryKey(),
|
|
18
|
+
value: t.text(),
|
|
19
|
+
count: t.integer().default(0),
|
|
20
|
+
// epoch ms, or null for no expiry
|
|
21
|
+
expires: t.bigint().nullable(),
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The cache's table. One object per name: tome finds a table by identity. */
|
|
26
|
+
export function cacheTable(name = "sigil_cache") {
|
|
27
|
+
let found = made.get(name);
|
|
28
|
+
if (!found) made.set(name, (found = define(name)));
|
|
29
|
+
return found;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type Pool = {
|
|
33
|
+
unsafe(sql: string, params?: unknown[]): Promise<Record<string, unknown>[]>;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Entries in a table of the app's own database, through tome: shared by
|
|
38
|
+
* every process, with nothing else to run. Like Rails' Solid Cache. Slower
|
|
39
|
+
* than redis() per call, and plenty for most apps.
|
|
40
|
+
*
|
|
41
|
+
* `sigil db generate` creates the table, as for any plugin's tables.
|
|
42
|
+
*/
|
|
43
|
+
export function table(options: TableOptions = {}): CacheStore {
|
|
44
|
+
const database = options.database ?? "db";
|
|
45
|
+
const name = options.name ?? "sigil_cache";
|
|
46
|
+
if (!/^[a-z_][a-z0-9_]*$/.test(name)) {
|
|
47
|
+
throw new Error(`cache: "${name}" is not a plain table name`);
|
|
48
|
+
}
|
|
49
|
+
const T = `"${name}"`;
|
|
50
|
+
let sweeper: ReturnType<typeof setInterval> | undefined;
|
|
51
|
+
|
|
52
|
+
// The pool, never the request's transaction: a cache write must not be
|
|
53
|
+
// rolled back with the request that made it, nor wait on its locks.
|
|
54
|
+
const pool = (): Pool => {
|
|
55
|
+
const db = tomeFor(database);
|
|
56
|
+
if (!db) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
`cache: table() needs a tome over the "${database}" database. Register it first: plugins: [db.plugin, cache(table())].`,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
return db.database.pool() as unknown as Pool;
|
|
62
|
+
};
|
|
63
|
+
const run = (sql: string, params: unknown[]) => pool().unsafe(sql, params);
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
kind: "table",
|
|
67
|
+
shared: true,
|
|
68
|
+
plugin: {
|
|
69
|
+
after: ["tome"],
|
|
70
|
+
tables: { cache: cacheTable(name) },
|
|
71
|
+
database,
|
|
72
|
+
},
|
|
73
|
+
async open() {
|
|
74
|
+
const every = options.sweepEvery ?? 600_000;
|
|
75
|
+
sweeper = setInterval(() => {
|
|
76
|
+
run(
|
|
77
|
+
`delete from ${T} where "expires" is not null and "expires" <= $1`,
|
|
78
|
+
[Date.now()],
|
|
79
|
+
).catch(() => {});
|
|
80
|
+
}, every);
|
|
81
|
+
sweeper.unref?.();
|
|
82
|
+
},
|
|
83
|
+
async close() {
|
|
84
|
+
if (sweeper) clearInterval(sweeper);
|
|
85
|
+
sweeper = undefined;
|
|
86
|
+
},
|
|
87
|
+
async get(key) {
|
|
88
|
+
const rows = await run(
|
|
89
|
+
`select "value" from ${T} where "key" = $1 and ("expires" is null or "expires" > $2)`,
|
|
90
|
+
[key, Date.now()],
|
|
91
|
+
);
|
|
92
|
+
return rows[0]?.value as string | undefined;
|
|
93
|
+
},
|
|
94
|
+
async set(key, value, ttl) {
|
|
95
|
+
await run(
|
|
96
|
+
`insert into ${T} ("key", "value", "count", "expires") values ($1, $2, 0, $3)
|
|
97
|
+
on conflict ("key") do update set "value" = excluded."value", "count" = 0, "expires" = excluded."expires"`,
|
|
98
|
+
[key, value, ttl === undefined ? null : Date.now() + ttl],
|
|
99
|
+
);
|
|
100
|
+
},
|
|
101
|
+
async add(key, value, ttl) {
|
|
102
|
+
const now = Date.now();
|
|
103
|
+
const rows = await run(
|
|
104
|
+
`insert into ${T} ("key", "value", "count", "expires") values ($1, $2, 0, $3)
|
|
105
|
+
on conflict ("key") do update set "value" = excluded."value", "count" = 0, "expires" = excluded."expires"
|
|
106
|
+
where ${T}."expires" is not null and ${T}."expires" <= $4
|
|
107
|
+
returning "key"`,
|
|
108
|
+
[key, value, now + ttl, now],
|
|
109
|
+
);
|
|
110
|
+
return rows.length > 0;
|
|
111
|
+
},
|
|
112
|
+
async delete(key) {
|
|
113
|
+
const rows = await run(
|
|
114
|
+
`delete from ${T} where "key" = $1 returning "expires"`,
|
|
115
|
+
[key],
|
|
116
|
+
);
|
|
117
|
+
const expires = rows[0]?.expires;
|
|
118
|
+
return (
|
|
119
|
+
rows.length > 0 && (expires === null || Number(expires) > Date.now())
|
|
120
|
+
);
|
|
121
|
+
},
|
|
122
|
+
async increment(key, window) {
|
|
123
|
+
const now = Date.now();
|
|
124
|
+
// One statement, so two processes counting at once both land.
|
|
125
|
+
const rows = await run(
|
|
126
|
+
`insert into ${T} ("key", "value", "count", "expires") values ($1, '', 1, $2)
|
|
127
|
+
on conflict ("key") do update set
|
|
128
|
+
"count" = case when ${T}."expires" is not null and ${T}."expires" <= $3 then 1 else ${T}."count" + 1 end,
|
|
129
|
+
"expires" = case when ${T}."expires" is not null and ${T}."expires" <= $3 then excluded."expires" else ${T}."expires" end
|
|
130
|
+
returning "count"`,
|
|
131
|
+
[key, now + window, now],
|
|
132
|
+
);
|
|
133
|
+
return Number(rows[0]?.count);
|
|
134
|
+
},
|
|
135
|
+
async clear() {
|
|
136
|
+
await run(`delete from ${T}`, []);
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
}
|