diff --git a/src/app/services/user.js b/src/app/services/user.js index 899d3594..d8209de6 100644 --- a/src/app/services/user.js +++ b/src/app/services/user.js @@ -17,6 +17,7 @@ const MailService = require('./mail'); const UtilsService = require('./utils'); const passport = require('../middleware/passport'); const Logger = require('../../lib/logger').default; +const { default: Redis } = require('../../config/initializers/redis'); const { Op, col, fn } = sequelize; @@ -508,13 +509,24 @@ module.exports = (Model, App) => { const getUsage = async (user) => { const targetUser = await Model.users.findOne({ where: { username: user.bridgeUser } }); - const usage = await Model.file.findAll({ - where: { user_id: targetUser.id, status: { [Op.ne]: 'DELETED' } }, - attributes: [[fn('sum', col('size')), 'total']], - raw: true, - }); + Redis.getInstance(); + const cachedUsage = await Redis.getUsage(user.uuid); + let driveUsage = 0; - const driveUsage = parseInt(usage[0].total); + if (cachedUsage) { + logger.info('Cache hit for user ' + user.uuid); + driveUsage = cachedUsage; + } else { + const usage = await Model.file.findAll({ + where: { user_id: targetUser.id, status: { [Op.ne]: 'DELETED' } }, + attributes: [[fn('sum', col('size')), 'total']], + raw: true, + }); + + driveUsage = parseInt(usage[0].total); + + await Redis.setUsage(user.uuid, driveUsage); + } const backupsQuery = await Model.backup.findAll({ where: { userId: targetUser.id }, diff --git a/src/config/initializers/redis.ts b/src/config/initializers/redis.ts index 9c270017..0346b848 100644 --- a/src/config/initializers/redis.ts +++ b/src/config/initializers/redis.ts @@ -38,6 +38,24 @@ export default class Redis { return Redis.instance; } + static async getUsage(userUuid: string): Promise { + const r = Redis.instance; + + const v = await r.get(`${userUuid}-usage`); + + if (!v) { + return null; + } + + return (JSON.parse(v) as { usage: number }).usage; + } + + static async setUsage(userUuid: string, usage: number): Promise { + const r = Redis.instance; + + await r.set(`${userUuid}-usage`, JSON.stringify({ usage }), 'EX', 10*60); + } + static async releaseLock(key: string) { const r = Redis.instance;