-
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(api): use cron triggers to update kv metadata (#935)
* perf(api): simplify kv structure * feat: add all download stats endpoint * perf: add cron trigger to update metadata * Create twenty-parents-marry.md * test: update snapshots
- Loading branch information
Showing
22 changed files
with
393 additions
and
553 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"api": patch | ||
--- | ||
|
||
perf(api): use cron triggers to update kv metadata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,38 @@ | ||
import type { FontsourceMetadata, TTLMetadata } from '../types'; | ||
import type { Fontlist, FontlistQueries } from './types'; | ||
import { KV_TTL, METADATA_KEYS } from '../utils'; | ||
import type { Fontlist, FontlistQueries, MetadataList } from './types'; | ||
import { updateList, updateMetadata } from './update'; | ||
|
||
const getOrUpdateMetadata = async ( | ||
const getMetadata = async ( | ||
env: Env, | ||
ctx: ExecutionContext, | ||
): Promise<FontsourceMetadata> => { | ||
const { value, metadata } = await env.FONTLIST.getWithMetadata< | ||
FontsourceMetadata, | ||
TTLMetadata | ||
>('metadata', { | ||
): Promise<MetadataList> => { | ||
const value = await env.METADATA.get<MetadataList>(METADATA_KEYS.fonts, { | ||
type: 'json', | ||
cacheTtl: KV_TTL, | ||
}); | ||
|
||
if (!value) { | ||
return await updateMetadata(env); | ||
} | ||
|
||
// If the ttl is not set or the cache expiry is less than the current time, then return old value | ||
// while revalidating the cache | ||
if (!metadata?.ttl || metadata.ttl < Date.now() / 1000) { | ||
ctx.waitUntil(updateMetadata(env)); | ||
return await updateMetadata(env, ctx); | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
const getOrUpdateList = async ( | ||
const getList = async ( | ||
key: FontlistQueries, | ||
env: Env, | ||
ctx: ExecutionContext, | ||
): Promise<Fontlist> => { | ||
const { value, metadata } = await env.FONTLIST.getWithMetadata< | ||
Fontlist, | ||
TTLMetadata | ||
>(key, { | ||
const value = await env.METADATA.get<Fontlist>(METADATA_KEYS.fontlist(key), { | ||
type: 'json', | ||
cacheTtl: KV_TTL, | ||
}); | ||
|
||
if (!value) { | ||
return await updateList(key, env); | ||
} | ||
|
||
if (!metadata?.ttl || metadata.ttl < Date.now() / 1000) { | ||
ctx.waitUntil(updateList(key, env)); | ||
return await updateList(key, env, ctx); | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
export { getOrUpdateList, getOrUpdateMetadata }; | ||
export { getList, getMetadata }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,28 @@ | ||
import { type TTLMetadata } from '../types'; | ||
import type { ArrayMetadata, IDResponse } from './types'; | ||
import { updateArrayMetadata, updateId } from './update'; | ||
import { getMetadata } from '../fontlist/get'; | ||
import { KV_TTL, METADATA_KEYS } from '../utils'; | ||
import type { ArrayMetadata } from './types'; | ||
import { updateArrayMetadata } from './update'; | ||
|
||
const getOrUpdateArrayMetadata = async (env: Env, ctx: ExecutionContext) => { | ||
const { value, metadata } = await env.FONTLIST.getWithMetadata< | ||
ArrayMetadata, | ||
TTLMetadata | ||
>('metadata_arr', { | ||
const getArrayMetadata = async (env: Env, ctx: ExecutionContext) => { | ||
const value = await env.METADATA.get<ArrayMetadata>(METADATA_KEYS.fonts_arr, { | ||
type: 'json', | ||
cacheTtl: KV_TTL, | ||
}); | ||
|
||
if (!value) { | ||
return await updateArrayMetadata(env, ctx); | ||
} | ||
|
||
// If the ttl is not set or the cache expiry is less than the current time, then return old value | ||
// while revalidating the cache | ||
if (!metadata?.ttl || metadata.ttl < Date.now() / 1000) { | ||
ctx.waitUntil(updateArrayMetadata(env, ctx)); | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
const getOrUpdateId = async (id: string, env: Env, ctx: ExecutionContext) => { | ||
const { value, metadata } = await env.FONTS.getWithMetadata< | ||
IDResponse, | ||
TTLMetadata | ||
>(id, { type: 'json' }); | ||
|
||
if (!value) { | ||
return await updateId(id, env, ctx); | ||
const getId = async (id: string, env: Env, ctx: ExecutionContext) => { | ||
const data = await getMetadata(env, ctx); | ||
if (!data[id]) { | ||
return; | ||
} | ||
|
||
if (!metadata?.ttl || metadata.ttl < Date.now() / 1000) { | ||
ctx.waitUntil(updateId(id, env, ctx)); | ||
} | ||
|
||
return value; | ||
return data[id]; | ||
}; | ||
|
||
export { getOrUpdateArrayMetadata, getOrUpdateId }; | ||
export { getArrayMetadata, getId }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.