From 148545cea952b948508d005a95e8aebdc01c0b7a Mon Sep 17 00:00:00 2001 From: anime Date: Mon, 23 Dec 2024 17:39:48 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=8E=9F=E7=94=9FAI=E6=91=98=E8=A6=81?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E6=94=AF=E6=8C=81=E7=BC=93=E5=AD=98):=20?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E7=BC=93=E5=AD=98=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E5=8F=AA=E6=94=AF=E6=8C=81memory=EF=BC=8C=E4=B8=8D=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=96=87=E4=BB=B6=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit dd56dae44f7f555d9004e7d1f872085bded2cb86) --- blog.config.js | 1 + lib/cache/cache_manager.js | 4 ++-- lib/cache/memory_cache.js | 4 ++-- lib/plugins/aiSummary.js | 1 + pages/[prefix]/[slug]/index.js | 37 +++++++++++++++++++++------------- 5 files changed, 29 insertions(+), 18 deletions(-) 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 - ) } }