deno.land / x / lume@v2.1.4 / core / cache.ts

نووسراو ببینە
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
import { emptyDir, ensureDir } from "../deps/fs.ts";import { posix } from "../deps/path.ts";import { md5 } from "./utils/digest.ts";
export interface Options { /** The folder to load the files from */ folder?: string;}
/** * Class to cache the content transformations (like transform_images manipulations) */export default class Cache { #folder: string;
constructor(options: Options = {}) { this.#folder = options.folder || "_cache"; }
async set( content: string | Uint8Array, key: unknown, result: string | Uint8Array, ): Promise<void> { const [dir, file] = await paths(content, key);
await ensureDir(posix.join(this.#folder, dir));
if (typeof result === "string") { await Deno.writeTextFile(posix.join(this.#folder, dir, file), result); } else { await Deno.writeFile(posix.join(this.#folder, dir, file), result); } }
async get( content: Uint8Array | string, key: unknown, ): Promise<Uint8Array | undefined> { const [dir, file] = await paths(content, key);
try { return await Deno.readFile(posix.join(this.#folder, dir, file)); } catch { // Ignore } }
async getText(content: string, key: unknown): Promise<string | undefined> { const [dir, file] = await paths(content, key);
try { return await Deno.readTextFile(posix.join(this.#folder, dir, file)); } catch { // Ignore } }
async clear(): Promise<void> { await emptyDir(this.#folder); }}
function paths( content: string | Uint8Array, key: unknown,): Promise<[string, string]> { return Promise.all([ md5(content), md5(JSON.stringify(key)), ]);}
lume

Version Info

Tagged at
7 months ago