Browse Source

feat: NocoCache update

pull/7727/head
mertmit 7 months ago
parent
commit
b14b98341a
  1. 11
      packages/nocodb/src/cache/CacheMgr.ts
  2. 8
      packages/nocodb/src/cache/NocoCache.ts
  3. 10
      packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts
  4. 14
      packages/nocodb/src/models/BaseUser.ts
  5. 13
      packages/nocodb/src/models/CalendarView.ts
  6. 35
      packages/nocodb/src/models/Column.ts
  7. 11
      packages/nocodb/src/models/Filter.ts
  8. 25
      packages/nocodb/src/models/FormView.ts
  9. 10
      packages/nocodb/src/models/FormulaColumn.ts
  10. 18
      packages/nocodb/src/models/GalleryView.ts
  11. 24
      packages/nocodb/src/models/GridView.ts
  12. 15
      packages/nocodb/src/models/GridViewColumn.ts
  13. 13
      packages/nocodb/src/models/Hook.ts
  14. 13
      packages/nocodb/src/models/HookFilter.ts
  15. 15
      packages/nocodb/src/models/KanbanView.ts
  16. 26
      packages/nocodb/src/models/MapView.ts
  17. 13
      packages/nocodb/src/models/Plugin.ts
  18. 9
      packages/nocodb/src/models/Source.ts
  19. 11
      packages/nocodb/src/models/View.ts

11
packages/nocodb/src/cache/CacheMgr.ts vendored

