From 0e792f3ba1bc8511303b09ada5b8aaabac13a643 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Thu, 18 Jan 2024 21:26:07 +0000 Subject: [PATCH 1/3] refactor: replace all delAll pattern based delete for singleQuery --- packages/nocodb/src/models/Column.ts | 12 ++-- packages/nocodb/src/models/Filter.ts | 26 ++++----- .../nocodb/src/models/GalleryViewColumn.ts | 5 +- packages/nocodb/src/models/GridViewColumn.ts | 10 +--- packages/nocodb/src/models/Model.ts | 7 +-- packages/nocodb/src/models/Sort.ts | 20 ++----- packages/nocodb/src/models/View.ts | 58 ++++++++++++++----- 7 files changed, 66 insertions(+), 72 deletions(-) diff --git a/packages/nocodb/src/models/Column.ts b/packages/nocodb/src/models/Column.ts index 852f5eb8d0..5d4cae6c79 100644 --- a/packages/nocodb/src/models/Column.ts +++ b/packages/nocodb/src/models/Column.ts @@ -196,10 +196,7 @@ export default class Column implements ColumnType { ncMeta, ); - await NocoCache.delAll( - CacheScope.SINGLE_QUERY, - `${column.fk_model_id}:default:*`, - ); + await View.clearSingleQueryCache(view.fk_model_id, []); if (column.view_id) { const viewColId = await View.getViewColumnId( @@ -940,7 +937,7 @@ export default class Column implements ColumnType { // on column delete, delete any optimised single query cache { - await NocoCache.delAll(CacheScope.SINGLE_QUERY, `${col.fk_model_id}:*`); + await View.clearSingleQueryCache(col.fk_model_id); } } @@ -1163,7 +1160,7 @@ export default class Column implements ColumnType { await this.insertColOption(column, colId, ncMeta); // on column update, delete any optimised single query cache - await NocoCache.delAll(CacheScope.SINGLE_QUERY, `${oldCol.fk_model_id}:*`); + await View.clearSingleQueryCache(oldCol.fk_model_id); const updatedColumn = await Column.get({ colId }); if (!skipFormulaInvalidate) { @@ -1217,7 +1214,8 @@ export default class Column implements ColumnType { ); const column = await Column.get({ colId }, ncMeta); - await NocoCache.delAll(CacheScope.SINGLE_QUERY, `${column.fk_model_id}:*`); + + await View.clearSingleQueryCache(column.fk_model_id, []); } public getValidators(): any { diff --git a/packages/nocodb/src/models/Filter.ts b/packages/nocodb/src/models/Filter.ts index 115643b0e3..fd63c0f818 100644 --- a/packages/nocodb/src/models/Filter.ts +++ b/packages/nocodb/src/models/Filter.ts @@ -211,10 +211,8 @@ export default class Filter implements FilterType { // if not a view filter then no need to delete if (filter.fk_view_id) { const view = await View.get(filter.fk_view_id, ncMeta); - await NocoCache.delAll( - CacheScope.SINGLE_QUERY, - `${view.fk_model_id}:${view.id}:*`, - ); + + await View.clearSingleQueryCache(view.fk_model_id, [view]); } } @@ -259,10 +257,9 @@ export default class Filter implements FilterType { // if not a view filter then no need to delete if (filter.fk_view_id) { const view = await View.get(filter.fk_view_id, ncMeta); - await NocoCache.delAll( - CacheScope.SINGLE_QUERY, - `${view.fk_model_id}:${view.id}:*`, - ); + await View.clearSingleQueryCache(view.fk_model_id, [ + { id: filter.fk_view_id }, + ]); } } @@ -290,10 +287,10 @@ export default class Filter implements FilterType { // if not a view filter then no need to delete if (filter.fk_view_id) { const view = await View.get(filter.fk_view_id, ncMeta); - await NocoCache.delAll( - CacheScope.SINGLE_QUERY, - `${view.fk_model_id}:${view.id}:*`, - ); + + await View.clearSingleQueryCache(view.fk_model_id, [ + { id: filter.fk_view_id }, + ]); } } } @@ -448,10 +445,7 @@ export default class Filter implements FilterType { // on update delete any optimised single query cache { const view = await View.get(viewId, ncMeta); - await NocoCache.delAll( - CacheScope.SINGLE_QUERY, - `${view.fk_model_id}:${view.id}:*`, - ); + await View.clearSingleQueryCache(view.fk_model_id, [view]); } } diff --git a/packages/nocodb/src/models/GalleryViewColumn.ts b/packages/nocodb/src/models/GalleryViewColumn.ts index 39f38b845f..882625f46b 100644 --- a/packages/nocodb/src/models/GalleryViewColumn.ts +++ b/packages/nocodb/src/models/GalleryViewColumn.ts @@ -93,10 +93,7 @@ export default class GalleryViewColumn { // on new view column, delete any optimised single query cache { const view = await View.get(column.fk_view_id, ncMeta); - await NocoCache.delAll( - CacheScope.SINGLE_QUERY, - `${view.fk_model_id}:${view.id}:*`, - ); + await View.clearSingleQueryCache(view.fk_model_id, [view]); } return this.get(id, ncMeta); diff --git a/packages/nocodb/src/models/GridViewColumn.ts b/packages/nocodb/src/models/GridViewColumn.ts index 89a072ac9d..21cee0a029 100644 --- a/packages/nocodb/src/models/GridViewColumn.ts +++ b/packages/nocodb/src/models/GridViewColumn.ts @@ -125,10 +125,7 @@ export default class GridViewColumn implements GridColumnType { // on new view column, delete any optimised single query cache { const view = await View.get(column.fk_view_id, ncMeta); - await NocoCache.delAll( - CacheScope.SINGLE_QUERY, - `${view.fk_model_id}:${view.id}:*`, - ); + await View.clearSingleQueryCache(view.fk_model_id, [view]); } return this.get(id, ncMeta); @@ -169,10 +166,7 @@ export default class GridViewColumn implements GridColumnType { { const gridCol = await this.get(columnId, ncMeta); const view = await View.get(gridCol.fk_view_id, ncMeta); - await NocoCache.delAll( - CacheScope.SINGLE_QUERY, - `${view.fk_model_id}:${view.id}:*`, - ); + await View.clearSingleQueryCache(view.fk_model_id, [view]); } return res; diff --git a/packages/nocodb/src/models/Model.ts b/packages/nocodb/src/models/Model.ts index ebe5ad416e..792e7743d1 100644 --- a/packages/nocodb/src/models/Model.ts +++ b/packages/nocodb/src/models/Model.ts @@ -658,7 +658,7 @@ export default class Model implements TableType { ); // clear all the cached query under this model - await NocoCache.delAll(CacheScope.SINGLE_QUERY, `${tableId}:*`); + await View.clearSingleQueryCache(view.fk_model_id, []); // clear all the cached query under related models for (const col of await this.get(tableId).then((t) => t.getColumns())) { @@ -668,10 +668,7 @@ export default class Model implements TableType { if (colOptions.fk_related_model_id === tableId) continue; - await NocoCache.delAll( - CacheScope.SINGLE_QUERY, - `${colOptions.fk_related_model_id}:*`, - ); + await View.clearSingleQueryCache(colOptions.fk_related_model_id, []); } return res; diff --git a/packages/nocodb/src/models/Sort.ts b/packages/nocodb/src/models/Sort.ts index 8e8e3dccae..d10750695c 100644 --- a/packages/nocodb/src/models/Sort.ts +++ b/packages/nocodb/src/models/Sort.ts @@ -38,10 +38,7 @@ export default class Sort { // on delete, delete any optimised single query cache { const view = await View.get(viewId, ncMeta); - await NocoCache.delAll( - CacheScope.SINGLE_QUERY, - `${view.fk_model_id}:${view.id}:*`, - ); + await View.clearSingleQueryCache(view.fk_model_id, [view]); } } @@ -113,10 +110,7 @@ export default class Sort { // on insert, delete any optimised single query cache { const view = await View.get(row.fk_view_id, ncMeta); - await NocoCache.delAll( - CacheScope.SINGLE_QUERY, - `${view.fk_model_id}:${view.id}:*`, - ); + await View.clearSingleQueryCache(view.fk_model_id, [view]); } return this.get(row.id, ncMeta); @@ -181,10 +175,7 @@ export default class Sort { { const sort = await this.get(sortId, ncMeta); const view = await View.get(sort.fk_view_id, ncMeta); - await NocoCache.delAll( - CacheScope.SINGLE_QUERY, - `${view.fk_model_id}:${view.id}:*`, - ); + await View.clearSingleQueryCache(view.fk_model_id, [view]); } return res; @@ -202,10 +193,7 @@ export default class Sort { // on delete, delete any optimised single query cache if (sort?.fk_view_id) { const view = await View.get(sort.fk_view_id, ncMeta); - await NocoCache.delAll( - CacheScope.SINGLE_QUERY, - `${view.fk_model_id}:${view.id}:*`, - ); + await View.clearSingleQueryCache(view.fk_model_id, [view]); } } diff --git a/packages/nocodb/src/models/View.ts b/packages/nocodb/src/models/View.ts index 51e06faeef..7f21d44e2b 100644 --- a/packages/nocodb/src/models/View.ts +++ b/packages/nocodb/src/models/View.ts @@ -815,10 +815,7 @@ export default class View implements ViewType { const res = await ncMeta.metaUpdate(null, null, table, updateObj, colId); // on view column update, delete corresponding single query cache - await NocoCache.delAll( - CacheScope.SINGLE_QUERY, - `${view.fk_model_id}:${view.id}:*`, - ); + await View.clearSingleQueryCache(view.fk_model_id, [view]); return res; } @@ -860,10 +857,7 @@ export default class View implements ViewType { ); // on view column update, delete any optimised single query cache - await NocoCache.delAll( - CacheScope.SINGLE_QUERY, - `${view.fk_model_id}:${view.id}:*`, - ); + await View.clearSingleQueryCache(view.fk_model_id, [view]); return { ...existingCol, ...colData }; } else { @@ -1094,10 +1088,7 @@ export default class View implements ViewType { } // on update, delete any optimised single query cache - await NocoCache.delAll( - CacheScope.SINGLE_QUERY, - `${view.fk_model_id}:${view.id}:*`, - ); + await View.clearSingleQueryCache(view.fk_model_id, [view]); return view; } @@ -1137,10 +1128,7 @@ export default class View implements ViewType { await NocoCache.del(`${CacheScope.VIEW}:${view.fk_model_id}:${view.id}`); // on update, delete any optimised single query cache - await NocoCache.delAll( - CacheScope.SINGLE_QUERY, - `${view.fk_model_id}:${view.id}:*`, - ); + await View.clearSingleQueryCache(view.fk_model_id, [view]); await Model.getNonDefaultViewsCountAndReset( { modelId: view.fk_model_id }, @@ -1514,4 +1502,42 @@ export default class View implements ViewType { ); await NocoCache.setList(CacheScope.GRID_VIEW_COLUMN, [viewId], views); } + + public static clearSingleQueryCache( + modelId: string, + views?: { id?: string }[], + ncMeta = Noco.ncMeta, + ) { + // get all views of the model + let viewsList = + views || (await NocoCache.getList(CacheScope.VIEW, [modelId])).list; + + if (!views && !viewsList?.length) { + viewsList = await ncMeta.metaList2(null, null, MetaTable.VIEWS, { + condition: { + fk_model_id: modelId, + }, + }); + } + + // clear cache for each view + await Promise.all([ + ...viewsList.map(async (view) => { + await NocoCache.del( + `${CacheScope.SINGLE_QUERY}:${modelId}:${view.id}:queries`, + ); + await NocoCache.del( + `${CacheScope.SINGLE_QUERY}:${modelId}:${view.id}:read`, + ); + }), + (async () => { + await NocoCache.del( + `${CacheScope.SINGLE_QUERY}:${modelId}:default:queries`, + ); + await NocoCache.del( + `${CacheScope.SINGLE_QUERY}:${modelId}:default:read`, + ); + })(), + ]); + } } From 906f5841f4584051b12f40cac19ea5cb00fa2cc8 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Thu, 18 Jan 2024 21:26:07 +0000 Subject: [PATCH 2/3] fix: corrections --- packages/nocodb/src/models/Column.ts | 2 +- packages/nocodb/src/models/Model.ts | 2 +- packages/nocodb/src/models/View.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/nocodb/src/models/Column.ts b/packages/nocodb/src/models/Column.ts index 5d4cae6c79..9b7ffd464b 100644 --- a/packages/nocodb/src/models/Column.ts +++ b/packages/nocodb/src/models/Column.ts @@ -196,7 +196,7 @@ export default class Column implements ColumnType { ncMeta, ); - await View.clearSingleQueryCache(view.fk_model_id, []); + await View.clearSingleQueryCache(column.fk_model_id, []); if (column.view_id) { const viewColId = await View.getViewColumnId( diff --git a/packages/nocodb/src/models/Model.ts b/packages/nocodb/src/models/Model.ts index 792e7743d1..83a97d9c60 100644 --- a/packages/nocodb/src/models/Model.ts +++ b/packages/nocodb/src/models/Model.ts @@ -658,7 +658,7 @@ export default class Model implements TableType { ); // clear all the cached query under this model - await View.clearSingleQueryCache(view.fk_model_id, []); + await View.clearSingleQueryCache(tableId, []); // clear all the cached query under related models for (const col of await this.get(tableId).then((t) => t.getColumns())) { diff --git a/packages/nocodb/src/models/View.ts b/packages/nocodb/src/models/View.ts index 7f21d44e2b..acf3b0f075 100644 --- a/packages/nocodb/src/models/View.ts +++ b/packages/nocodb/src/models/View.ts @@ -1503,7 +1503,7 @@ export default class View implements ViewType { await NocoCache.setList(CacheScope.GRID_VIEW_COLUMN, [viewId], views); } - public static clearSingleQueryCache( + public static async clearSingleQueryCache( modelId: string, views?: { id?: string }[], ncMeta = Noco.ncMeta, From 0d5e15e765a7fb33d52145ad91bbae556bc87137 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Thu, 18 Jan 2024 21:26:07 +0000 Subject: [PATCH 3/3] fix: pass proper model id --- packages/nocodb/src/models/Column.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nocodb/src/models/Column.ts b/packages/nocodb/src/models/Column.ts index 9b7ffd464b..efea0d2238 100644 --- a/packages/nocodb/src/models/Column.ts +++ b/packages/nocodb/src/models/Column.ts @@ -1168,7 +1168,7 @@ export default class Column implements ColumnType { // whenever a new request comes for that formula, it will be populated again getFormulasReferredTheColumn({ column: updatedColumn, - columns: await Column.list({ fk_model_id: column.fk_model_id }, ncMeta), + columns: await Column.list({ fk_model_id: oldCol.fk_model_id }, ncMeta), }) .then(async (formulas) => { for (const formula of formulas) {