Skip to content

Commit

Permalink
Revert "Add cache"
Browse files Browse the repository at this point in the history
caches doesn't work on deno deploy

This reverts commit f6ac5dd.
  • Loading branch information
AnInternetTroll committed Apr 22, 2024
1 parent f6ac5dd commit 60ade76
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 33 deletions.
2 changes: 1 addition & 1 deletion main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S deno run --allow-net --allow-read=.env --no-check --watch --location=http://speedrunbot-slash/
#!/usr/bin/env -S deno run --allow-net --allow-read=.env --no-check --watch
import { serve } from "./deps_server.ts";
import { config } from "./src/config.ts";
import { handler } from "./src/pages/mod.tsx";
Expand Down
33 changes: 1 addition & 32 deletions src/srcom/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { delay, TimeDelta } from "../../deps_general.ts";
import { GetSearch, Language } from "../../deps_server.ts";
export const SRC_URL = "https://www.speedrun.com";
export const SRC_API = `${SRC_URL}/api/v1`;
export const CACHE_KEY = `srcom-v1`;

export interface Opts {
outputType?: MarkupType;
Expand Down Expand Up @@ -36,46 +35,16 @@ export async function fetch(
input: string | URL | Request,
init?: RequestInit | undefined,
): Promise<Response> {
// Caching logic summed up
// 1. Save every response in the cache, and by extension their date and etag values
// 2. When we need to make a new call, check if we have something in
// our cache and if it's not a day old, then ask speedrun.com if what
// we have is still actual (Send ETag as If-None-Match)
// 3. If speedrun.com says what we have is good then we use the cache,
// if not then we ask speedrun.com for something new and save that in
// cache instead
const cache = await caches.open(CACHE_KEY);
const req = new Request(input, {
const res = await globalThis.fetch(input, {
...init,
headers: {
...init?.headers,
"User-Agent": "aninternettroll/speedrunbot-slash",
},
});
const match = await cache.match(req);
const date = match?.headers.get("date");
const dayAgo = new Date();
dayAgo.setDate(dayAgo.getDate() - 1);
const cacheOutdated = !!date && new Date(date).getTime() < dayAgo.getTime();
if (!match || cacheOutdated) {
const res = await globalThis.fetch(req);
if (res.status >= 500) {
throw new SpeedrunComError(`Speedrun.com panicked ${res.status}`);
} else {
await cache.put(req, res.clone());
return res;
}
}
const etag = match.headers.get("ETag");
if (etag) req.headers.append("If-None-Match", etag);
const res = await globalThis.fetch(req);
if (res.status >= 500) {
throw new SpeedrunComError(`Speedrun.com panicked ${res.status}`);
} else if (res.status === 304) {
return match;
} else {
req.headers.delete("ETag");
await cache.put(req, res);
return res;
}
}
Expand Down

0 comments on commit 60ade76

Please sign in to comment.