@ -500,6 +500,17 @@ export default abstract class CacheMgr {
});
}
async update(key: string, value: any): Promise<boolean> {
let o = await this.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
// update data
o = { ...o, ...value };
// set cache
await this.set(key, o);
}
return true;
}
// wrap value with metadata
prepareValue(args: {
value: any;

8
packages/nocodb/src/cache/NocoCache.ts vendored

@ -106,6 +106,14 @@ export default class NocoCache {
);
}
public static async update(
key: string,
updateObj: Record<string, any>,
): Promise<boolean> {
if (this.cacheDisabled) return Promise.resolve(true);
return this.client.update(`${this.prefix}:${key}`, updateObj);
}
public static async destroy(): Promise<boolean> {
if (this.cacheDisabled) return Promise.resolve(true);
return this.client.destroy();

10
packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts

@ -1217,13 +1217,9 @@ export default async function formulaQueryBuilderv2(
error: e.message,
});
// update cache to reflect the error in UI
const key = `${CacheScope.COL_FORMULA}:${column.id}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
o = { ...o, error: e.message };
// set cache
await NocoCache.set(key, o);
}
await NocoCache.update(`${CacheScope.COL_FORMULA}:${column.id}`, {
error: e.message,
});
}
throw new Error(`Formula error: ${e.message}`);
}

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

@ -237,23 +237,17 @@ export default class BaseUser {
) {
const updateObj = extractProps(baseUser, ['starred', 'hidden', 'order']);
const key = `${CacheScope.BASE_USER}:${baseId}:${userId}`;
// set meta
await ncMeta.metaUpdate(null, null, MetaTable.PROJECT_USERS, updateObj, {
fk_user_id: userId,
base_id: baseId,
});
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
// update data
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
}
await NocoCache.update(
`${CacheScope.BASE_USER}:${baseId}:${userId}`,
updateObj,
);
// cache and return
return await this.get(baseId, userId, ncMeta);
}

13
packages/nocodb/src/models/CalendarView.ts

@ -85,21 +85,16 @@ export default class CalendarView implements CalendarType {
body: Partial<CalendarView>,
ncMeta = Noco.ncMeta,
) {
// get existing cache
const key = `${CacheScope.CALENDAR_VIEW}:${calendarId}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
const updateObj = extractProps(body, ['fk_cover_image_col_id', 'meta']);
if (updateObj.meta && typeof updateObj.meta === 'object') {
updateObj.meta = JSON.stringify(updateObj.meta ?? {});
}
if (o) {
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
}
await NocoCache.update(
`${CacheScope.CALENDAR_VIEW}:${calendarId}`,
updateObj,
);
if (body.calendar_range) {
await NocoCache.del(`${CacheScope.CALENDAR_VIEW}:${calendarId}`);

35
packages/nocodb/src/models/Column.ts

@ -1084,38 +1084,6 @@ export default class Column<T = any> implements ColumnType {
});
}
// get existing cache
const key = `${CacheScope.COLUMN}:${colId}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
}
// get model column list from cache
const columnListFromCache = await NocoCache.getList(CacheScope.COLUMN, [
oldCol.fk_model_id,
]);
// update column list in cache if cache exists
if (!columnListFromCache.list?.length) {
const updatedColumnList = columnListFromCache.list.map((column: any) => {
if (column.id === colId) {
return {
...column,
...updateObj,
};
}
return column;
});
await NocoCache.setList(
CacheScope.COLUMN,
[oldCol.fk_model_id],
updatedColumnList,
);
}
// set meta
await ncMeta.metaUpdate(
null,
@ -1130,6 +1098,9 @@ export default class Column<T = any> implements ColumnType {
},
colId,
);
await NocoCache.update(`${CacheScope.COLUMN}:${colId}`, updateObj);
await this.insertColOption(column, colId, ncMeta);
// on column update, delete any optimised single query cache

11
packages/nocodb/src/models/Filter.ts

@ -234,15 +234,6 @@ export default class Filter implements FilterType {
if (typeof updateObj.value === 'string')
updateObj.value = updateObj.value.slice(0, 255);
// get existing cache
const key = `${CacheScope.FILTER_EXP}:${id}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
// update alias
if (o) {
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
}
// set meta
const res = await ncMeta.metaUpdate(
null,
@ -252,6 +243,8 @@ export default class Filter implements FilterType {
id,
);
await NocoCache.update(`${CacheScope.FILTER_EXP}:${id}`, updateObj);
// on update delete any optimised single query cache
{
const filter = await this.get(id, ncMeta);

25
packages/nocodb/src/models/FormView.ts

@ -88,9 +88,6 @@ export default class FormView implements FormType {
body: Partial<FormView>,
ncMeta = Noco.ncMeta,
) {
// get existing cache
const key = `${CacheScope.FORM_VIEW}:${formId}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
const updateObj = extractProps(body, [
'heading',
'subheading',
@ -105,20 +102,24 @@ export default class FormView implements FormType {
'meta',
]);
if (o) {
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
}
if (updateObj.meta) {
updateObj.meta = serializeJSON(updateObj.meta);
}
// update meta
return await ncMeta.metaUpdate(null, null, MetaTable.FORM_VIEW, updateObj, {
fk_view_id: formId,
});
const res = await ncMeta.metaUpdate(
null,
null,
MetaTable.FORM_VIEW,
updateObj,
{
fk_view_id: formId,
},
);
await NocoCache.update(`${CacheScope.FORM_VIEW}:${formId}`, updateObj);
return res;
}
async getColumns(ncMeta = Noco.ncMeta) {

10
packages/nocodb/src/models/FormulaColumn.ts

@ -74,20 +74,14 @@ export default class FormulaColumn {
'parsed_tree',
]);
// get existing cache
const key = `${CacheScope.COL_FORMULA}:${columnId}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
}
if ('parsed_tree' in updateObj)
updateObj.parsed_tree = stringifyMetaProp(updateObj, 'parsed_tree');
// set meta
await ncMeta.metaUpdate(null, null, MetaTable.COL_FORMULA, updateObj, {
fk_column_id: columnId,
});
await NocoCache.update(`${CacheScope.COL_FORMULA}:${columnId}`, updateObj);
}
public getParsedTree() {

18
packages/nocodb/src/models/GalleryView.ts

@ -98,22 +98,13 @@ export default class GalleryView implements GalleryType {
body: Partial<GalleryView>,
ncMeta = Noco.ncMeta,
) {
// get existing cache
const key = `${CacheScope.GALLERY_VIEW}:${galleryId}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
const updateObj = extractProps(body, ['fk_cover_image_col_id', 'meta']);
if (updateObj.meta && typeof updateObj.meta === 'object') {
updateObj.meta = JSON.stringify(updateObj.meta ?? {});
}
if (o) {
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
}
// update meta
return await ncMeta.metaUpdate(
const res = await ncMeta.metaUpdate(
null,
null,
MetaTable.GALLERY_VIEW,
@ -122,5 +113,12 @@ export default class GalleryView implements GalleryType {
fk_view_id: galleryId,
},
);
await NocoCache.update(
`${CacheScope.GALLERY_VIEW}:${galleryId}`,
updateObj,
);
return res;
}
}

24
packages/nocodb/src/models/GridView.ts

@ -68,23 +68,25 @@ export default class GridView implements GridType {
body: Partial<GridView>,
ncMeta = Noco.ncMeta,
) {
// get existing cache
const key = `${CacheScope.GRID_VIEW}:${viewId}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
const updateObj = extractProps(body, ['row_height', 'meta']);
if (updateObj.meta && typeof updateObj.meta === 'object') {
updateObj.meta = JSON.stringify(updateObj.meta ?? {});
}
if (o) {
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
}
// update meta
return await ncMeta.metaUpdate(null, null, MetaTable.GRID_VIEW, updateObj, {
fk_view_id: viewId,
});
const res = await ncMeta.metaUpdate(
null,
null,
MetaTable.GRID_VIEW,
updateObj,
{
fk_view_id: viewId,
},
);
await NocoCache.update(`${CacheScope.GRID_VIEW}:${viewId}`, updateObj);
return res;
}
}

15
packages/nocodb/src/models/GridViewColumn.ts

@ -140,15 +140,7 @@ export default class GridViewColumn implements GridColumnType {
'group_by_order',
'group_by_sort',
]);
// get existing cache
const key = `${CacheScope.GRID_VIEW_COLUMN}:${columnId}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
// update data
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
}
// set meta
const res = await ncMeta.metaUpdate(
null,
@ -158,6 +150,11 @@ export default class GridViewColumn implements GridColumnType {
columnId,
);
await NocoCache.update(
`${CacheScope.GRID_VIEW_COLUMN}:${columnId}`,
updateObj,
);
// on view column update, delete any optimised single query cache
{
const gridCol = await this.get(columnId, ncMeta);

13
packages/nocodb/src/models/Hook.ts

@ -209,20 +209,11 @@ export default class Hook implements HookType {
updateObj.notification = JSON.stringify(updateObj.notification);
}
// get existing cache
const key = `${CacheScope.HOOK}:${hookId}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
// update data
o = { ...o, ...updateObj };
// replace notification
o.notification = updateObj.notification;
// set cache
await NocoCache.set(key, o);
}
// set meta
await ncMeta.metaUpdate(null, null, MetaTable.HOOKS, updateObj, hookId);
await NocoCache.update(`${CacheScope.HOOK}:${hookId}`, updateObj);
return this.get(hookId, ncMeta);
}

