Browse Source

fix(nocodb): cache logic

pull/5430/head
Wing-Kam Wong 2 years ago
parent
commit
3df9b42a9a
  1. 23
      packages/nocodb/src/lib/cache/RedisCacheMgr.ts
  2. 30
      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 await this.client.smembers(key); return 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 {
@ -131,15 +131,20 @@ 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}`);
let list: any[] = [];
const isEmptyList = arr[0] === 'NONE'; const isEmptyList = arr.length && arr[0] === 'NONE';
if (!isEmptyList) {
list = await Promise.all( if (isEmptyList) {
arr.map(async (k) => await this.get(k, CacheGetType.TYPE_OBJECT)) return Promise.resolve({
); list: [],
isEmptyList,
});
} }
return { return {
list, list: await Promise.all(
arr.map(async (k) => await this.get(k, CacheGetType.TYPE_OBJECT))
),
isEmptyList, isEmptyList,
}; };
} }
@ -248,7 +253,7 @@ export default class RedisCacheMgr extends CacheMgr {
: `${this.prefix}:${scope}:${subListKeys.join(':')}:list`; : `${this.prefix}:${scope}:${subListKeys.join(':')}:list`;
log(`RedisCacheMgr::appendToList: append key ${key} to ${listKey}`); log(`RedisCacheMgr::appendToList: append key ${key} to ${listKey}`);
let list = (await this.get(listKey, CacheGetType.TYPE_ARRAY)) || []; let list = (await this.get(listKey, CacheGetType.TYPE_ARRAY)) || [];
if (list.length === 1 && list[0] === 'NONE') { if (list.length && list[0] === 'NONE') {
list = []; list = [];
await this.del(listKey); await this.del(listKey);
} }

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

@ -2,6 +2,7 @@ import debug from 'debug';
import Redis from 'ioredis-mock'; import Redis from 'ioredis-mock';
import { CacheDelDirection, CacheGetType, CacheScope } from '../utils/globals'; import { CacheDelDirection, CacheGetType, CacheScope } from '../utils/globals';
import CacheMgr from './CacheMgr'; import CacheMgr from './CacheMgr';
const log = debug('nc:cache'); const log = debug('nc:cache');
export default class RedisMockCacheMgr extends CacheMgr { export default class RedisMockCacheMgr extends CacheMgr {
@ -43,7 +44,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 await this.client.smembers(key); return 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 {
@ -102,10 +103,9 @@ export default class RedisMockCacheMgr extends CacheMgr {
`RedisMockCacheMgr::delAll: deleting all keys with pattern ${this.prefix}:${scope}:${pattern}` `RedisMockCacheMgr::delAll: deleting all keys with pattern ${this.prefix}:${scope}:${pattern}`
); );
await Promise.all( await Promise.all(
keys.map( keys.map(async (k) => {
async (k) => await this.deepDel(scope, k, CacheDelDirection.CHILD_TO_PARENT);
await this.deepDel(scope, k, CacheDelDirection.CHILD_TO_PARENT) })
)
); );
return Promise.all( return Promise.all(
keys.map(async (k) => { keys.map(async (k) => {
@ -131,15 +131,19 @@ 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}`);
let list: any[] = []; const isEmptyList = arr.length && arr[0] === 'NONE';
const isEmptyList = arr[0] === 'NONE';
if (!isEmptyList) { if (isEmptyList) {
list = await Promise.all( return Promise.resolve({
arr.map(async (k) => await this.get(k, CacheGetType.TYPE_OBJECT)) list: [],
); isEmptyList,
});
} }
return { return {
list, list: await Promise.all(
arr.map(async (k) => await this.get(k, CacheGetType.TYPE_OBJECT))
),
isEmptyList, isEmptyList,
}; };
} }
@ -248,7 +252,7 @@ export default class RedisMockCacheMgr extends CacheMgr {
: `${this.prefix}:${scope}:${subListKeys.join(':')}:list`; : `${this.prefix}:${scope}:${subListKeys.join(':')}:list`;
log(`RedisMockCacheMgr::appendToList: append key ${key} to ${listKey}`); log(`RedisMockCacheMgr::appendToList: append key ${key} to ${listKey}`);
let list = (await this.get(listKey, CacheGetType.TYPE_ARRAY)) || []; let list = (await this.get(listKey, CacheGetType.TYPE_ARRAY)) || [];
if (list.length === 1 && list[0] === 'NONE') { if (list.length && list[0] === 'NONE') {
list = []; list = [];
await this.del(listKey); await this.del(listKey);
} }

Loading…
Cancel
Save