mirror of
https://github.com/NekoMonci12/RakunNakun-AI.git
synced 2025-12-20 07:19:17 +00:00
Read-Only Mode MongoDB
This commit is contained in:
@@ -12,6 +12,7 @@ class MongoCacheManager {
|
|||||||
this.collectionName = collectionName;
|
this.collectionName = collectionName;
|
||||||
this.client = new MongoClient(mongoUrl, { useUnifiedTopology: true });
|
this.client = new MongoClient(mongoUrl, { useUnifiedTopology: true });
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
|
this.readOnly = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async connect() {
|
async connect() {
|
||||||
@@ -21,6 +22,17 @@ class MongoCacheManager {
|
|||||||
this.collection = this.client.db(this.dbName).collection(this.collectionName);
|
this.collection = this.client.db(this.dbName).collection(this.collectionName);
|
||||||
this.connected = true;
|
this.connected = true;
|
||||||
console.log("[MongoCache] Connected to MongoDB for caching.");
|
console.log("[MongoCache] Connected to MongoDB for caching.");
|
||||||
|
|
||||||
|
// Try a dry-run write to detect read-only access
|
||||||
|
try {
|
||||||
|
await this.collection.insertOne({ _test: true });
|
||||||
|
await this.collection.deleteOne({ _test: true });
|
||||||
|
} catch (e) {
|
||||||
|
if (e.code === 13 || e.message.includes("not authorized")) {
|
||||||
|
console.warn("[MongoCache] MongoDB user is read-only. Caching will be read-only.");
|
||||||
|
this.readOnly = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("[MongoCache] MongoDB connection error:", error);
|
console.error("[MongoCache] MongoDB connection error:", error);
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
@@ -54,8 +66,8 @@ class MongoCacheManager {
|
|||||||
async setCache(input, value) {
|
async setCache(input, value) {
|
||||||
try {
|
try {
|
||||||
await this.connect();
|
await this.connect();
|
||||||
if (!this.connected || !this.collection) {
|
if (!this.connected || !this.collection || this.readOnly) {
|
||||||
console.error("[MongoCache] Not connected to MongoDB, skipping setCache.");
|
console.warn("[MongoCache] Skipping cache store due to read-only mode or no connection.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const key = this.normalize(input);
|
const key = this.normalize(input);
|
||||||
|
|||||||
Reference in New Issue
Block a user