mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-04 23:16:53 +00:00
移除mongodb
This commit is contained in:
9
lib/cache/cache_manager.js
vendored
9
lib/cache/cache_manager.js
vendored
@@ -1,7 +1,6 @@
|
|||||||
import MemoryCache from './memory_cache'
|
|
||||||
import FileCache from './local_file_cache'
|
|
||||||
import MongoCache from './mongo_db_cache'
|
|
||||||
import BLOG from '@/blog.config'
|
import BLOG from '@/blog.config'
|
||||||
|
import FileCache from './local_file_cache'
|
||||||
|
import MemoryCache from './memory_cache'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 为减少频繁接口请求,notion数据将被缓存
|
* 为减少频繁接口请求,notion数据将被缓存
|
||||||
@@ -39,9 +38,7 @@ export async function delCacheData(key) {
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
function getApi() {
|
function getApi() {
|
||||||
if (process.env.MONGO_DB_URL && process.env.MONGO_DB_NAME) {
|
if (process.env.ENABLE_FILE_CACHE) {
|
||||||
return MongoCache
|
|
||||||
} else if (process.env.ENABLE_FILE_CACHE) {
|
|
||||||
return FileCache
|
return FileCache
|
||||||
} else {
|
} else {
|
||||||
return MemoryCache
|
return MemoryCache
|
||||||
|
|||||||
49
lib/cache/mongo_db_cache.js
vendored
49
lib/cache/mongo_db_cache.js
vendored
@@ -1,49 +0,0 @@
|
|||||||
const MongoClient = require('mongodb').MongoClient
|
|
||||||
|
|
||||||
const DB_URL = process.env.MONGO_DB_URL // e.g. mongodb+srv://mongo_user:[password]@xxx.mongodb.net//?retryWrites=true&w=majority
|
|
||||||
const DB_NAME = process.env.MONGO_DB_NAME // e.g. tangly1024
|
|
||||||
const DB_COLLECTION = 'posts'
|
|
||||||
|
|
||||||
export async function getCache (key) {
|
|
||||||
const client = await MongoClient.connect(DB_URL).catch(err => { console.error(err) })
|
|
||||||
const dbo = client.db(DB_NAME)
|
|
||||||
const query = { block_id: key }
|
|
||||||
const res = await dbo.collection('posts').findOne(query).catch(err => { console.error(err) })
|
|
||||||
await client.close()
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 并发请求写文件异常; Vercel生产环境不支持写文件。
|
|
||||||
* @param key
|
|
||||||
* @param data
|
|
||||||
* @returns {Promise<null>}
|
|
||||||
*/
|
|
||||||
export async function setCache (key, data) {
|
|
||||||
const client = await MongoClient.connect(DB_URL).catch(err => { console.error(err) })
|
|
||||||
const dbo = client.db(DB_NAME)
|
|
||||||
data.block_id = key
|
|
||||||
const query = { block_id: key }
|
|
||||||
const jsonObj = JSON.parse(JSON.stringify(data))
|
|
||||||
|
|
||||||
const updRes = await dbo.collection(DB_COLLECTION).updateOne(query, { $set: jsonObj }).catch(err => { console.error(err) })
|
|
||||||
console.log('更新结果', key, updRes)
|
|
||||||
if (updRes.matchedCount === 0) {
|
|
||||||
const insertRes = await dbo.collection(DB_COLLECTION).insertOne(jsonObj).catch(err => { console.error(err) })
|
|
||||||
console.log('插入结果', key, insertRes)
|
|
||||||
}
|
|
||||||
await client.close()
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function delCache (key, data) {
|
|
||||||
const client = await MongoClient.connect(DB_URL).catch(err => { console.error(err) })
|
|
||||||
const dbo = client.db(DB_NAME)
|
|
||||||
const query = { block_id: key }
|
|
||||||
const res = await dbo.collection('posts').deleteOne(query).catch(err => { console.error(err) })
|
|
||||||
console.log('删除结果', key, res)
|
|
||||||
await client.close()
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
export default { getCache, setCache, delCache }
|
|
||||||
@@ -33,7 +33,6 @@
|
|||||||
"localStorage": "^1.0.4",
|
"localStorage": "^1.0.4",
|
||||||
"lodash.throttle": "^4.1.1",
|
"lodash.throttle": "^4.1.1",
|
||||||
"memory-cache": "^0.2.0",
|
"memory-cache": "^0.2.0",
|
||||||
"mongodb": "^4.6.0",
|
|
||||||
"next": "13.3.1",
|
"next": "13.3.1",
|
||||||
"notion-client": "6.15.6",
|
"notion-client": "6.15.6",
|
||||||
"notion-utils": "6.15.6",
|
"notion-utils": "6.15.6",
|
||||||
|
|||||||
Reference in New Issue
Block a user