Browse Source

refactor: replace all delAll pattern based delete for singleQuery

pull/7459/head
Pranav C 11 months ago
parent
commit
0e792f3ba1
  1. 12
      packages/nocodb/src/models/Column.ts
  2. 26
      packages/nocodb/src/models/Filter.ts
  3. 5
      packages/nocodb/src/models/GalleryViewColumn.ts
  4. 10
      packages/nocodb/src/models/GridViewColumn.ts
  5. 7
      packages/nocodb/src/models/Model.ts
  6. 20
      packages/nocodb/src/models/Sort.ts
  7. 58
      packages/nocodb/src/models/View.ts

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

@ -196,10 +196,7 @@ export default class Column<T = any> implements ColumnType {
ncMeta, ncMeta,
); );
await NocoCache.delAll( await View.clearSingleQueryCache(view.fk_model_id, []);
CacheScope.SINGLE_QUERY,
`${column.fk_model_id}:default:*`,
);
if (column.view_id) { if (column.view_id) {
const viewColId = await View.getViewColumnId( const viewColId = await View.getViewColumnId(
@ -940,7 +937,7 @@ export default class Column<T = any> implements ColumnType {
// on column delete, delete any optimised single query cache // 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<T = any> implements ColumnType {
await this.insertColOption(column, colId, ncMeta); await this.insertColOption(column, colId, ncMeta);
// on column update, delete any optimised single query cache // 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 }); const updatedColumn = await Column.get({ colId });
if (!skipFormulaInvalidate) { if (!skipFormulaInvalidate) {
@ -1217,7 +1214,8 @@ export default class Column<T = any> implements ColumnType {
); );
const column = await Column.get({ colId }, ncMeta); 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 { public getValidators(): any {

26
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 not a view filter then no need to delete
if (filter.fk_view_id) { if (filter.fk_view_id) {
const view = await View.get(filter.fk_view_id, ncMeta); const view = await View.get(filter.fk_view_id, ncMeta);
await NocoCache.delAll(
CacheScope.SINGLE_QUERY, await View.clearSingleQueryCache(view.fk_model_id, [view]);
`${view.fk_model_id}:${view.id}:*`,
);
} }
} }
@ -259,10 +257,9 @@ export default class Filter implements FilterType {
// if not a view filter then no need to delete // if not a view filter then no need to delete
if (filter.fk_view_id) { if (filter.fk_view_id) {
const view = await View.get(filter.fk_view_id, ncMeta); const view = await View.get(filter.fk_view_id, ncMeta);
await NocoCache.delAll( await View.clearSingleQueryCache(view.fk_model_id, [
CacheScope.SINGLE_QUERY, { id: filter.fk_view_id },
`${view.fk_model_id}:${view.id}:*`, ]);
);
} }
} }
@ -290,10 +287,10 @@ export default class Filter implements FilterType {
// if not a view filter then no need to delete // if not a view filter then no need to delete
if (filter.fk_view_id) { if (filter.fk_view_id) {
const view = await View.get(filter.fk_view_id, ncMeta); const view = await View.get(filter.fk_view_id, ncMeta);
await NocoCache.delAll(
CacheScope.SINGLE_QUERY, await View.clearSingleQueryCache(view.fk_model_id, [
`${view.fk_model_id}:${view.id}:*`, { id: filter.fk_view_id },
); ]);
} }
} }
} }
@ -448,10 +445,7 @@ export default class Filter implements FilterType {
// on update delete any optimised single query cache // on update delete any optimised single query cache
{ {
const view = await View.get(viewId, ncMeta); const view = await View.get(viewId, ncMeta);
await NocoCache.delAll( await View.clearSingleQueryCache(view.fk_model_id, [view]);
CacheScope.SINGLE_QUERY,
`${view.fk_model_id}:${view.id}:*`,
);
} }
} }

5
packages/nocodb/src/models/GalleryViewColumn.ts

@ -93,10 +93,7 @@ export default class GalleryViewColumn {
// on new view column, delete any optimised single query cache // on new view column, delete any optimised single query cache
{ {
const view = await View.get(column.fk_view_id, ncMeta); const view = await View.get(column.fk_view_id, ncMeta);
await NocoCache.delAll( await View.clearSingleQueryCache(view.fk_model_id, [view]);
CacheScope.SINGLE_QUERY,
`${view.fk_model_id}:${view.id}:*`,
);
} }
return this.get(id, ncMeta); return this.get(id, ncMeta);

10
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 // on new view column, delete any optimised single query cache
{ {
const view = await View.get(column.fk_view_id, ncMeta); const view = await View.get(column.fk_view_id, ncMeta);
await NocoCache.delAll( await View.clearSingleQueryCache(view.fk_model_id, [view]);
CacheScope.SINGLE_QUERY,
`${view.fk_model_id}:${view.id}:*`,
);
} }
return this.get(id, ncMeta); return this.get(id, ncMeta);
@ -169,10 +166,7 @@ export default class GridViewColumn implements GridColumnType {
{ {
const gridCol = await this.get(columnId, ncMeta); const gridCol = await this.get(columnId, ncMeta);
const view = await View.get(gridCol.fk_view_id, ncMeta); const view = await View.get(gridCol.fk_view_id, ncMeta);
await NocoCache.delAll( await View.clearSingleQueryCache(view.fk_model_id, [view]);
CacheScope.SINGLE_QUERY,
`${view.fk_model_id}:${view.id}:*`,
);
} }
return res; return res;

7
packages/nocodb/src/models/Model.ts

@ -658,7 +658,7 @@ export default class Model implements TableType {
); );
// clear all the cached query under this model // 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 // clear all the cached query under related models
for (const col of await this.get(tableId).then((t) => t.getColumns())) { 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; if (colOptions.fk_related_model_id === tableId) continue;
await NocoCache.delAll( await View.clearSingleQueryCache(colOptions.fk_related_model_id, []);
CacheScope.SINGLE_QUERY,
`${colOptions.fk_related_model_id}:*`,
);
} }
return res; return res;

20
packages/nocodb/src/models/Sort.ts

@ -38,10 +38,7 @@ export default class Sort {
// on delete, delete any optimised single query cache // on delete, delete any optimised single query cache
{ {
const view = await View.get(viewId, ncMeta); const view = await View.get(viewId, ncMeta);
await NocoCache.delAll( await View.clearSingleQueryCache(view.fk_model_id, [view]);
CacheScope.SINGLE_QUERY,
`${view.fk_model_id}:${view.id}:*`,
);
} }
} }
@ -113,10 +110,7 @@ export default class Sort {
// on insert, delete any optimised single query cache // on insert, delete any optimised single query cache
{ {
const view = await View.get(row.fk_view_id, ncMeta); const view = await View.get(row.fk_view_id, ncMeta);
await NocoCache.delAll( await View.clearSingleQueryCache(view.fk_model_id, [view]);
CacheScope.SINGLE_QUERY,
`${view.fk_model_id}:${view.id}:*`,
);
} }
return this.get(row.id, ncMeta); return this.get(row.id, ncMeta);
@ -181,10 +175,7 @@ export default class Sort {
{ {
const sort = await this.get(sortId, ncMeta); const sort = await this.get(sortId, ncMeta);
const view = await View.get(sort.fk_view_id, ncMeta); const view = await View.get(sort.fk_view_id, ncMeta);
await NocoCache.delAll( await View.clearSingleQueryCache(view.fk_model_id, [view]);
CacheScope.SINGLE_QUERY,
`${view.fk_model_id}:${view.id}:*`,
);
} }
return res; return res;
@ -202,10 +193,7 @@ export default class Sort {
// on delete, delete any optimised single query cache // on delete, delete any optimised single query cache
if (sort?.fk_view_id) { if (sort?.fk_view_id) {
const view = await View.get(sort.fk_view_id, ncMeta); const view = await View.get(sort.fk_view_id, ncMeta);
await NocoCache.delAll( await View.clearSingleQueryCache(view.fk_model_id, [view]);
CacheScope.SINGLE_QUERY,
`${view.fk_model_id}:${view.id}:*`,
);
} }
} }

58
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); const res = await ncMeta.metaUpdate(null, null, table, updateObj, colId);
// on view column update, delete corresponding single query cache // on view column update, delete corresponding single query cache
await NocoCache.delAll( await View.clearSingleQueryCache(view.fk_model_id, [view]);
CacheScope.SINGLE_QUERY,
`${view.fk_model_id}:${view.id}:*`,
);
return res; return res;
} }
@ -860,10 +857,7 @@ export default class View implements ViewType {
); );
// on view column update, delete any optimised single query cache // on view column update, delete any optimised single query cache
await NocoCache.delAll( await View.clearSingleQueryCache(view.fk_model_id, [view]);
CacheScope.SINGLE_QUERY,
`${view.fk_model_id}:${view.id}:*`,
);
return { ...existingCol, ...colData }; return { ...existingCol, ...colData };
} else { } else {
@ -1094,10 +1088,7 @@ export default class View implements ViewType {
} }
// on update, delete any optimised single query cache // on update, delete any optimised single query cache
await NocoCache.delAll( await View.clearSingleQueryCache(view.fk_model_id, [view]);
CacheScope.SINGLE_QUERY,
`${view.fk_model_id}:${view.id}:*`,
);
return view; return view;
} }
@ -1137,10 +1128,7 @@ export default class View implements ViewType {
await NocoCache.del(`${CacheScope.VIEW}:${view.fk_model_id}:${view.id}`); await NocoCache.del(`${CacheScope.VIEW}:${view.fk_model_id}:${view.id}`);
// on update, delete any optimised single query cache // on update, delete any optimised single query cache
await NocoCache.delAll( await View.clearSingleQueryCache(view.fk_model_id, [view]);
CacheScope.SINGLE_QUERY,
`${view.fk_model_id}:${view.id}:*`,
);
await Model.getNonDefaultViewsCountAndReset( await Model.getNonDefaultViewsCountAndReset(
{ modelId: view.fk_model_id }, { modelId: view.fk_model_id },
@ -1514,4 +1502,42 @@ export default class View implements ViewType {
); );
await NocoCache.setList(CacheScope.GRID_VIEW_COLUMN, [viewId], views); 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`,
);
})(),
]);
}
} }

Loading…
Cancel
Save