Skip to content

Commit 46e38c9

Browse files
committed
feat: manually delete cache
1 parent 23d9440 commit 46e38c9

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/cloudflare-worker/cache.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@ import { Cache as Base } from "../core/types";
33
export class Cache implements Base {
44
private cache;
55

6-
constructor() {
6+
constructor(private expiration = 60) {
77
this.cache = caches.open("leetcode");
88
}
99

1010
async put(key: string, value: any, options?: { expire?: number }) {
1111
key = this.urlify(key);
1212

1313
if (value instanceof Response) {
14-
value.headers.set("Cache-Control", `public, max-age=${options?.expire ?? 60}`);
14+
value.headers.set(
15+
"Cache-Control",
16+
`public, max-age=${options?.expire ?? this.expiration}`,
17+
);
1518
await (await this.cache).put(key, value);
1619
} else {
1720
const dummy: Response = new Response(JSON.stringify(value), {
1821
headers: {
1922
"Content-Type": "application/x-custom-data",
20-
"Cache-Control": `public, max-age=${options?.expire ?? 60}`,
23+
"Cache-Control": `public, max-age=${options?.expire ?? this.expiration}`,
2124
},
2225
});
2326

@@ -41,6 +44,12 @@ export class Cache implements Base {
4144
return null;
4245
}
4346

47+
async delete(key: string) {
48+
key = this.urlify(key);
49+
50+
return (await this.cache).delete(key);
51+
}
52+
4453
private urlify(key: string) {
4554
try {
4655
return new URL(key).href;

src/cloudflare-worker/handler.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function sanitize(config: Record<string, string>): Config {
3333
font: "baloo_2",
3434
animation: true,
3535
theme: { light: "light", dark: "dark" },
36+
cache: 60,
3637
};
3738

3839
if (!config.username?.trim()) {
@@ -110,13 +111,14 @@ function sanitize(config: Record<string, string>): Config {
110111
sanitized.extensions.push(RemoteStyleExtension);
111112
}
112113

114+
if (config.cache && parseInt(config.cache) >= 0 && parseInt(config.cache) <= 60 * 60 * 24 * 7) {
115+
sanitized.cache = parseInt(config.cache);
116+
}
117+
113118
return sanitized;
114119
}
115120

116121
async function generate(config: Record<string, string>): Promise<Response> {
117-
const generator = new Generator(new Cache());
118-
generator.verbose = true;
119-
120122
let sanitized: Config;
121123
try {
122124
sanitized = sanitize(config);
@@ -131,6 +133,9 @@ async function generate(config: Record<string, string>): Promise<Response> {
131133
const cache_header =
132134
`max-age=${cache_time}` + (cache_time <= 0 ? ", no-store, no-cache" : ", public");
133135

136+
const generator = new Generator(new Cache(sanitized.cache));
137+
generator.verbose = true;
138+
134139
const headers = new Header().add("cors", "svg");
135140
headers.set("cache-control", cache_header);
136141

@@ -157,6 +162,18 @@ router.get("*", async ({ query }: { query: Record<string, string> }) => {
157162
return await generate(query);
158163
});
159164

165+
router.delete("/:site/:username", async ({ params }) => {
166+
if (params?.site && params?.username) {
167+
const site = params.site.toLowerCase();
168+
const username = params.username.toLowerCase();
169+
const cache = new Cache(60);
170+
return new Response(
171+
JSON.stringify({ success: await cache.delete(`data-${username}-${site}`) }, null, 4),
172+
{ headers: new Header().add("cors", "json") },
173+
);
174+
}
175+
});
176+
160177
// 404 for all other routes
161178
router.all("*", () => new Response("Not Found.", { status: 404 }));
162179

0 commit comments

Comments
 (0)