Skip to content

Commit

Permalink
feat: more accurate TTL for weather data cache
Browse files Browse the repository at this point in the history
  • Loading branch information
SegaraRai committed Jun 14, 2024
1 parent e772f14 commit c553a0c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/server/cachedFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { encodeBase64URL } from "./base64url";
export async function cachedFetch(
url: string | URL,
kv: KVNamespace,
ttl: number,
ttl: number | ((data: unknown) => number),
fetcher: (url: string | URL) => Promise<Response> = fetch
): Promise<unknown> {
const urlHash = encodeBase64URL(
Expand All @@ -26,8 +26,9 @@ export async function cachedFetch(
}

const networkResult = await response.json();
const ttlValue = typeof ttl === "function" ? ttl(networkResult) : ttl;
await kv.put(cacheKey, JSON.stringify(networkResult), {
expirationTtl: ttl,
expirationTtl: ttlValue,
});

return networkResult;
Expand Down
4 changes: 3 additions & 1 deletion src/server/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const WEATHER_CACHE_TTL = 5 * 60;
export const WEATHER_CACHE_TTL_MIN = 2 * 60;
export const WEATHER_CACHE_TTL_MAX = 14 * 60;

export const REV_GEOCODING_CACHE_TTL = 24 * 60 * 60;

export const BASE_REQUEST_HEADERS = {
Expand Down
14 changes: 12 additions & 2 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import { cachedFetch } from "./cachedFetch";
import {
BASE_REQUEST_HEADERS,
REV_GEOCODING_CACHE_TTL,
WEATHER_CACHE_TTL,
WEATHER_CACHE_TTL_MAX,
WEATHER_CACHE_TTL_MIN,
} from "./config";
import { derivePublicKey, importPrivateKey } from "./cryptoKey";
import { encryptLocation, parseLocation } from "./location";
import { locationParamSchema } from "./schemas";
import { clamp, getNextWeatherDataUpdateTimestamp } from "./utils";
import { renderWeatherWidget } from "./widgetRenderer";

const NO_CACHE = "private, max-age=0, no-cache, no-store";
Expand Down Expand Up @@ -170,7 +172,15 @@ app.get(
const weatherData = (await cachedFetch(
forecastURL,
c.env.KV_FETCH_CACHE,
WEATHER_CACHE_TTL,
(data) =>
clamp(
Math.round(
(getNextWeatherDataUpdateTimestamp(data as Weather) - Date.now()) /
1000
),
WEATHER_CACHE_TTL_MIN,
WEATHER_CACHE_TTL_MAX
),
proxiedFetcher
).catch(() =>
Promise.reject(
Expand Down
15 changes: 15 additions & 0 deletions src/server/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Weather } from "../types/weather";

export function clamp(value: number, min: number, max: number): number {
return Math.min(Math.max(value, min), max);
}

export function getNextWeatherDataUpdateTimestamp(weather: Weather): number {
const localOffsetDate = new Date(`${weather.current.time}:00Z`);
const localOffsetTimestamp = localOffsetDate.getTime();
return (
localOffsetTimestamp -
weather.utc_offset_seconds * 1000 +
weather.current.interval * 1000
);
}

0 comments on commit c553a0c

Please sign in to comment.