cache.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const cache = new (require("data-store"))({ path: "persistentDb.json" });
  2. var Cache = {}
  3. Cache.SetScores = function(scores) {
  4. var dataToSet = {};
  5. for (var i in scores)
  6. if (scores[i].score)
  7. dataToSet[scores[i].name] = scores[i].score;
  8. cache.set("scores", dataToSet);
  9. cache.set("saveTs", Date.now());
  10. }
  11. Cache.getReportedQuestions = function(questionId, username) {
  12. return cache.get("report") || {};
  13. }
  14. Cache.isReportedBy = function(questionId, username) {
  15. var reported = cache.get("report") || {};
  16. var questionReported = reported[questionId] || {};
  17. reported[questionId] = questionReported;
  18. return !!(questionReported[username]);
  19. }
  20. Cache.reportQuestion = function(questionId, username) {
  21. var reported = cache.get("report") || {};
  22. var questionReported = reported[questionId] || {};
  23. reported[questionId] = questionReported;
  24. if (questionReported[username])
  25. return;
  26. questionReported[username] = Date.now();
  27. cache.set("report", reported);
  28. }
  29. Cache.clearReports = function() {
  30. cache.set("report", {});
  31. }
  32. Cache.unreportQuestion = function(questionId, username) {
  33. var reported = cache.get("report") || {};
  34. if (!reported[questionId])
  35. return;
  36. if (username)
  37. reported[questionId][username] && delete reported[questionId][username];
  38. else
  39. delete reported[questionId];
  40. cache.set("report", reported);
  41. }
  42. Cache.setExportTs = function() {
  43. cache.set("mysqlTs", Date.now());
  44. }
  45. Cache.getLastMysqlSave = function() {
  46. return cache.get("mysqlTs") || 0;
  47. }
  48. Cache.GetData = function() {
  49. return {
  50. scores: cache.get("scores"),
  51. lastSave: cache.get("saveTs") || 0,
  52. lastMysqlExport: cache.get("mysqlTs") || 0
  53. };
  54. }
  55. module.exports = Cache;