Browse Source

fix: base and workspace cache

pull/7202/head
mertmit 1 year 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,
});
if (baseUser) {
const user = await User.get(userId, ncMeta);
baseUser = { ...user, ...baseUser };
const {
id,
email,
invite_token,
roles: main_roles,
} = await User.get(userId, ncMeta);
baseUser = {
...baseUser,
id,
email,
invite_token,
main_roles,
};
await NocoCache.set(
`${CacheScope.BASE_USER}:${baseId}:${userId}`,
baseUser,
@ -137,10 +150,14 @@ export default class BaseUser {
baseUsers = await queryBuilder;
baseUsers = baseUsers.map((baseUser) => {
baseUser.base_id = base_id;
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;

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

@ -3,7 +3,12 @@ import { NcError } from '~/helpers/catchError';
import Noco from '~/Noco';
import { extractProps } from '~/helpers/extractProps';
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 { 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
await NocoCache.del(`${CacheScope.USER}:${existingUser.email}`);
// get existing cache
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);
}
}
await ncMeta.metaUpdate(null, null, MetaTable.USERS, updateObj, id);
// set meta
return await ncMeta.metaUpdate(null, null, MetaTable.USERS, updateObj, id);
// clear all user related cache
await this.clearCache(id);
return this.get(id, 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');
// clear all user related cache
await NocoCache.del(`${CacheScope.USER}:${userId}`);
await NocoCache.del(`${CacheScope.USER}:${user.email}`);
await this.clearCache(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,
} 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