Skip to content

Commit

Permalink
feature:
Browse files Browse the repository at this point in the history
缓存优化
  • Loading branch information
tangly1024 committed Dec 16, 2021
1 parent 54026de commit 6dc1794
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 27 deletions.
35 changes: 23 additions & 12 deletions lib/cache/cache_manager.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import BLOG from '@/blog.config'
import { getCacheFromFile, setCacheToFile } from '@/lib/cache/local_file_cache'
import { getCacheFromMemory, setCacheToMemory } from '@/lib/cache/memory_cache'
import BLOG from '@/blog.config'
// 关闭本地缓存
const enableCache = true
const enableCache = true && !BLOG.isProd // 生产环境禁用
const cacheProvider = 'memory' // ['memory','file'] 用内存或data.json做缓存

/**
* 为减少频繁接口请求,notion数据将被缓存
Expand All @@ -14,21 +14,32 @@ export async function getDataFromCache (key) {
return null
}
let dataFromCache
if (BLOG.isProd) {
dataFromCache = await getCacheFromMemory(key)
} else {
dataFromCache = await getCacheFromFile(key)
switch (cacheProvider) {
case 'memory':
dataFromCache = await getCacheFromMemory(key)
break
case 'file':
dataFromCache = await getCacheFromFile(key)
break
default:
break
}

return dataFromCache
}

export async function setDataToCache (key, data) {
if (!enableCache) {
if (!enableCache || !data) {
return
}
if (BLOG.isProd) {
await setCacheToMemory(key, data)
} else {
await setCacheToFile(key, data)
switch (cacheProvider) {
case 'memory':
await setCacheToMemory(key, data)
break
case 'file':
await setCacheToFile(key, data)
break
default:
break
}
}
2 changes: 1 addition & 1 deletion lib/cache/memory_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export async function getCacheFromMemory (key, options) { // url为缓存标识
}

export async function setCacheToMemory (key, data) { // url为缓存标识
await cache.put(key, data, 60 * 1000)
await cache.put(key, data, 5 * 60 * 1000)
}
5 changes: 3 additions & 2 deletions lib/notion/getNotionData.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import { getPostBlocks } from '@/lib/notion/getPostBlocks'
*/
export async function getNotionPageData ({ pageId = BLOG.notionPageId, from }) {
// 尝试从缓存获取
const data = await getDataFromCache('page_record_map_' + pageId)
const cacheKey = 'page_record_map_' + pageId
const data = await getDataFromCache(cacheKey)
if (data) {
console.log('[请求缓存]:', `from:${from}`, `id:${pageId}`)
return data
}
const pageRecordMap = await getPageRecordMapByNotionAPI({ pageId, from })
// 存入缓存
if (pageRecordMap) {
await setDataToCache('page_record_map', pageRecordMap)
await setDataToCache(cacheKey, pageRecordMap)
}
return pageRecordMap
}
Expand Down
5 changes: 3 additions & 2 deletions lib/notion/getPostBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { NotionAPI } from 'notion-client'
import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager'

export async function getPostBlocks (id, from) {
let pageBlock = await getDataFromCache('page_block_' + id)
const cacheKey = 'page_block_' + id
let pageBlock = await getDataFromCache(cacheKey)
if (pageBlock) {
console.log('[请求缓存]:', `from:${from}`, `id:${id}`)
return pageBlock
Expand All @@ -20,7 +21,7 @@ export async function getPostBlocks (id, from) {
}

if (pageBlock) {
await setDataToCache('page_block_' + id, pageBlock)
await setDataToCache(cacheKey, pageBlock)
}
return pageBlock
}
6 changes: 3 additions & 3 deletions pages/article/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ const Slug = ({ post, blockMap, tags, prev, next, allPosts, recommendPosts, cate

export async function getStaticPaths () {
let posts = []
if (BLOG.isProd) {
posts = await getAllPosts({ from: 'slug - paths', includePage: true })
}
// if (BLOG.isProd) {
posts = await getAllPosts({ from: 'slug - paths', includePage: true })
// }
return {
paths: posts.map(row => `${BLOG.path}/article/${row.slug}`),
fallback: true
Expand Down
54 changes: 47 additions & 7 deletions public/sitemap.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<url><loc>https://tangly1024.com</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T03:34:52.290Z</lastmod></url>
<url><loc>https://tangly1024.com/about</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T03:34:52.290Z</lastmod></url>
<url><loc>https://tangly1024.com/archive</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T03:34:52.290Z</lastmod></url>
<url><loc>https://tangly1024.com/category</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T03:34:52.290Z</lastmod></url>
<url><loc>https://tangly1024.com/feed</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T03:34:52.290Z</lastmod></url>
<url><loc>https://tangly1024.com/search</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T03:34:52.290Z</lastmod></url>
<url><loc>https://tangly1024.com/tag</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T03:34:52.290Z</lastmod></url>
<url><loc>https://tangly1024.com</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/search</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/about</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/archive</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/category</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/category/技术分享</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/category/碎片杂文</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/category/投资理财</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/category/读书笔记</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/Notion</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/Blog</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/Vercel</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/NextJS</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/Nobelium</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/Fruition</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/Hexo</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/开发</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/Serverless</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/Database</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/新闻</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/经济</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/工具</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/CVS</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/Mac</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/金钱</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/思考</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/VSCode</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/NodeJS</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/Parallels Desktop</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/虚拟机</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/市场</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/ReactJS</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/文字</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/Python</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/WebStorm</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/营销</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/健康</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/查理·芒格</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/企业</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/投资</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/教育</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/心理</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/indigo</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/品牌</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/广告</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
<url><loc>https://tangly1024.com/tag/区块链</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-16T13:36:37.657Z</lastmod></url>
</urlset>

0 comments on commit 6dc1794

Please sign in to comment.