This commit is contained in:
tangly1024.com
2024-02-04 18:28:03 +08:00
parent a234a10abf
commit 1e984d1108
4 changed files with 8 additions and 13 deletions

View File

@@ -18,10 +18,7 @@ export async function getPostBlocks(id, from, slice) {
return filterPostBlocks(id, pageBlock, slice)
}
const start = new Date().getTime()
pageBlock = await getPageWithRetry(id, from)
const end = new Date().getTime()
console.log('[API耗时]', `${end - start}ms`)
if (pageBlock) {
await setDataToCache(cacheKey, pageBlock)
@@ -38,10 +35,7 @@ export async function getSingleBlock(id, from) {
return pageBlock
}
const start = new Date().getTime()
pageBlock = await getPageWithRetry(id, from)
const end = new Date().getTime()
console.log('[API耗时]', `${end - start}ms`)
if (pageBlock) {
await setDataToCache(cacheKey, pageBlock)
@@ -56,16 +50,17 @@ export async function getSingleBlock(id, from) {
*/
export async function getPageWithRetry(id, from, retryAttempts = 3) {
if (retryAttempts && retryAttempts > 0) {
console.log('[请求API]', `from:${from}`, `id:${id}`, retryAttempts < 3 ? `剩余重试次数:${retryAttempts}` : '')
console.log('[API-->>请求]', `from:${from}`, `id:${id}`, retryAttempts < 3 ? `剩余重试次数:${retryAttempts}` : '')
try {
const authToken = BLOG.NOTION_ACCESS_TOKEN || null
const api = new NotionAPI({ authToken, userTimeZone: Intl.DateTimeFormat().resolvedOptions().timeZone })
const start = new Date().getTime()
const pageData = await api.getPage(id)
// console.log('stringfy', JSON.stringify(pageData))
console.info('[响应成功]:', `from:${from}`)
const end = new Date().getTime()
console.log('[API<<--响应]', `耗时:${end - start}ms - from:${from}`)
return pageData
} catch (e) {
console.warn('[响应异常]:', e)
console.warn('[API<<--异常]:', e)
await delay(1000)
const cacheKey = 'page_block_' + id
const pageBlock = await getDataFromCache(cacheKey)