const cache = new (require("data-store"))({ path: "persistentDb.json" }); var Cache = {} Cache.SetScores = function(scores) { var dataToSet = {}; for (var i in scores) if (scores[i].score) dataToSet[scores[i].name] = scores[i].score; cache.set("scores", dataToSet); cache.set("saveTs", Date.now()); } Cache.getReportedQuestions = function(questionId, username) { return cache.get("report") || {}; } Cache.isReportedBy = function(questionId, username) { var reported = cache.get("report") || {}; var questionReported = reported[questionId] || {}; reported[questionId] = questionReported; return !!(questionReported[username]); } Cache.reportQuestion = function(questionId, username) { var reported = cache.get("report") || {}; var questionReported = reported[questionId] || {}; reported[questionId] = questionReported; if (questionReported[username]) return; questionReported[username] = Date.now(); cache.set("report", reported); } Cache.clearReports = function() { cache.set("report", {}); } Cache.unreportQuestion = function(questionId, username) { var reported = cache.get("report") || {}; if (!reported[questionId]) return; if (username) reported[questionId][username] && delete reported[questionId][username]; else delete reported[questionId]; cache.set("report", reported); } Cache.setExportTs = function() { cache.set("mysqlTs", Date.now()); } Cache.getLastMysqlSave = function() { return cache.get("mysqlTs") || 0; } Cache.GetData = function() { return { scores: cache.get("scores"), lastSave: cache.get("saveTs") || 0, lastMysqlExport: cache.get("mysqlTs") || 0 }; } module.exports = Cache;