Skip to content

Commit

Permalink
fix: refactor fetching and refetch logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ourongxing committed Nov 4, 2024
1 parent 2aea2aa commit 552b75d
Show file tree
Hide file tree
Showing 18 changed files with 190 additions and 221 deletions.
89 changes: 0 additions & 89 deletions server/api/s/[id].get.ts

This file was deleted.

22 changes: 22 additions & 0 deletions server/api/s/entire.post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { SourceID, SourceResponse } from "@shared/types"
import { getCacheTable } from "#/database/cache"

export default defineEventHandler(async (event) => {
try {
const { sources: _ }: { sources: SourceID[] } = await readBody(event)
const cacheTable = await getCacheTable()
const ids = _?.filter(k => sources[k])
if (ids?.length && cacheTable) {
const caches = await cacheTable.getEntire(ids)
const now = Date.now()
return caches.map(cache => ({
status: "cache",
id: cache.id,
items: cache.items,
updatedTime: now - cache.updated < sources[cache.id].interval ? now : cache.updated,
})) as SourceResponse[]
}
} catch {
//
}
})
14 changes: 0 additions & 14 deletions server/api/s/entries.post.ts

This file was deleted.

15 changes: 8 additions & 7 deletions server/api/s/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@ export default defineEventHandler(async (event): Promise<SourceResponse> => {
}

const cacheTable = await getCacheTable()
// Date.now() in Cloudflare Worker will not update throughout the entire runtime.
const now = Date.now()
let cache: CacheInfo
let cache: CacheInfo | undefined
if (cacheTable) {
cache = await cacheTable.get(id)
if (cache) {
// if (cache) {
// interval 刷新间隔,对于缓存失效也要执行的。本质上表示本来内容更新就很慢,这个间隔内可能内容压根不会更新。
// 默认 10 分钟,是低于 TTL 的,但部分 Source 的更新间隔会超过 TTL,甚至有的一天更新一次。
const interval = sources[id].interval
if (now - cache.updated < interval) {
if (now - cache.updated < sources[id].interval) {
return {
status: "success",
id,
updatedTime: now,
items: cache.data,
items: cache.items,
}
}

Expand All @@ -47,7 +48,7 @@ export default defineEventHandler(async (event): Promise<SourceResponse> => {
status: "cache",
id,
updatedTime: cache.updated,
items: cache.data,
items: cache.items,
}
}
}
Expand All @@ -56,7 +57,7 @@ export default defineEventHandler(async (event): Promise<SourceResponse> => {

try {
const newData = (await getters[id]()).slice(0, 30)
if (cacheTable && newData) {
if (cacheTable && newData.length) {
if (event.context.waitUntil) event.context.waitUntil(cacheTable.set(id, newData))
else await cacheTable.set(id, newData)
}
Expand All @@ -73,7 +74,7 @@ export default defineEventHandler(async (event): Promise<SourceResponse> => {
status: "cache",
id,
updatedTime: cache.updated,
items: cache.data,
items: cache.items,
}
} else {
throw e
Expand Down
41 changes: 19 additions & 22 deletions server/database/cache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import process from "node:process"
import type { NewsItem } from "@shared/types"
import type { Database } from "db0"
import type { CacheInfo } from "../types"
import type { CacheInfo, CacheRow } from "../types"

export class Cache {
private db
Expand All @@ -28,27 +28,22 @@ export class Cache {
logger.success(`set ${key} cache`)
}

async get(key: string): Promise<CacheInfo> {
const row: any = await this.db.prepare(`SELECT id, data, updated FROM cache WHERE id = ?`).get(key)
const r = row
? {
...row,
data: JSON.parse(row.data),
}
: undefined
logger.success(`get ${key} cache`)
return r
async get(key: string): Promise<CacheInfo | undefined > {
const row = (await this.db.prepare(`SELECT id, data, updated FROM cache WHERE id = ?`).get(key)) as CacheRow | undefined
if (row) {
logger.success(`get ${key} cache`)
return {
id: row.id,
updated: row.updated,
items: JSON.parse(row.data),
}
}
}

async getEntries(keys: string[]) {
async getEntire(keys: string[]): Promise<CacheInfo[]> {
const keysStr = keys.map(k => `id = '${k}'`).join(" or ")
const res = await this.db.prepare(`SELECT id, data, updated FROM cache WHERE ${keysStr}`).all() as any

const rows = (res.results ?? res) as {
id: SourceID
data: string
updated: number
}[]
const rows = (res.results ?? res) as CacheRow[]

/**
* https://developers.cloudflare.com/d1/build-with-d1/d1-client-api/#return-object
Expand All @@ -60,12 +55,14 @@ export class Cache {
* }
*/
if (rows?.length) {
logger.success(`get entries cache`)
return Object.fromEntries(rows.map(row => [row.id, {
logger.success(`get entire (...) cache`)
return rows.map(row => ({
id: row.id,
updatedTime: row.updated,
updated: row.updated,
items: JSON.parse(row.data) as NewsItem[],
}]))
}))
} else {
return []
}
}

Expand Down
8 changes: 7 additions & 1 deletion server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ export interface RSSItem {

export interface CacheInfo {
id: SourceID
data: NewsItem[]
items: NewsItem[]
updated: number
}

export interface CacheRow {
id: SourceID
data: string
updated: number
}

Expand Down
2 changes: 2 additions & 0 deletions shared/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ export const originSources = {
},
depth: {
title: "深度头条",
// invalid, not way to get
disable: true,
interval: Time.Common,
},
},
Expand Down
2 changes: 0 additions & 2 deletions shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,3 @@ export interface SourceResponse {
updatedTime: number | string
items: NewsItem[]
}

export type EntriesSourceResponse = Partial<Record<SourceID, SourceResponse>>
Loading

0 comments on commit 552b75d

Please sign in to comment.