diff --git a/blog.config.js b/blog.config.js index 161f7715..89567a8f 100644 --- a/blog.config.js +++ b/blog.config.js @@ -277,6 +277,7 @@ const BLOG = { AI_SUMMARY_KEY: process.env.AI_SUMMARY_KEY || '', + AI_SUMMARY_CACHE_TIME: process.env.AI_SUMMARY_CACHE_TIME || 1800, // 缓存时间,单位秒 AI_SUMMARY_WORD_LIMIT: process.env.AI_SUMMARY_WORD_LIMIT || 1000, diff --git a/lib/cache/cache_manager.js b/lib/cache/cache_manager.js index 1d665ca1..f35a1fe1 100644 --- a/lib/cache/cache_manager.js +++ b/lib/cache/cache_manager.js @@ -20,12 +20,12 @@ export async function getDataFromCache(key, force) { } } -export async function setDataToCache(key, data) { +export async function setDataToCache(key, data, customCacheTime) { if (!data) { return } // console.trace('[API-->>缓存写入]:', key) - await getApi().setCache(key, data) + await getApi().setCache(key, data, customCacheTime) } export async function delCacheData(key) { diff --git a/lib/cache/memory_cache.js b/lib/cache/memory_cache.js index 1a7012f9..001666c3 100644 --- a/lib/cache/memory_cache.js +++ b/lib/cache/memory_cache.js @@ -7,8 +7,8 @@ export async function getCache(key, options) { return await cache.get(key) } -export async function setCache(key, data) { - await cache.put(key, data, cacheTime * 1000) +export async function setCache(key, data, customCacheTime) { + await cache.put(key, data, (customCacheTime || cacheTime) * 1000) } export async function delCache(key) { diff --git a/lib/plugins/aiSummary.js b/lib/plugins/aiSummary.js index 5b9c1d4e..336b9954 100644 --- a/lib/plugins/aiSummary.js +++ b/lib/plugins/aiSummary.js @@ -7,6 +7,7 @@ */ export async function getAiSummary(aiSummaryAPI, aiSummaryKey, truncatedText) { try { + console.log('请求文章摘要', truncatedText.slice(0, 100)) const response = await fetch(aiSummaryAPI, { method: 'POST', headers: { diff --git a/pages/[prefix]/[slug]/index.js b/pages/[prefix]/[slug]/index.js index 6d30e227..fa9ced8e 100644 --- a/pages/[prefix]/[slug]/index.js +++ b/pages/[prefix]/[slug]/index.js @@ -8,6 +8,7 @@ import { idToUuid } from 'notion-utils' import Slug from '..' import { getPageContentText } from '@/pages/search/[keyword]' import { getAiSummary } from '@/lib/plugins/aiSummary' +import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager' /** * 根据notion的slug访问页面 @@ -99,22 +100,30 @@ export async function getStaticProps({ params: { prefix, slug }, locale }) { const aiSummaryAPI = siteConfig('AI_SUMMARY_API') if (aiSummaryAPI) { - const aiSummaryKey = siteConfig('AI_SUMMARY_KEY') - const wordLimit = siteConfig('AI_SUMMARY_WORD_LIMIT', '1000') const post = props.post - let content = '' - for (let heading of post.toc) { - content += heading.text + ' ' + const cacheKey = `ai_summary_${post.id}` + let aiSummary = await getDataFromCache(cacheKey) + if (aiSummary) { + props.post.aiSummary = aiSummary + } else { + const aiSummaryKey = siteConfig('AI_SUMMARY_KEY') + const aiSummaryCacheTime = siteConfig('AI_SUMMARY_CACHE_TIME') + const wordLimit = siteConfig('AI_SUMMARY_WORD_LIMIT', '1000') + let content = '' + for (let heading of post.toc) { + content += heading.text + ' ' + } + content += getPageContentText(post, post.blockMap) + const combinedText = post.title + ' ' + content + const truncatedText = combinedText.slice(0, wordLimit) + aiSummary = await getAiSummary( + aiSummaryAPI, + aiSummaryKey, + truncatedText + ) + await setDataToCache(cacheKey, aiSummary, aiSummaryCacheTime) + props.post.aiSummary = aiSummary } - content += getPageContentText(post, post.blockMap) - const combinedText = post.title + ' ' + content - const truncatedText = combinedText.slice(0, wordLimit) - - props.post.aiSummary = await getAiSummary( - aiSummaryAPI, - aiSummaryKey, - truncatedText - ) } }