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

22 lines
561 B
JavaScript
Raw Normal View History

2021-08-04 11:02:24 +02:00
const { getDB } = require("../db")
const { StaticDatabases } = require("../db/utils")
2021-07-06 19:10:04 +02:00
const redis = require("../redis/authRedis")
const EXPIRY_SECONDS = 3600
2021-08-04 11:02:24 +02:00
exports.getUser = async userId => {
2021-07-06 19:10:04 +02:00
const client = await redis.getUserClient()
// try cache
let user = await client.get(userId)
if (!user) {
2021-08-04 11:02:24 +02:00
user = await getDB(StaticDatabases.GLOBAL.name).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)
}