Merge pull request #1816 from RafaelTaranto/chore/database-optimization

LAM-1092 database optimization
This commit is contained in:
Rafael Taranto 2025-04-14 11:19:32 +01:00 committed by GitHub
commit 2dddd24d3f
18 changed files with 141 additions and 307 deletions

View file

@ -1,21 +1,18 @@
const { asyncLocalStorage } = require('../../async-storage')
const db = require('../../db')
const { USER_SESSIONS_TABLE_NAME } = require('../../constants')
const logger = require('../../logger')
const schemaCache = {}
let schemaCache = Date.now()
const cleanUserSessions = (cleanInterval) => (req, res, next) => {
const schema = asyncLocalStorage.getStore() ? asyncLocalStorage.getStore().get('schema') : null
const now = Date.now()
if (!schema) return next()
if (schema && schemaCache.schema + cleanInterval > now) return next()
if (schemaCache + cleanInterval > now) return next()
logger.debug(`Clearing expired sessions for schema ${schema}`)
logger.debug(`Clearing expired sessions for schema 'public'`)
return db.none('DELETE FROM $1^ WHERE expire < to_timestamp($2 / 1000.0)', [USER_SESSIONS_TABLE_NAME, now])
.then(() => {
schemaCache.schema = now
schemaCache = now
return next()
})
.catch(next)