-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.ts
More file actions
132 lines (118 loc) · 3.42 KB
/
cache.ts
File metadata and controls
132 lines (118 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { createHash } from "node:crypto";
import {
existsSync,
mkdirSync,
readdirSync,
readFileSync,
statSync,
unlinkSync,
writeFileSync,
} from "node:fs";
import { dirname, join } from "node:path";
/** Interface for cache operations, enabling dependency injection in tests. */
export interface CacheManagerInterface {
getIndexPath(version: string): string;
getSearchIndexPath(version: string): string;
getDocPath(version: string, docPath: string): string;
isValid(path: string, ttlMs: number): boolean;
read<T>(path: string): T | null;
write<T>(path: string, data: T): void;
runGarbageCollection(
indexTtlMs: number,
docTtlMs: number,
): { scanned: number; deleted: number; errors: number };
}
function hash(input: string): string {
return createHash("sha1").update(input).digest("hex");
}
/**
* Creates a file-based cache manager for storing documentation.
* Uses SHA-1 hashes for document filenames to handle arbitrary paths.
* @param cacheRoot - Root directory for all cached files.
* @returns A CacheManagerInterface implementation.
*/
export function CacheManager(cacheRoot: string): CacheManagerInterface {
if (!existsSync(cacheRoot)) {
mkdirSync(cacheRoot, { recursive: true });
}
return {
getIndexPath(version: string): string {
return join(cacheRoot, `python-${version}.json`);
},
getSearchIndexPath(version: string): string {
return join(cacheRoot, `python-${version}-search-index.json`);
},
getDocPath(version: string, docPath: string): string {
return join(cacheRoot, "docs", version, `${hash(docPath)}.json`);
},
isValid(path: string, ttlMs: number): boolean {
try {
return Date.now() - statSync(path).mtimeMs < ttlMs;
} catch {
return false;
}
},
read<T>(path: string): T | null {
try {
return JSON.parse(readFileSync(path, "utf-8")) as T;
} catch {
return null;
}
},
write<T>(path: string, data: T): void {
const dir = dirname(path);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
writeFileSync(path, JSON.stringify(data));
},
runGarbageCollection(
indexTtlMs: number,
docTtlMs: number,
): { scanned: number; deleted: number; errors: number } {
let scanned = 0;
let deleted = 0;
let errors = 0;
const tryDelete = (filePath: string, ttl: number) => {
scanned++;
if (!this.isValid(filePath, ttl)) {
try {
unlinkSync(filePath);
deleted++;
} catch {
errors++;
}
}
};
if (existsSync(cacheRoot)) {
try {
for (const file of readdirSync(cacheRoot)) {
if (file.endsWith(".json")) {
tryDelete(join(cacheRoot, file), indexTtlMs);
}
}
} catch {
errors++;
}
}
const docsRoot = join(cacheRoot, "docs");
if (existsSync(docsRoot)) {
try {
for (const version of readdirSync(docsRoot)) {
const versionDir = join(docsRoot, version);
try {
for (const doc of readdirSync(versionDir)) {
tryDelete(join(versionDir, doc), docTtlMs);
}
} catch {
errors++;
}
}
} catch {
errors++;
}
}
return { scanned, deleted, errors };
},
};
}