Publish the latest Android version code (#2085)
* feat(website): publish latest Android version code * fix(website): select latest Android APK release
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import { env, waitUntil } from "cloudflare:workers";
|
||||
|
||||
export const GITHUB_CACHE_TTL_MS = 5 * 60 * 1000;
|
||||
|
||||
export interface WebsiteCacheContext {
|
||||
cache: KVNamespace | null;
|
||||
waitUntil: (promise: Promise<unknown>) => void;
|
||||
}
|
||||
|
||||
interface CachedValue<T> {
|
||||
fetchedAt: number;
|
||||
value: T;
|
||||
@@ -9,10 +12,6 @@ interface CachedValue<T> {
|
||||
|
||||
type Validator<T> = (value: unknown) => value is T;
|
||||
|
||||
function getWebsiteCache(): KVNamespace | null {
|
||||
return (env as { WEBSITE_CACHE?: KVNamespace }).WEBSITE_CACHE ?? null;
|
||||
}
|
||||
|
||||
function isCachedValue<T>(value: unknown, isValue: Validator<T>): value is CachedValue<T> {
|
||||
if (typeof value !== "object" || value === null) return false;
|
||||
const record = value as Record<string, unknown>;
|
||||
@@ -20,17 +19,20 @@ function isCachedValue<T>(value: unknown, isValue: Validator<T>): value is Cache
|
||||
}
|
||||
|
||||
async function readCachedValue<T>(
|
||||
cache: KVNamespace | null,
|
||||
key: string,
|
||||
isValue: Validator<T>,
|
||||
): Promise<CachedValue<T> | null> {
|
||||
const cache = getWebsiteCache();
|
||||
if (!cache) return null;
|
||||
const cached = await cache.get(key, { cacheTtl: 60, type: "json" });
|
||||
return isCachedValue(cached, isValue) ? cached : null;
|
||||
}
|
||||
|
||||
async function writeCachedValue<T>(key: string, value: T): Promise<void> {
|
||||
const cache = getWebsiteCache();
|
||||
async function writeCachedValue<T>(
|
||||
cache: KVNamespace | null,
|
||||
key: string,
|
||||
value: T,
|
||||
): Promise<void> {
|
||||
if (!cache) return;
|
||||
await cache.put(
|
||||
key,
|
||||
@@ -42,20 +44,22 @@ async function writeCachedValue<T>(key: string, value: T): Promise<void> {
|
||||
}
|
||||
|
||||
export async function getBlockingColdCache<T>({
|
||||
context,
|
||||
key,
|
||||
isValue,
|
||||
fetchFresh,
|
||||
}: {
|
||||
context: WebsiteCacheContext;
|
||||
key: string;
|
||||
isValue: Validator<T>;
|
||||
fetchFresh: () => Promise<T>;
|
||||
}): Promise<T> {
|
||||
const cached = await readCachedValue(key, isValue);
|
||||
const cached = await readCachedValue(context.cache, key, isValue);
|
||||
if (cached) {
|
||||
if (Date.now() - cached.fetchedAt > GITHUB_CACHE_TTL_MS) {
|
||||
waitUntil(
|
||||
context.waitUntil(
|
||||
fetchFresh()
|
||||
.then((fresh) => writeCachedValue(key, fresh))
|
||||
.then((fresh) => writeCachedValue(context.cache, key, fresh))
|
||||
.catch(() => undefined),
|
||||
);
|
||||
}
|
||||
@@ -63,6 +67,6 @@ export async function getBlockingColdCache<T>({
|
||||
}
|
||||
|
||||
const fresh = await fetchFresh();
|
||||
await writeCachedValue(key, fresh);
|
||||
await writeCachedValue(context.cache, key, fresh);
|
||||
return fresh;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user