Skip to content

Commit

Permalink
Add Deno platform cache to codegen service
Browse files Browse the repository at this point in the history
  • Loading branch information
danopia committed May 10, 2024
1 parent 2203bc5 commit b1915c3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
48 changes: 48 additions & 0 deletions generation/deploy/cache-platform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Cache } from "https://deno.land/x/httpcache@0.1.2/mod.ts";

// https://docs.deno.com/deploy/manual/edge-cache

export async function platformCache(
name = 'http-policy-cache',
): Promise<Cache> {

const nativeCache = await globalThis.caches.open(name);

return new Cache({

async get(url) {
const cached = await nativeCache.match(url);
if (!cached) return undefined;

const policyHeader = cached.headers.get('cache-policy');
if (!policyHeader) {
console.warn('WARN: platform cache lacked policy', cached);
return undefined;
}

const BodyBuffer = new Uint8Array(await cached.arrayBuffer());

// const idx = BodyBuffer.indexOf(10);
return {
// policy: JSON.parse(new TextDecoder().decode(BodyBuffer.slice(0, idx))),
policy: JSON.parse(policyHeader),
// body: BodyBuffer.slice(idx+1),
body: BodyBuffer,
};
},

async set(url, resp) {
await nativeCache.put(url, new Response(resp.body, {
headers: {
'cache-policy': JSON.stringify(resp.policy),
},
}));
},

async delete(url) {
await nativeCache.delete(url);
},

close() {},
});
}
14 changes: 9 additions & 5 deletions generation/deploy/cache.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { inMemoryCache } from "https://deno.land/x/httpcache@0.1.2/in_memory.ts";
import type { Cache } from "https://deno.land/x/httpcache@0.1.2/mod.ts";
import { s3Cache } from "./cache-s3.ts";
import { S3 } from "./s3-api.ts";
import { ApiFactory } from "../../lib/client/mod.ts";
import { AsyncTracer, Span } from "./tracer.ts";

// can maybe replace this whole dep with deno edge cache once it's out of beta
import type { Cache } from "https://deno.land/x/httpcache@0.1.2/mod.ts";
import { inMemoryCache } from "https://deno.land/x/httpcache@0.1.2/in_memory.ts";
import { s3Cache } from "./cache-s3.ts";
import { platformCache } from "./cache-platform.ts";

const s3Api = new ApiFactory({
region: Deno.env.get('HTTPCACHE_S3_REGION') || 'us-east-2',
}).makeNew(S3);

const caches: Array<Cache> = [
inMemoryCache(40),
await platformCache(),
s3Cache(s3Api, Deno.env.get('HTTPCACHE_S3_BUCKET') || 'deno-httpcache'),
];
const cacheLabels = ['in-memory', 's3'];
const cacheLabels = ['in-memory', 'platform', 's3'];

import { AsyncTracer, Span } from "./tracer.ts";
const tracer = new AsyncTracer('cached-fetch');

export async function cachedFetch(mode: 'immutable' | 'mutable', label: string, url: string) {
Expand Down

1 comment on commit b1915c3

@deno-deploy
Copy link

@deno-deploy deno-deploy bot commented on b1915c3 May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to deploy:

UNCAUGHT_EXCEPTION

TypeError: Cannot read properties of undefined (reading 'open')
    at platformCache (https://raw.githubusercontent.com/cloudydeno/deno-aws_api/b1915c3c6bdb2714ed23560275347f9908dac1f7/generation/deploy/cache-platform.ts:9:47)
    at https://raw.githubusercontent.com/cloudydeno/deno-aws_api/b1915c3c6bdb2714ed23560275347f9908dac1f7/generation/deploy/cache.ts:17:9

Please sign in to comment.