kopia lustrzana https://github.com/cloudflare/wildebeest
37 wiersze
914 B
TypeScript
37 wiersze
914 B
TypeScript
const CACHE_DO_NAME = 'cachev1'
|
|
|
|
export interface Cache {
|
|
get<T>(key: string): Promise<T | null>
|
|
put<T>(key: string, value: T): Promise<void>
|
|
}
|
|
|
|
export function cacheFromEnv(env: any): Cache {
|
|
return {
|
|
async get<T>(key: string): Promise<T | null> {
|
|
const id = env.DO_CACHE.idFromName(CACHE_DO_NAME)
|
|
const stub = env.DO_CACHE.get(id)
|
|
|
|
const res = await stub.fetch('http://cache/' + key)
|
|
if (!res.ok) {
|
|
throw new Error(`DO cache returned ${res.status}: ${await res.text()}`)
|
|
}
|
|
|
|
return (await res.json()) as T
|
|
},
|
|
|
|
async put<T>(key: string, value: T): Promise<void> {
|
|
const id = env.DO_CACHE.idFromName(CACHE_DO_NAME)
|
|
const stub = env.DO_CACHE.get(id)
|
|
|
|
const res = await stub.fetch('http://cache/', {
|
|
method: 'PUT',
|
|
body: JSON.stringify({ key, value }),
|
|
})
|
|
|
|
if (!res.ok) {
|
|
throw new Error(`DO cache returned ${res.status}: ${await res.text()}`)
|
|
}
|
|
},
|
|
}
|
|
}
|