From 6c2d960fbb86116abef5bbfe754e4c236f6431a8 Mon Sep 17 00:00:00 2001 From: tlyong1992 Date: Mon, 6 Jun 2022 17:23:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5mongodb?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/cache/cache_manager.js | 8 ++++-- lib/cache/mongo_db_cache.js | 49 +++++++++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 lib/cache/mongo_db_cache.js diff --git a/lib/cache/cache_manager.js b/lib/cache/cache_manager.js index 4a667df4..ec921177 100644 --- a/lib/cache/cache_manager.js +++ b/lib/cache/cache_manager.js @@ -1,9 +1,13 @@ import MemoryCache from './memory_cache' import FileCache from './local_file_cache' +import MongoCache from './mongo_db_cache' const enableCache = true +const enableMongoCache = true let api -if (process.env.ENABLE_FILE_CACHE) { +if (enableMongoCache) { + api = MongoCache +} else if (process.env.ENABLE_FILE_CACHE) { api = FileCache } else { api = MemoryCache @@ -22,7 +26,7 @@ export async function getDataFromCache(key) { if (JSON.stringify(dataFromCache) === '[]') { return null } - return dataFromCache + return MongoCache.getCache(key) } export async function setDataToCache(key, data) { diff --git a/lib/cache/mongo_db_cache.js b/lib/cache/mongo_db_cache.js new file mode 100644 index 00000000..b3996470 --- /dev/null +++ b/lib/cache/mongo_db_cache.js @@ -0,0 +1,49 @@ +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} + */ +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 } diff --git a/package.json b/package.json index ab5eb052..22ab8bc5 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "localStorage": "^1.0.4", "lodash.throttle": "^4.1.1", "memory-cache": "^0.2.0", + "mongodb": "^4.6.0", "next": "^12.0.5", "notion-client": "6.12.9", "notion-utils": "6.10.0",