diff --git a/packages/nocodb/src/models/Column.ts b/packages/nocodb/src/models/Column.ts index ede9becbf2..a2670acca7 100644 --- a/packages/nocodb/src/models/Column.ts +++ b/packages/nocodb/src/models/Column.ts @@ -203,10 +203,7 @@ export default class Column implements ColumnType { ncMeta, ); - await NocoCache.delAll( - CacheScope.SINGLE_QUERY, - `${column.fk_model_id}:default:*`, - ); + await View.clearSingleQueryCache(column.fk_model_id, []); if (column.view_id) { const viewColId = await View.getViewColumnId( @@ -947,7 +944,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); } } @@ -1170,7 +1167,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) { @@ -1178,7 +1175,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) { @@ -1224,7 +1221,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..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 NocoCache.delAll(CacheScope.SINGLE_QUERY, `${tableId}:*`); + await View.clearSingleQueryCache(tableId, []); // 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..acf3b0f075 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 async 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`, + ); + })(), + ]); + } }