Skip to content

Commit

Permalink
refactor: drop user-land expires in favor of cache built-ins
Browse files Browse the repository at this point in the history
  • Loading branch information
esroyo committed Mar 3, 2024
1 parent d80898a commit 981346a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 24 deletions.
11 changes: 3 additions & 8 deletions src/cache/deno-kv-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,17 @@ export class DenoKvCache implements Cache {
const settledKv = await this.kv;
const blob = await kvGet(settledKv, ['cache', ...key]);
const value = blob && JSON.parse(new TextDecoder().decode(blob));
const isValidCacheEntry = !!(
value &&
value.expires &&
value.expires > Date.now()
);
return isValidCacheEntry ? value : null;
return value || null;
}

public async set(key: string[], value: ResponseProps): Promise<void> {
const expires = calcExpires(value.headers);
const blob = new TextEncoder().encode(JSON.stringify({
...value,
expires: calcExpires(value.headers).expires,
headers: Object.fromEntries(value.headers.entries()),
}));
const settledKv = await this.kv;
await kvSet(settledKv, ['cache', ...key], blob);
await kvSet(settledKv, ['cache', ...key], blob, { expireIn: expires });
}

public async close(): Promise<void> {
Expand Down
12 changes: 3 additions & 9 deletions src/cache/redis-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,18 @@ export class RedisCache implements Cache {
const settledRedis = await this.redis;
const data = await settledRedis.get(this.serializeKey(key));
const value = data && JSON.parse(data);
const isValidCacheEntry = !!(
value &&
value.expires &&
value.expires > Date.now()
);
return isValidCacheEntry ? value : null;
return value || null;
}

public async set(key: string[], value: ResponseProps): Promise<void> {
const control = calcExpires(value.headers);
const expires = calcExpires(value.headers);
const data = JSON.stringify({
...value,
expires: control.expires,
headers: Object.fromEntries(value.headers.entries()),
});
const settledRedis = await this.redis;
await settledRedis.set(this.serializeKey(key), data, {
ex: Math.floor(control.maxAge / 1000),
ex: Math.floor(expires / 1000),
});
}

Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export interface HttpZResponseModel {
export type ResponseProps = {
url: string;
body: string;
ctime?: number;
headers: Headers;
status: number;
statusText: string;
Expand Down
8 changes: 2 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,15 @@ export const isForbidden = ({ status }: { status: number }): boolean => {

export const calcExpires = (
headers: Headers,
): { maxAge: number; expires: string } => {
): number => {
const DEFAULT = '600';
const cacheControl = Object.fromEntries(
(headers.get('cache-control') ?? '').split(/\s*,\s*/g).map((part) =>
part.split('=')
),
);
const effectiveMaxAge = Number(cacheControl['max-age'] || DEFAULT) * 1000;
const expires = String(Date.now() + effectiveMaxAge);
return {
maxAge: effectiveMaxAge,
expires,
};
return effectiveMaxAge;
};

export const buildDebugPerformance = (performance: Performance): string => (
Expand Down

0 comments on commit 981346a

Please # to comment.