From f8f2fd6e1daa92049d7b2cccab3bba2805fac389 Mon Sep 17 00:00:00 2001 From: mertmit Date: Mon, 11 Dec 2023 15:58:33 +0000 Subject: [PATCH] feat: allow custom prefix for setList --- packages/nocodb/src/cache/CacheMgr.ts | 1 + packages/nocodb/src/cache/NocoCache.ts | 3 ++- packages/nocodb/src/cache/RedisCacheMgr.ts | 16 ++++++++++++---- packages/nocodb/src/cache/RedisMockCacheMgr.ts | 16 ++++++++++++---- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/packages/nocodb/src/cache/CacheMgr.ts b/packages/nocodb/src/cache/CacheMgr.ts index 21f8300b2d..81e0654bfa 100644 --- a/packages/nocodb/src/cache/CacheMgr.ts +++ b/packages/nocodb/src/cache/CacheMgr.ts @@ -21,6 +21,7 @@ export default abstract class CacheMgr { scope: string, subListKeys: string[], list: any[], + props?: string[], ): Promise; public abstract deepDel( scope: string, diff --git a/packages/nocodb/src/cache/NocoCache.ts b/packages/nocodb/src/cache/NocoCache.ts index 9dda63a4c1..bd24c9fc6c 100644 --- a/packages/nocodb/src/cache/NocoCache.ts +++ b/packages/nocodb/src/cache/NocoCache.ts @@ -86,9 +86,10 @@ export default class NocoCache { scope: string, subListKeys: string[], list: any[], + props: string[] = [], ): Promise { if (this.cacheDisabled) return Promise.resolve(true); - return this.client.setList(scope, subListKeys, list); + return this.client.setList(scope, subListKeys, list, props); } public static async deepDel( diff --git a/packages/nocodb/src/cache/RedisCacheMgr.ts b/packages/nocodb/src/cache/RedisCacheMgr.ts index 9dacc2385a..c7d4f89357 100644 --- a/packages/nocodb/src/cache/RedisCacheMgr.ts +++ b/packages/nocodb/src/cache/RedisCacheMgr.ts @@ -185,6 +185,7 @@ export default class RedisCacheMgr extends CacheMgr { scope: string, subListKeys: string[], list: any[], + props: string[] = [], ): Promise { // remove null from arrays subListKeys = subListKeys.filter((k) => k); @@ -203,11 +204,18 @@ export default class RedisCacheMgr extends CacheMgr { (await this.get(listKey, CacheGetType.TYPE_ARRAY)) || []; for (const o of list) { // construct key for Get - // e.g. nc::: let getKey = `${this.prefix}:${scope}:${o.id}`; - // special case - MODEL_ROLE_VISIBILITY - if (scope === CacheScope.MODEL_ROLE_VISIBILITY) { - getKey = `${this.prefix}:${scope}:${o.fk_view_id}:${o.role}`; + if (props.length) { + const propValues = props.map((p) => o[p]); + // e.g. nc:::: + getKey = `${this.prefix}:${scope}:${propValues.join(':')}`; + } else { + // e.g. nc::: + getKey = `${this.prefix}:${scope}:${o.id}`; + // special case - MODEL_ROLE_VISIBILITY + if (scope === CacheScope.MODEL_ROLE_VISIBILITY) { + getKey = `${this.prefix}:${scope}:${o.fk_view_id}:${o.role}`; + } } // set Get Key log(`RedisCacheMgr::setList: setting key ${getKey}`); diff --git a/packages/nocodb/src/cache/RedisMockCacheMgr.ts b/packages/nocodb/src/cache/RedisMockCacheMgr.ts index 3381a4f7cb..7295d83aa5 100644 --- a/packages/nocodb/src/cache/RedisMockCacheMgr.ts +++ b/packages/nocodb/src/cache/RedisMockCacheMgr.ts @@ -185,6 +185,7 @@ export default class RedisMockCacheMgr extends CacheMgr { scope: string, subListKeys: string[], list: any[], + props: string[] = [], ): Promise { // remove null from arrays subListKeys = subListKeys.filter((k) => k); @@ -203,11 +204,18 @@ export default class RedisMockCacheMgr extends CacheMgr { (await this.get(listKey, CacheGetType.TYPE_ARRAY)) || []; for (const o of list) { // construct key for Get - // e.g. nc::: let getKey = `${this.prefix}:${scope}:${o.id}`; - // special case - MODEL_ROLE_VISIBILITY - if (scope === CacheScope.MODEL_ROLE_VISIBILITY) { - getKey = `${this.prefix}:${scope}:${o.fk_view_id}:${o.role}`; + if (props.length) { + const propValues = props.map((p) => o[p]); + // e.g. nc:::: + getKey = `${this.prefix}:${scope}:${propValues.join(':')}`; + } else { + // e.g. nc::: + getKey = `${this.prefix}:${scope}:${o.id}`; + // special case - MODEL_ROLE_VISIBILITY + if (scope === CacheScope.MODEL_ROLE_VISIBILITY) { + getKey = `${this.prefix}:${scope}:${o.fk_view_id}:${o.role}`; + } } // set Get Key log(`RedisMockCacheMgr::setList: setting key ${getKey}`);