Browse Source

fix: base and workspace cache

pull/7202/head
mertmit 12 months ago
parent
commit
1f03ef34fd
  1. 23
      packages/nocodb/src/models/BaseUser.ts
  2. 49
      packages/nocodb/src/models/User.ts

23
packages/nocodb/src/models/BaseUser.ts

@ -73,8 +73,21 @@ export default class BaseUser {
base_id: baseId, base_id: baseId,
}); });
if (baseUser) { if (baseUser) {
const user = await User.get(userId, ncMeta); const {
baseUser = { ...user, ...baseUser }; id,
email,
invite_token,
roles: main_roles,
} = await User.get(userId, ncMeta);
baseUser = {
...baseUser,
id,
email,
invite_token,
main_roles,
};
await NocoCache.set( await NocoCache.set(
`${CacheScope.BASE_USER}:${baseId}:${userId}`, `${CacheScope.BASE_USER}:${baseId}:${userId}`,
baseUser, baseUser,
@ -137,10 +150,14 @@ export default class BaseUser {
baseUsers = await queryBuilder; baseUsers = await queryBuilder;
baseUsers = baseUsers.map((baseUser) => { baseUsers = baseUsers.map((baseUser) => {
baseUser.base_id = base_id;
return this.castType(baseUser); return this.castType(baseUser);
}); });
await NocoCache.setList(CacheScope.BASE_USER, [base_id], baseUsers); await NocoCache.setList(CacheScope.BASE_USER, [base_id], baseUsers, [
'base_id',
'id',
]);
} }
return baseUsers; return baseUsers;

49
packages/nocodb/src/models/User.ts

@ -3,7 +3,12 @@ import { NcError } from '~/helpers/catchError';
import Noco from '~/Noco'; import Noco from '~/Noco';
import { extractProps } from '~/helpers/extractProps'; import { extractProps } from '~/helpers/extractProps';
import NocoCache from '~/cache/NocoCache'; import NocoCache from '~/cache/NocoCache';
import { CacheGetType, CacheScope, MetaTable } from '~/utils/globals'; import {
CacheDelDirection,
CacheGetType,
CacheScope,
MetaTable,
} from '~/utils/globals';
import { BaseUser } from '~/models'; import { BaseUser } from '~/models';
import { sanitiseUserObj } from '~/utils'; import { sanitiseUserObj } from '~/utils';
@ -106,22 +111,12 @@ export default class User implements UserType {
// delete the email-based cache to avoid unexpected behaviour since we can update email as well // delete the email-based cache to avoid unexpected behaviour since we can update email as well
await NocoCache.del(`${CacheScope.USER}:${existingUser.email}`); await NocoCache.del(`${CacheScope.USER}:${existingUser.email}`);
// get existing cache await ncMeta.metaUpdate(null, null, MetaTable.USERS, updateObj, id);
const keys = [
// update user:<id>
`${CacheScope.USER}:${id}`,
];
for (const key of keys) {
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
}
}
// set meta // clear all user related cache
return await ncMeta.metaUpdate(null, null, MetaTable.USERS, updateObj, id); await this.clearCache(id);
return this.get(id, ncMeta);
} }
public static async getByEmail(_email: string, ncMeta = Noco.ncMeta) { public static async getByEmail(_email: string, ncMeta = Noco.ncMeta) {
@ -237,8 +232,7 @@ export default class User implements UserType {
if (!user) NcError.badRequest('User not found'); if (!user) NcError.badRequest('User not found');
// clear all user related cache // clear all user related cache
await NocoCache.del(`${CacheScope.USER}:${userId}`); await this.clearCache(userId);
await NocoCache.del(`${CacheScope.USER}:${user.email}`);
return await ncMeta.metaDelete(null, null, MetaTable.USERS, userId); return await ncMeta.metaDelete(null, null, MetaTable.USERS, userId);
} }
@ -278,4 +272,23 @@ export default class User implements UserType {
base_roles: baseRoles ? baseRoles : null, base_roles: baseRoles ? baseRoles : null,
} as any; } as any;
} }
protected static async clearCache(userId: string) {
const user = await this.get(userId);
if (!user) NcError.badRequest('User not found');
const bases = await BaseUser.getProjectsList(userId, {});
for (const base of bases) {
await NocoCache.deepDel(
CacheScope.BASE_USER,
`${CacheScope.BASE_USER}:${base.id}:list`,
CacheDelDirection.PARENT_TO_CHILD,
);
}
// clear all user related cache
await NocoCache.del(`${CacheScope.USER}:${userId}`);
await NocoCache.del(`${CacheScope.USER}:${user.email}`);
}
} }

Loading…
Cancel
Save