Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: single cache file #54

Merged
merged 24 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions deno_dir_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ export * from "./glob.ts";
deps.set(url, expectedHeaders, new TextEncoder().encode(expectedText));
const headers = deps.getHeaders(url)!;
assertEquals(headers, expectedHeaders);
const text = new TextDecoder().decode(deps.get(url, undefined));
const cacheEntry = deps.get(url)!;
assertEquals(cacheEntry.headers, expectedHeaders);
const text = new TextDecoder().decode(cacheEntry.content);
assertEquals(text, expectedText);

// ok
Expand All @@ -78,17 +80,15 @@ export * from "./glob.ts";
});

Deno.test({
name: "HttpCache - global cache - allowCopyGlobalToLocal",
name: "HttpCache - global cache - get",
async fn() {
const denoDir = new DenoDir();
const url = new URL("https://deno.land/std@0.140.0/path/mod.ts");
const deps = await denoDir.createHttpCache();
// disallow will still work because we're using a global cache
// which is not affected by this option
const text = await deps.get(url, {
allowCopyGlobalToLocal: false,
});
assertEquals(text!.length, 820);
const entry = await deps.get(url);
assertEquals(entry!.content.length, 820);
},
});

Expand All @@ -98,22 +98,23 @@ Deno.test({
await withTempDir(async (tempDir) => {
const denoDir = new DenoDir();
const url = new URL("https://deno.land/std@0.140.0/path/mod.ts");
const deps = await denoDir.createHttpCache({
vendorRoot: tempDir,
});
// disallow

// disallow copy from global to local because readonly
{
const text = deps.get(url, {
allowCopyGlobalToLocal: false,
using deps = await denoDir.createHttpCache({
vendorRoot: tempDir,
readOnly: true,
});
const text = deps.get(url);
assertEquals(text, undefined);
}
// allow
// this should be fine though
{
const text = deps.get(url, {
allowCopyGlobalToLocal: true,
using deps = await denoDir.createHttpCache({
vendorRoot: tempDir,
});
assertEquals(text!.length, 820);
const entry = deps.get(url);
assertEquals(entry!.content.length, 820);
}
});
},
Expand Down
14 changes: 5 additions & 9 deletions file_fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,27 +168,23 @@ export class FileFetcher {
options: ResolvedFetchOptions,
httpCache: HttpCache,
): LoadResponse | undefined {
const headers = httpCache.getHeaders(specifier);
if (!headers) {
const cacheEntry = httpCache.get(specifier, options);
if (!cacheEntry) {
return undefined;
}
const location = headers["location"];
const location = cacheEntry.headers["location"];
if (location != null && location.length > 0) {
const redirect = new URL(location, specifier);
return {
kind: "redirect",
specifier: redirect.toString(),
};
}
const content = httpCache.get(specifier, options);
if (content == null) {
return undefined;
}
return {
kind: "module",
specifier: specifier.toString(),
headers,
content,
headers: cacheEntry.headers,
content: cacheEntry.content,
};
}

Expand Down
18 changes: 12 additions & 6 deletions http_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ export interface HttpCacheGetOptions {
* global cache (DENO_DIR) and not the local cache (vendor folder).
*/
checksum?: string;
/** Allow copying from the global to the local cache (vendor folder). */
allowCopyGlobalToLocal?: boolean;
}

export interface HttpCacheEntry {
headers: Record<string, string>;
content: Uint8Array;
}

export class HttpCache implements Disposable {
Expand Down Expand Up @@ -48,7 +51,11 @@ export class HttpCache implements Disposable {

let cache: LocalHttpCache | GlobalHttpCache;
if (options.vendorRoot != null) {
cache = LocalHttpCache.new(options.vendorRoot, options.root);
cache = LocalHttpCache.new(
options.vendorRoot,
options.root,
/* allow global to local copy */ !options.readOnly,
);
} else {
cache = GlobalHttpCache.new(options.root);
}
Expand All @@ -73,11 +80,10 @@ export class HttpCache implements Disposable {
get(
url: URL,
options?: HttpCacheGetOptions,
): Uint8Array | undefined {
const data = this.#cache.getFileBytes(
): HttpCacheEntry | undefined {
const data = this.#cache.get(
url.toString(),
options?.checksum,
options?.allowCopyGlobalToLocal ?? true,
);
return data == null ? undefined : data;
}
Expand Down
13 changes: 6 additions & 7 deletions lib/deno_cache_dir.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ export class GlobalHttpCache {
getHeaders(url: string): any;
/**
* @param {string} url
* @param {string | undefined} maybe_checksum
* @param {boolean} allow_global_to_local_copy
* @param {string | undefined} [maybe_checksum]
* @returns {any}
*/
getFileBytes(url: string, maybe_checksum: string | undefined, allow_global_to_local_copy: boolean): any;
get(url: string, maybe_checksum?: string): any;
/**
* @param {string} url
* @param {any} headers
Expand All @@ -64,21 +63,21 @@ export class LocalHttpCache {
/**
* @param {string} local_path
* @param {string} global_path
* @param {boolean} allow_global_to_local_copy
* @returns {LocalHttpCache}
*/
static new(local_path: string, global_path: string): LocalHttpCache;
static new(local_path: string, global_path: string, allow_global_to_local_copy: boolean): LocalHttpCache;
/**
* @param {string} url
* @returns {any}
*/
getHeaders(url: string): any;
/**
* @param {string} url
* @param {string | undefined} maybe_checksum
* @param {boolean} allow_global_to_local_copy
* @param {string | undefined} [maybe_checksum]
* @returns {any}
*/
getFileBytes(url: string, maybe_checksum: string | undefined, allow_global_to_local_copy: boolean): any;
get(url: string, maybe_checksum?: string): any;
/**
* @param {string} url
* @param {any} headers
Expand Down
Loading