Browse Source

feat(nocodb): revise cache getList logic

pull/5430/head
Wing-Kam Wong 2 years ago
parent
commit
61e1c3b4b6
  1. 23
      packages/nocodb/src/lib/cache/RedisCacheMgr.ts
  2. 25
      packages/nocodb/src/lib/cache/RedisMockCacheMgr.ts

23
packages/nocodb/src/lib/cache/RedisCacheMgr.ts vendored

@ -44,7 +44,7 @@ export default class RedisCacheMgr extends CacheMgr {
async get(key: string, type: string, config?: any): Promise<any> { async get(key: string, type: string, config?: any): Promise<any> {
log(`RedisCacheMgr::get: getting key ${key} with type ${type}`); log(`RedisCacheMgr::get: getting key ${key} with type ${type}`);
if (type === CacheGetType.TYPE_ARRAY) { if (type === CacheGetType.TYPE_ARRAY) {
return this.client.smembers(key); return await this.client.smembers(key);
} else if (type === CacheGetType.TYPE_OBJECT) { } else if (type === CacheGetType.TYPE_OBJECT) {
const res = await this.client.get(key); const res = await this.client.get(key);
try { try {
@ -114,7 +114,13 @@ export default class RedisCacheMgr extends CacheMgr {
); );
} }
async getList(scope: string, subKeys: string[]): Promise<any[]> { async getList(
scope: string,
subKeys: string[]
): Promise<{
list: any[];
isEmptyList: boolean;
}> {
// remove null from arrays // remove null from arrays
subKeys = subKeys.filter((k) => k); subKeys = subKeys.filter((k) => k);
// e.g. key = nc:<orgs>:<scope>:<project_id_1>:<base_id_1>:list // e.g. key = nc:<orgs>:<scope>:<project_id_1>:<base_id_1>:list
@ -125,10 +131,18 @@ export default class RedisCacheMgr extends CacheMgr {
// e.g. arr = ["nc:<orgs>:<scope>:<model_id_1>", "nc:<orgs>:<scope>:<model_id_2>"] // e.g. arr = ["nc:<orgs>:<scope>:<model_id_1>", "nc:<orgs>:<scope>:<model_id_2>"]
const arr = (await this.get(key, CacheGetType.TYPE_ARRAY)) || []; const arr = (await this.get(key, CacheGetType.TYPE_ARRAY)) || [];
log(`RedisCacheMgr::getList: getting list with key ${key}`); log(`RedisCacheMgr::getList: getting list with key ${key}`);
return Promise.all( let list: any[] = [];
const isEmptyList = arr[0] === 'NONE';
if (!isEmptyList) {
list = await Promise.all(
arr.map(async (k) => await this.get(k, CacheGetType.TYPE_OBJECT)) arr.map(async (k) => await this.get(k, CacheGetType.TYPE_OBJECT))
); );
} }
return {
list,
isEmptyList,
};
}
async setList( async setList(
scope: string, scope: string,
@ -144,8 +158,7 @@ export default class RedisCacheMgr extends CacheMgr {
? `${this.prefix}:${scope}:list` ? `${this.prefix}:${scope}:list`
: `${this.prefix}:${scope}:${subListKeys.join(':')}:list`; : `${this.prefix}:${scope}:${subListKeys.join(':')}:list`;
if (!list.length) { if (!list.length) {
log(`RedisCacheMgr::setList: List is empty for ${listKey}. Skipping ...`); return this.set(listKey, ['NONE']);
return Promise.resolve(true);
} }
// fetch existing list // fetch existing list
const listOfGetKeys = const listOfGetKeys =

25
packages/nocodb/src/lib/cache/RedisMockCacheMgr.ts vendored

@ -43,7 +43,7 @@ export default class RedisMockCacheMgr extends CacheMgr {
async get(key: string, type: string, config?: any): Promise<any> { async get(key: string, type: string, config?: any): Promise<any> {
log(`RedisMockCacheMgr::get: getting key ${key} with type ${type}`); log(`RedisMockCacheMgr::get: getting key ${key} with type ${type}`);
if (type === CacheGetType.TYPE_ARRAY) { if (type === CacheGetType.TYPE_ARRAY) {
return this.client.smembers(key); return await this.client.smembers(key);
} else if (type === CacheGetType.TYPE_OBJECT) { } else if (type === CacheGetType.TYPE_OBJECT) {
const res = await this.client.get(key); const res = await this.client.get(key);
try { try {
@ -114,7 +114,13 @@ export default class RedisMockCacheMgr extends CacheMgr {
); );
} }
async getList(scope: string, subKeys: string[]): Promise<any[]> { async getList(
scope: string,
subKeys: string[]
): Promise<{
list: any[];
isEmptyList: boolean;
}> {
// remove null from arrays // remove null from arrays
subKeys = subKeys.filter((k) => k); subKeys = subKeys.filter((k) => k);
// e.g. key = nc:<orgs>:<scope>:<project_id_1>:<base_id_1>:list // e.g. key = nc:<orgs>:<scope>:<project_id_1>:<base_id_1>:list
@ -125,10 +131,18 @@ export default class RedisMockCacheMgr extends CacheMgr {
// e.g. arr = ["nc:<orgs>:<scope>:<model_id_1>", "nc:<orgs>:<scope>:<model_id_2>"] // e.g. arr = ["nc:<orgs>:<scope>:<model_id_1>", "nc:<orgs>:<scope>:<model_id_2>"]
const arr = (await this.get(key, CacheGetType.TYPE_ARRAY)) || []; const arr = (await this.get(key, CacheGetType.TYPE_ARRAY)) || [];
log(`RedisMockCacheMgr::getList: getting list with key ${key}`); log(`RedisMockCacheMgr::getList: getting list with key ${key}`);
return Promise.all( let list: any[] = [];
const isEmptyList = arr[0] === 'NONE';
if (!isEmptyList) {
list = await Promise.all(
arr.map(async (k) => await this.get(k, CacheGetType.TYPE_OBJECT)) arr.map(async (k) => await this.get(k, CacheGetType.TYPE_OBJECT))
); );
} }
return {
list,
isEmptyList,
};
}
async setList( async setList(
scope: string, scope: string,
@ -144,10 +158,7 @@ export default class RedisMockCacheMgr extends CacheMgr {
? `${this.prefix}:${scope}:list` ? `${this.prefix}:${scope}:list`
: `${this.prefix}:${scope}:${subListKeys.join(':')}:list`; : `${this.prefix}:${scope}:${subListKeys.join(':')}:list`;
if (!list.length) { if (!list.length) {
log( return this.set(listKey, ['NONE']);
`RedisMockCacheMgr::setList: List is empty for ${listKey}. Skipping ...`
);
return Promise.resolve(true);
} }
// fetch existing list // fetch existing list
const listOfGetKeys = const listOfGetKeys =

Loading…
Cancel
Save