REDis
This commit is contained in:
+18
-2
@@ -15,6 +15,7 @@ import { createSessionManager } from './src/security/session.mjs';
|
||||
import { readBodyBuffer, readJson, sendError, sendJson, sendWorkbook } from './src/http/responses.mjs';
|
||||
import { createBaseDatabase } from './src/data/base.mjs';
|
||||
import { resolveRegion } from './src/data/region-service.mjs';
|
||||
import { createRedisCache, withCacheInvalidation } from './src/cache/redis-cache.mjs';
|
||||
|
||||
const root = resolve(process.cwd());
|
||||
const envPath = join(root, '.env');
|
||||
@@ -93,7 +94,19 @@ const initializeDatabase = () => createBaseDatabase({
|
||||
displayName: process.env.INITIAL_ADMIN_DISPLAY_NAME
|
||||
}
|
||||
});
|
||||
const database = await createDatabase({ root, seed: initializeDatabase });
|
||||
const persistentDatabase = await createDatabase({ root, seed: initializeDatabase });
|
||||
const cache = await createRedisCache();
|
||||
const resultCacheWriteMethods = new Set(['saveResult', 'saveResults', 'updateExam', 'archiveExam']);
|
||||
const database = withCacheInvalidation(persistentDatabase, cache, (method, args) => {
|
||||
const namespaces = ['public'];
|
||||
const instance = args[0];
|
||||
if (resultCacheWriteMethods.has(method)
|
||||
|| (['createWorkflow', 'processWorkflow', 'transferWorkflow'].includes(method) && instance?.businessType === 'score_appeal')
|
||||
|| (method === 'saveWorkflow' && instance?.businessType === 'score_appeal')) {
|
||||
namespaces.push('results');
|
||||
}
|
||||
return namespaces;
|
||||
});
|
||||
const readDb = () => database.read();
|
||||
|
||||
const { parseCookies, currentUser, safeUser, requireUser } = createSessionManager({ sessions, readDb, sendError });
|
||||
@@ -825,6 +838,8 @@ function admitCardsHtml(db, registrations, title) {
|
||||
|
||||
const routeContext = {
|
||||
database,
|
||||
cache,
|
||||
resultsCacheTtlSeconds: process.env.REDIS_RESULTS_CACHE_TTL_SECONDS || 86400,
|
||||
readDb,
|
||||
publicSiteConfig,
|
||||
sendJson,
|
||||
@@ -927,12 +942,13 @@ const server = createServer(async (request, response) => {
|
||||
server.listen(port, host, () => {
|
||||
console.log(`衡准考试信息管理系统:http://${host}:${port}`);
|
||||
console.log(`数据库:${database.client}(${database.location})`);
|
||||
console.log(`Redis 缓存:${cache.status === 'ready' ? '已连接' : cache.status === 'disabled' ? '未配置' : '不可用,已回源数据库'}`);
|
||||
});
|
||||
|
||||
async function shutdown(signal) {
|
||||
console.log(`收到 ${signal},正在关闭服务...`);
|
||||
server.close(async () => {
|
||||
await database.close();
|
||||
await Promise.allSettled([database.close(), cache.close()]);
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user