Skip to content

Commit

Permalink
本地开发、开启文件缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed May 31, 2022
1 parent a942a8f commit 5ad1934
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
17 changes: 12 additions & 5 deletions lib/cache/cache_manager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { getCacheFromMemory as getCache, setCacheToMemory as setCache, delCacheFromMemory as delCache } from '@/lib/cache/memory_cache'
// import { getCacheFromFile as getCache, setCacheToFile as setCache, delCacheFromFile as delCache } from './local_file_cache'
import MemoryCache from './memory_cache'
import FileCache from './local_file_cache'
const enableCache = true

let api
if (process.env.ENABLE_FILE_CACHE) {
api = FileCache
} else {
api = MemoryCache
}

/**
* 为减少频繁接口请求,notion数据将被缓存
* @param {*} key
Expand All @@ -11,7 +18,7 @@ export async function getDataFromCache(key) {
if (!enableCache) {
return null
}
const dataFromCache = await getCache(key)
const dataFromCache = await api.getCache(key)
if (JSON.stringify(dataFromCache) === '[]') {
return null
}
Expand All @@ -22,12 +29,12 @@ export async function setDataToCache(key, data) {
if (!enableCache || !data) {
return
}
await setCache(key, data)
await api.setCache(key, data)
}

export async function delCacheData(key) {
if (!enableCache) {
return
}
await delCache(key)
await api.delCache(key)
}
8 changes: 5 additions & 3 deletions lib/cache/local_file_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const cacheInvalidSeconds = 1000000000 * 1000
// 文件名
const jsonFile = path.resolve('./data.json')

export async function getCacheFromFile (key) {
export async function getCache (key) {
const exist = await fs.existsSync(jsonFile)
if (!exist) return null
const data = await fs.readFileSync(jsonFile)
Expand All @@ -33,18 +33,20 @@ export async function getCacheFromFile (key) {
* @param data
* @returns {Promise<null>}
*/
export async function setCacheToFile (key, data) {
export async function setCache (key, data) {
const exist = await fs.existsSync(jsonFile)
const json = exist ? JSON.parse(await fs.readFileSync(jsonFile)) : {}
json[key] = data
json[key + '_expire_time'] = new Date().getTime()
fs.writeFileSync(jsonFile, JSON.stringify(json))
}

export async function delCacheFromFile (key, data) {
export async function delCache (key, data) {
const exist = await fs.existsSync(jsonFile)
const json = exist ? JSON.parse(await fs.readFileSync(jsonFile)) : {}
delete json.key
json[key + '_expire_time'] = new Date().getTime()
fs.writeFileSync(jsonFile, JSON.stringify(json))
}

export default { getCache, setCache, delCache }
8 changes: 5 additions & 3 deletions lib/cache/memory_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import BLOG from 'blog.config'

const cacheTime = BLOG.isProd ? 10 * 60 : 120 * 60 // 120 minutes for dev,10 minutes for prod

export async function getCacheFromMemory(key, options) {
export async function getCache(key, options) {
return await cache.get(key)
}

export async function setCacheToMemory(key, data) {
export async function setCache(key, data) {
await cache.put(key, data, cacheTime * 1000)
}

export async function delCacheFromMemory(key) {
export async function delCache(key) {
await cache.del(key)
}

export default { getCache, setCache, delCache }

0 comments on commit 5ad1934

Please sign in to comment.