feat(原生AI摘要功能支持缓存): 自定义缓存功能只支持memory,不支持文件缓存

(cherry picked from commit dd56dae44f7f555d9004e7d1f872085bded2cb86)
This commit is contained in:
anime
2024-12-23 17:39:48 +08:00
parent 46527958d0
commit 148545cea9
5 changed files with 29 additions and 18 deletions

View File

@@ -277,6 +277,7 @@ const BLOG = {
AI_SUMMARY_KEY: AI_SUMMARY_KEY:
process.env.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, AI_SUMMARY_WORD_LIMIT: process.env.AI_SUMMARY_WORD_LIMIT || 1000,

View File

@@ -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) { if (!data) {
return return
} }
// console.trace('[API-->>缓存写入]:', key) // console.trace('[API-->>缓存写入]:', key)
await getApi().setCache(key, data) await getApi().setCache(key, data, customCacheTime)
} }
export async function delCacheData(key) { export async function delCacheData(key) {

View File

@@ -7,8 +7,8 @@ export async function getCache(key, options) {
return await cache.get(key) return await cache.get(key)
} }
export async function setCache(key, data) { export async function setCache(key, data, customCacheTime) {
await cache.put(key, data, cacheTime * 1000) await cache.put(key, data, (customCacheTime || cacheTime) * 1000)
} }
export async function delCache(key) { export async function delCache(key) {

View File

@@ -7,6 +7,7 @@
*/ */
export async function getAiSummary(aiSummaryAPI, aiSummaryKey, truncatedText) { export async function getAiSummary(aiSummaryAPI, aiSummaryKey, truncatedText) {
try { try {
console.log('请求文章摘要', truncatedText.slice(0, 100))
const response = await fetch(aiSummaryAPI, { const response = await fetch(aiSummaryAPI, {
method: 'POST', method: 'POST',
headers: { headers: {

View File

@@ -8,6 +8,7 @@ import { idToUuid } from 'notion-utils'
import Slug from '..' import Slug from '..'
import { getPageContentText } from '@/pages/search/[keyword]' import { getPageContentText } from '@/pages/search/[keyword]'
import { getAiSummary } from '@/lib/plugins/aiSummary' import { getAiSummary } from '@/lib/plugins/aiSummary'
import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager'
/** /**
* 根据notion的slug访问页面 * 根据notion的slug访问页面
@@ -99,22 +100,30 @@ export async function getStaticProps({ params: { prefix, slug }, locale }) {
const aiSummaryAPI = siteConfig('AI_SUMMARY_API') const aiSummaryAPI = siteConfig('AI_SUMMARY_API')
if (aiSummaryAPI) { if (aiSummaryAPI) {
const aiSummaryKey = siteConfig('AI_SUMMARY_KEY')
const wordLimit = siteConfig('AI_SUMMARY_WORD_LIMIT', '1000')
const post = props.post const post = props.post
let content = '' const cacheKey = `ai_summary_${post.id}`
for (let heading of post.toc) { let aiSummary = await getDataFromCache(cacheKey)
content += heading.text + ' ' 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
)
} }
} }