Add caching for user permissions in Redis
All checks were successful
farmcontrol/farmcontrol-ws/pipeline/head This commit looks good
All checks were successful
farmcontrol/farmcontrol-ws/pipeline/head This commit looks good
- Introduced utility functions to save user permissions to Redis, enhancing performance by reducing database load. - Updated user schema to call the new caching function during permission recalculation, ensuring permissions are stored efficiently. - Improved overall permission management by integrating Redis caching into the existing permission handling workflow.
This commit is contained in:
parent
998bb725a2
commit
ecbdfb97c1
@ -1,4 +1,15 @@
|
||||
import mongoose from 'mongoose';
|
||||
import { redisServer } from './redis.js';
|
||||
|
||||
export const getUserPermissionsCacheKey = (userId) => `permissions:${userId}`;
|
||||
|
||||
export const saveUserPermissionsToRedis = async (userId, permissions = {}) => {
|
||||
if (!userId) {
|
||||
return;
|
||||
}
|
||||
|
||||
await redisServer.setKey(getUserPermissionsCacheKey(userId), permissions || {});
|
||||
};
|
||||
|
||||
export const applyPermissionSettingsList = (settingsList = []) => {
|
||||
const permissions = {};
|
||||
|
||||
@ -4,6 +4,7 @@ import {
|
||||
applyPermissionSettingsList,
|
||||
resolvePermissionSettings,
|
||||
resolveReferencedDocs,
|
||||
saveUserPermissionsToRedis,
|
||||
} from '../../permissions.js';
|
||||
|
||||
const { Schema } = mongoose;
|
||||
@ -47,6 +48,7 @@ userSchema.statics.recalculate = async function (user, actingUser) {
|
||||
if (user && typeof user === 'object' && !user._bsontype) {
|
||||
user.permissions = permissions;
|
||||
}
|
||||
await saveUserPermissionsToRedis(userId, permissions);
|
||||
const { editObject } = await import('../../database.js');
|
||||
|
||||
await editObject({
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user