13
packages/nocodb/src/models/HookFilter.ts

@ -152,17 +152,12 @@ export default class Filter {
'is_group',
'logical_op',
]);
// get existing cache
const key = `${CacheScope.FILTER_EXP}:${id}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
// update alias
if (o) {
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
}
// set meta
await ncMeta.metaUpdate(null, null, MetaTable.FILTER_EXP, updateObj, id);
// update cache
await NocoCache.update(`${CacheScope.FILTER_EXP}:${id}`, updateObj);
}
static async delete(id: string, ncMeta = Noco.ncMeta) {

15
packages/nocodb/src/models/KanbanView.ts

@ -99,10 +99,6 @@ export default class KanbanView implements KanbanType {
body: Partial<KanbanView>,
ncMeta = Noco.ncMeta,
) {
// get existing cache
const key = `${CacheScope.KANBAN_VIEW}:${kanbanId}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
const updateObj = extractProps(body, [
'fk_cover_image_col_id',
'fk_grp_col_id',
@ -113,13 +109,8 @@ export default class KanbanView implements KanbanType {
updateObj.meta = JSON.stringify(updateObj.meta ?? {});
}
if (o) {
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
}
// update meta
return await ncMeta.metaUpdate(
const res = await ncMeta.metaUpdate(
null,
null,
MetaTable.KANBAN_VIEW,
@ -128,5 +119,9 @@ export default class KanbanView implements KanbanType {
fk_view_id: kanbanId,
},
);
await NocoCache.update(`${CacheScope.KANBAN_VIEW}:${kanbanId}`, updateObj);
return res;
}
}

26
packages/nocodb/src/models/MapView.ts

@ -70,22 +70,12 @@ export default class MapView implements MapType {
body: Partial<MapView>,
ncMeta = Noco.ncMeta,
) {
// get existing cache
const key = `${CacheScope.MAP_VIEW}:${mapId}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
const updateObj = extractProps(body, ['fk_geo_data_col_id', 'meta']);
if (updateObj.meta && typeof updateObj.meta === 'object') {
updateObj.meta = JSON.stringify(updateObj.meta ?? {});
}
if (o) {
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
}
if (body.fk_geo_data_col_id != null) {
const mapViewColumns = await MapViewColumn.list(mapId);
const mapViewMappedByColumn = mapViewColumns.find(
@ -98,8 +88,18 @@ export default class MapView implements MapType {
}
// update meta
return await ncMeta.metaUpdate(null, null, MetaTable.MAP_VIEW, updateObj, {
fk_view_id: mapId,
});
const res = await ncMeta.metaUpdate(
null,
null,
MetaTable.MAP_VIEW,
updateObj,
{
fk_view_id: mapId,
},
);
await NocoCache.update(`${CacheScope.MAP_VIEW}:${mapId}`, updateObj);
return res;
}
}

13
packages/nocodb/src/models/Plugin.ts

@ -65,16 +65,6 @@ export default class Plugin implements PluginType {
updateObj.input = JSON.stringify(updateObj.input);
}
// get existing cache
const key = `${CacheScope.PLUGIN}:${pluginId}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
// update alias
if (o) {
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
await NocoCache.set(`${CacheScope.PLUGIN}:${o.title}`, o);
}
// set meta
await Noco.ncMeta.metaUpdate(
null,
@ -84,6 +74,9 @@ export default class Plugin implements PluginType {
pluginId,
);
await NocoCache.update(`${CacheScope.PLUGIN}:${pluginId}`, updateObj);
await NocoCache.update(`${CacheScope.PLUGIN}:${plugin.title}`, updateObj);
return this.get(pluginId);
}

9
packages/nocodb/src/models/Source.ts

@ -147,14 +147,7 @@ export default class Source implements SourceType {
oldBase.id,
);
const cacheKey = `${CacheScope.BASE}:${sourceId}`;
let o = await NocoCache.get(cacheKey, CacheGetType.TYPE_OBJECT);
if (o) {
// update data
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(cacheKey, o);
}
await NocoCache.update(`${CacheScope.BASE}:${sourceId}`, updateObj);
// call before reorder to update cache
const returnBase = await this.get(oldBase.id, false, ncMeta);

11
packages/nocodb/src/models/View.ts

@ -858,18 +858,11 @@ export default class View implements ViewType {
}
}
// get existing cache
const key = `${cacheScope}:${colId}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
// update data
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
}
// set meta
const res = await ncMeta.metaUpdate(null, null, table, updateObj, colId);
await NocoCache.update(`${cacheScope}:${colId}`, updateObj);
// on view column update, delete corresponding single query cache
await View.clearSingleQueryCache(view.fk_model_id, [view]);

Loading…
Cancel
Save