layercache 1.0.1 → 1.0.2

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/dist/cli.js ADDED
@@ -0,0 +1,96 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ RedisTagIndex
4
+ } from "./chunk-IILH5XTS.js";
5
+
6
+ // src/cli.ts
7
+ import Redis from "ioredis";
8
+ async function main(argv = process.argv.slice(2)) {
9
+ const args = parseArgs(argv);
10
+ if (!args.command || !args.redisUrl) {
11
+ printUsage();
12
+ process.exitCode = 1;
13
+ return;
14
+ }
15
+ const redis = new Redis(args.redisUrl);
16
+ try {
17
+ if (args.command === "stats") {
18
+ const keys = await scanKeys(redis, args.pattern ?? "*");
19
+ process.stdout.write(`${JSON.stringify({ totalKeys: keys.length, pattern: args.pattern ?? "*" }, null, 2)}
20
+ `);
21
+ return;
22
+ }
23
+ if (args.command === "keys") {
24
+ const keys = await scanKeys(redis, args.pattern ?? "*");
25
+ process.stdout.write(`${keys.join("\n")}
26
+ `);
27
+ return;
28
+ }
29
+ if (args.command === "invalidate") {
30
+ if (args.tag) {
31
+ const tagIndex = new RedisTagIndex({ client: redis, prefix: args.tagIndexPrefix ?? "layercache:tag-index" });
32
+ const keys2 = await tagIndex.keysForTag(args.tag);
33
+ if (keys2.length > 0) {
34
+ await redis.del(...keys2);
35
+ }
36
+ process.stdout.write(`${JSON.stringify({ deletedKeys: keys2.length, tag: args.tag }, null, 2)}
37
+ `);
38
+ return;
39
+ }
40
+ const keys = await scanKeys(redis, args.pattern ?? "*");
41
+ if (keys.length > 0) {
42
+ await redis.del(...keys);
43
+ }
44
+ process.stdout.write(`${JSON.stringify({ deletedKeys: keys.length, pattern: args.pattern ?? "*" }, null, 2)}
45
+ `);
46
+ return;
47
+ }
48
+ printUsage();
49
+ process.exitCode = 1;
50
+ } finally {
51
+ redis.disconnect();
52
+ }
53
+ }
54
+ function parseArgs(argv) {
55
+ const [command, ...rest] = argv;
56
+ const parsed = { command };
57
+ for (let index = 0; index < rest.length; index += 1) {
58
+ const token = rest[index];
59
+ const value = rest[index + 1];
60
+ if (token === "--redis") {
61
+ parsed.redisUrl = value;
62
+ index += 1;
63
+ } else if (token === "--pattern") {
64
+ parsed.pattern = value;
65
+ index += 1;
66
+ } else if (token === "--tag") {
67
+ parsed.tag = value;
68
+ index += 1;
69
+ } else if (token === "--tag-index-prefix") {
70
+ parsed.tagIndexPrefix = value;
71
+ index += 1;
72
+ }
73
+ }
74
+ return parsed;
75
+ }
76
+ async function scanKeys(redis, pattern) {
77
+ const keys = [];
78
+ let cursor = "0";
79
+ do {
80
+ const [nextCursor, batch] = await redis.scan(cursor, "MATCH", pattern, "COUNT", 100);
81
+ cursor = nextCursor;
82
+ keys.push(...batch);
83
+ } while (cursor !== "0");
84
+ return keys;
85
+ }
86
+ function printUsage() {
87
+ process.stdout.write(
88
+ "Usage:\n layercache stats --redis <url> [--pattern <glob>]\n layercache keys --redis <url> [--pattern <glob>]\n layercache invalidate --redis <url> [--pattern <glob> | --tag <tag>] [--tag-index-prefix <prefix>]\n"
89
+ );
90
+ }
91
+ if (process.argv[1]?.includes("cli.")) {
92
+ void main();
93
+ }
94
+ export {
95
+ main
96
+ };