Repository navigation
Expand file tree
/
Copy pathmemory.ts
More file actions
52 lines (47 loc) · 1.6 KB
/
Copy pathmemory.ts
File metadata and controls
52 lines (47 loc) · 1.6 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
/**
* In-memory store for cached content. Can be useful for certain types of
* testing, but your entire cache will be lost when the process exits.
* As an alternative, consider the `fs` store, and commit the generated
* files to your project's repository / source control.
* @module
*/
import type { FetchCacheOptions } from "../fetch-cache.js";
import type { FMCCacheContent } from "../store.js";
import FMCStore from "../store.js";
// TODO LRU cache
/**
* Used to instantiate a new in-memory store.
*
* @param options (usually none)
* @returns memory store instance, to pass to `createCachingMock`
* @example
* ```ts
* import createFetchCache from "fetch-mock-cache"; // or /runtimes/deno.js etc
* import Store from "fetch-mock-cache/stores/memory";
* const fetchCache = createFetchCache({ Store });
* ```
*/
export default class FMCMemoryStore extends FMCStore {
/** Cache entries keyed by the store-generated request id. */
store: Map<string, FMCCacheContent> = new Map();
/**
* Returns cached content for the request, or `undefined` on a cache miss.
*/
override async fetchContent(
req: FMCCacheContent["request"],
options?: FetchCacheOptions,
): Promise<FMCCacheContent | undefined> {
const key = await this.idFromRequest(req, options);
return this.store.get(key);
}
/**
* Stores content in memory under the request's generated cache id.
*/
override async storeContent(
content: FMCCacheContent,
options?: FetchCacheOptions,
): Promise<void> {
const key = await this.idFromRequest(content.request, options);
this.store.set(key, content);
}
}