budibase/packages/auth/src/cache/user.js

25 lines
646 B
JavaScript
Raw Normal View History

const { getGlobalDB } = require("../db/utils")
2021-07-06 19:10:04 +02:00
const redis = require("../redis/authRedis")
const { lookupTenantId } = require("../utils")
2021-07-06 19:10:04 +02:00
const EXPIRY_SECONDS = 3600
exports.getUser = async (userId, tenantId = null) => {
if (!tenantId) {
tenantId = await lookupTenantId({ userId })
}
2021-07-06 19:10:04 +02:00
const client = await redis.getUserClient()
// try cache
let user = await client.get(userId)
if (!user) {
user = await getGlobalDB(tenantId).get(userId)
2021-07-06 19:10:04 +02:00
client.store(userId, user, EXPIRY_SECONDS)
}
return user
}
exports.invalidateUser = async userId => {
const client = await redis.getUserClient()
await client.delete(userId)
}