Pārlūkot izejas kodu

增加退出时的缓存

flyzto 5 mēneši atpakaļ
vecāks
revīzija
b4a703b3f4

+ 44 - 0
server/libs/cache.js

@@ -0,0 +1,44 @@
+const fs = require('fs');
+const path = require('path');
+
+function getData(file, isMap) {
+  let data;
+  try {
+    data = fs.readFileSync(file);
+  }
+  catch (e) {
+    data = isMap ? '{}' : '[]';
+  }
+  try {
+    data = JSON.parse(data.toString());
+  }
+  catch (e) {
+    data = (isMap ? {} : []);
+  }
+  return data;
+}
+
+function setData(file, data, callback) {
+  if (typeof (data) != 'string') {
+    try {
+      data = JSON.stringify(data)
+    }
+    catch (error) {
+      callback?.(error);
+    }
+  }
+  const directoryPath = path.dirname(file);
+  if(!fs.existsSync(directoryPath)) {
+    fs.mkdirSync(directoryPath, { recursive: true });
+  }
+
+  try {
+    fs.writeFileSync(file, data);
+    callback?.();
+  }
+  catch (error) {
+    callback?.(error);
+  }
+}
+
+module.exports = { getData, setData };

+ 44 - 0
server/models/GamesPs.js

@@ -1,8 +1,13 @@
 const axios = require('axios');
 const Logs = require('../libs/logs');
+const Cache = require('../libs/cache');
 const Setting = require('./Setting');
 const { eventSolutions } = require('../triangle/eventSolutions');
 const { calcTotalProfit, calcTotalProfitWithFixedFirst } = require('../triangle/totalProfitCalc');
+const fs = require('fs');
+const path = require('path');
+
+const GamesCacheFile = path.join(__dirname, '../data/games.cache');
 
 const childOptions = process.env.NODE_ENV == 'development' ? {
   execArgv: ['--inspect=9228'],
@@ -759,6 +764,45 @@ Setting.onUpdate(fields => {
   postDataToChild('updateSetting', fields);
 });
 
+/**
+ * 保存GAMES数据到缓存文件
+ */
+const saveGamesToCache = () => {
+  Cache.setData(GamesCacheFile, GAMES, err => {
+    if (err) {
+      Logs.out('Failed to save games cache:', err.message);
+    }
+    else {
+      Logs.out('Games cache saved successfully');
+    }
+  });
+}
+
+/**
+ * 从缓存文件加载GAMES数据
+ */
+const loadGamesFromCache = () => {
+  const gamesCacheData = Cache.getData(GamesCacheFile, true);
+  Object.assign(GAMES, gamesCacheData);
+  Logs.out('Games cache loaded successfully');
+}
+
+// 在模块加载时尝试从缓存恢复数据
+loadGamesFromCache();
+
+// 监听进程退出事件,保存GAMES数据
+process.on('exit', saveGamesToCache);
+process.on('SIGINT', () => {
+  process.exit(0);
+});
+process.on('SIGTERM', () => {
+  process.exit(0);
+});
+process.on('SIGUSR2', () => {
+  process.exit(0);
+});
+
+
 module.exports = {
   updateLeaguesList, getFilteredLeagues,
   updateGamesList, updateGamesEvents,

+ 2 - 0
web/apps/web-antd/src/views/match/solutions/index.vue

@@ -326,6 +326,8 @@ const updateSolutions = async () => {
 const showTotalProfit = async () => {
   totalProfit.value = await calcTotalProfit();
   totalProfitVisible.value = true;
+  const { profit } = totalProfit.value;
+  console.log('profit', profit);
 };
 
 const closeTotalProfit = () => {