Browse Source

fix: cache store value

pull/7044/head
Pranav C 1 year ago
parent
commit
0cd3b72768
  1. 32
      packages/nocodb/src/models/Store.ts
  2. 13
      packages/nocodb/src/services/utils.service.ts
  3. 1
      packages/nocodb/src/utils/globals.ts

32
packages/nocodb/src/models/Store.ts

@ -2,7 +2,8 @@ import type { SortType } from 'nocodb-sdk';
import { NcError } from '~/helpers/catchError';
import { extractProps } from '~/helpers/extractProps';
import Noco from '~/Noco';
import { MetaTable } from '~/utils/globals';
import { CacheGetType, CacheScope, MetaTable } from '~/utils/globals';
import NocoCache from '~/cache/NocoCache';
// Store is used for storing key value pairs
export default class Store {
@ -18,8 +19,30 @@ export default class Store {
Object.assign(this, data);
}
public static get(key: string, ncMeta = Noco.ncMeta): Promise<Store> {
return ncMeta.metaGet(null, null, MetaTable.STORE, { key });
public static async get(
key: string,
lookInCache = false,
ncMeta = Noco.ncMeta,
): Promise<Store> {
// get from cache if lookInCache is true
if (lookInCache) {
const storeData =
key &&
(await NocoCache.get(
`${CacheScope.STORE}:${key}`,
CacheGetType.TYPE_OBJECT,
));
if (storeData) return storeData;
}
const storeData = await ncMeta.metaGet(null, null, MetaTable.STORE, {
key,
});
if (lookInCache)
await NocoCache.set(`${CacheScope.STORE}:${key}`, storeData);
return storeData;
}
static async saveOrUpdate(store: Store, ncMeta = Noco.ncMeta) {
@ -35,7 +58,7 @@ export default class Store {
'tag',
]);
const existing = await Store.get(store.key, ncMeta);
const existing = await Store.get(store.key,false, ncMeta);
if (existing) {
await ncMeta.metaUpdate(null, null, MetaTable.STORE, insertObj, {
key: store.key,
@ -43,5 +66,6 @@ export default class Store {
} else {
await ncMeta.metaInsert(null, null, MetaTable.STORE, insertObj);
}
if (store.key) await NocoCache.del(`${CacheScope.STORE}:${store.key}`);
}
}

13
packages/nocodb/src/services/utils.service.ts

@ -5,10 +5,10 @@ import { ViewTypes } from 'nocodb-sdk';
import { ConfigService } from '@nestjs/config';
import { useAgent } from 'request-filtering-agent';
import type { AppConfig } from '~/interface/config';
import {NC_APP_SETTINGS, NC_ATTACHMENT_FIELD_SIZE} from '~/constants';
import { NC_APP_SETTINGS, NC_ATTACHMENT_FIELD_SIZE } from '~/constants';
import SqlMgrv2 from '~/db/sql-mgr/v2/SqlMgrv2';
import { NcError } from '~/helpers/catchError';
import {Base, Store, User} from '~/models';
import { Base, Store, User } from '~/models';
import Noco from '~/Noco';
import NcConnectionMgrv2 from '~/utils/common/NcConnectionMgrv2';
import { MetaTable } from '~/utils/globals';
@ -368,10 +368,9 @@ export class UtilsService {
let settings: { invite_only_signup?: boolean } = {};
try {
settings = JSON.parse((await Store.get(NC_APP_SETTINGS))?.value);
settings = JSON.parse((await Store.get(NC_APP_SETTINGS, true))?.value);
} catch {}
const oidcAuthEnabled = !!(
process.env.NC_OIDC_ISSUER &&
process.env.NC_OIDC_AUTHORIZATION_URL &&
@ -420,10 +419,10 @@ export class UtilsService {
disableEmailAuth: this.configService.get('auth.disableEmailAuth', {
infer: true,
}),
mainSubDomain: this.configService.get('mainSubDomain', {infer: true}),
dashboardPath: this.configService.get('dashboardPath', {infer: true}),
mainSubDomain: this.configService.get('mainSubDomain', { infer: true }),
dashboardPath: this.configService.get('dashboardPath', { infer: true }),
inviteOnlySignup: settings.invite_only_signup,
}
};
return result;
}

1
packages/nocodb/src/utils/globals.ts

@ -158,6 +158,7 @@ export enum CacheScope {
SINGLE_QUERY = 'singleQuery',
JOBS = 'nc_jobs',
PRESIGNED_URL = 'presignedUrl',
STORE = 'presignedUrl',
}
export enum CacheGetType {

Loading…
Cancel
Save