-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathmod.ts
More file actions
74 lines (58 loc) · 2.06 KB
/
Copy pathmod.ts
File metadata and controls
74 lines (58 loc) · 2.06 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
import { withInstrumentation } from "./common.ts";
import { isFileSystemAvailable } from "./fileSystem.ts";
import { caches as headersCache } from "./headerscache.ts";
import {
caches as redisCache,
isAvailable as isRedisCacheAvailable,
} from "./redis.ts";
import { createTieredCache } from "./tiered.ts";
import { caches as lruCache } from "./lrucache.ts";
import { caches as fileSystem } from "./fileSystem.ts";
import { caches as inMemoryCache } from "./inMemoryCache.ts";
export const ENABLE_LOADER_CACHE: boolean =
Deno.env.get("ENABLE_LOADER_CACHE") !== "false";
const DEFAULT_CACHE_ENGINE = "CACHE_API";
const WEB_CACHE_ENGINES: CacheEngine[] = Deno.env.has("WEB_CACHE_ENGINE")
? Deno.env.get("WEB_CACHE_ENGINE")!.split(",") as CacheEngine[]
: [DEFAULT_CACHE_ENGINE];
export interface CacheStorageOption {
implementation: CacheStorage;
isAvailable: boolean;
}
export type CacheEngine =
| "CACHE_API"
| "REDIS"
| "FILE_SYSTEM";
export const cacheImplByEngine: Record<CacheEngine, CacheStorageOption> = {
CACHE_API: {
implementation: headersCache(globalThis.caches),
isAvailable: typeof globalThis.caches !== "undefined",
},
FILE_SYSTEM: {
implementation: headersCache(
lruCache(createTieredCache(inMemoryCache, fileSystem)),
),
isAvailable: isFileSystemAvailable,
},
REDIS: {
implementation: redisCache,
isAvailable: isRedisCacheAvailable,
},
};
for (const [engine, cache] of Object.entries(cacheImplByEngine)) {
cacheImplByEngine[engine as CacheEngine] = {
...cache,
implementation: withInstrumentation(cache.implementation, engine),
};
}
const eligibleCacheImplementations = WEB_CACHE_ENGINES
.map((engine) => cacheImplByEngine[engine])
.filter((engine) => engine?.isAvailable)
.map((engine) => engine.implementation);
const getCacheStorage = (): CacheStorage | undefined => {
if (eligibleCacheImplementations.length === 0) {
return cacheImplByEngine[DEFAULT_CACHE_ENGINE].implementation;
}
return createTieredCache(...eligibleCacheImplementations);
};
export const caches = getCacheStorage();