Skip to content

Commit

Permalink
📈 perf(cache): rEAD ALL CONFIG THROUGH CACHE!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
thrownullexception committed Dec 11, 2023
1 parent 9e43fe8 commit 9e48cc0
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 33 deletions.
9 changes: 4 additions & 5 deletions src/backend/lib/cache/AbstractCacheService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isNotEmpty } from "class-validator";
import { ConfigApiService } from "../config/config.service";

export abstract class AbstractCacheService {
Expand All @@ -10,8 +9,8 @@ export abstract class AbstractCacheService {

public abstract setup(): Promise<void>;

protected prefixKey(key: string) {
return `__dashpress__:${key}`;
private prefixKey(key: string) {
return `__dp__:${key}`;
}

abstract pullItem<T>(key: string): Promise<T | undefined>;
Expand All @@ -27,13 +26,13 @@ export abstract class AbstractCacheService {

const data = await this.pullItem<T>(key);

if (isNotEmpty(data)) {
if (data !== undefined) {
return data;
}

const fetchedData = await fetcher();

await this.persistData(key, fetchedData);
await this.persistData(key, fetchedData || null);

return fetchedData;
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend/lib/cache/MemoryCacheAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class MemoryCacheAdaptor extends AbstractCacheService {
}

async clearItem(key: string) {
delete this.getData()[this.prefixKey(key)];
delete this.getData()[key];
}

async purge() {
Expand Down
2 changes: 1 addition & 1 deletion src/backend/lib/cache/RedisCacheAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class RedisCacheAdaptor extends AbstractCacheService {
}

async clearItem(key: string) {
await (await this.getRedisInstance()).del(this.prefixKey(key));
await (await this.getRedisInstance()).del(key);
}

async purge() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { createCacheService } from "../cache";
import { ConfigApiService } from "../config/config.service";
import { NotFoundError } from "../errors";
import { ConfigDomain } from "./types";

const cacheService = createCacheService();

export abstract class AbstractConfigDataPersistenceService<T> {
protected readonly _configDomain!: ConfigDomain;

Expand All @@ -21,7 +24,27 @@ export abstract class AbstractConfigDataPersistenceService<T> {
return `${key}__${secondaryKey}`;
}

public abstract getItem(key: string): Promise<T | undefined>;
public abstract _getItem(key: string): Promise<T | undefined>; //

public abstract _persistItem(key: string, data: T): Promise<void>; //

public abstract _removeItem(key: string): Promise<void>; //

public async getItem(key: string) {
return await cacheService.getItem(key, async () => {
return this._getItem(key);
});
}

public async persistItem(key: string, data: T) {
await cacheService.clearItem(key);
await this._persistItem(key, data);
}

public async removeItem(key: string) {
await cacheService.clearItem(key);
await this._removeItem(key);
}

public abstract getItemLastUpdated(key: string): Promise<Date>;

Expand All @@ -39,8 +62,6 @@ export abstract class AbstractConfigDataPersistenceService<T> {

public abstract getAllItems(): Promise<T[]>;

public abstract persistItem(key: string, data: T): Promise<void>;

public async upsertItem(key: string, data: T): Promise<void> {
await this.persistItem(key, data);
}
Expand All @@ -49,8 +70,6 @@ export abstract class AbstractConfigDataPersistenceService<T> {
await this.persistItem(key, data);
}

public abstract removeItem(key: string): Promise<void>; //

public async resetState(keyField: keyof T, data: T[]) {
await this.resetToEmpty();
await this.saveAllItems(keyField, data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class DatabaseConfigDataPersistenceAdaptor<
);
}

async getItem(key: string) {
async _getItem(key: string) {
const connection = await this.getDbInstance();
const queryResponse = await connection
.table(CONFIG_TABLE_NAME)
Expand Down Expand Up @@ -129,7 +129,7 @@ export class DatabaseConfigDataPersistenceAdaptor<
}
}

async persistItem(key: string, value: T) {
async _persistItem(key: string, value: T) {
const affectedRowsCount = await (
await this.getDbInstance()
)(CONFIG_TABLE_NAME)
Expand All @@ -152,7 +152,7 @@ export class DatabaseConfigDataPersistenceAdaptor<
}
}

async removeItem(key: string): Promise<void> {
async _removeItem(key: string): Promise<void> {
await (
await this.getDbInstance()
)(CONFIG_TABLE_NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class JsonFileConfigDataPersistenceAdaptor<
);
}

async getItem(key: string) {
async _getItem(key: string) {
const allIndexedItems = await this.getDomainData();
const currentItem = allIndexedItems[key];
if (currentItem) {
Expand All @@ -80,13 +80,13 @@ export class JsonFileConfigDataPersistenceAdaptor<
return undefined;
}

async persistItem(key: string, data: T) {
async _persistItem(key: string, data: T) {
const allIndexedItems = await this.getDomainData();
allIndexedItems[key] = data;
await this.persist(allIndexedItems);
}

async removeItem(key: string): Promise<void> {
async _removeItem(key: string): Promise<void> {
const allIndexedItems = await this.getDomainData();
delete allIndexedItems[key];
await this.persist(allIndexedItems);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class MemoryConfigDataPersistenceAdaptor<
);
}

async getItem(key: string) {
async _getItem(key: string) {
const currentItem = MemoryConfigDataPersistenceAdaptor.getDomainData(
this._configDomain
)[key];
Expand All @@ -66,15 +66,15 @@ export class MemoryConfigDataPersistenceAdaptor<
return undefined;
}

async persistItem(key: string, data: T) {
async _persistItem(key: string, data: T) {
const domainData = MemoryConfigDataPersistenceAdaptor.getDomainData(
this._configDomain
);
domainData[key] = data;
this.persistDomainData(domainData);
}

public async removeItem(key: string): Promise<void> {
async _removeItem(key: string): Promise<void> {
const domainData = MemoryConfigDataPersistenceAdaptor.getDomainData(
this._configDomain
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,21 @@ export class RedisConfigDataPersistenceAdaptor<
);
}

async getItem(key: string) {
async _getItem(key: string) {
return JSON.parse(
await (
await this.getRedisInstance()
).hGet(this.wrapWithConfigDomain(), key)
);
}

async persistItem(key: string, data: T) {
async _persistItem(key: string, data: T) {
await (
await this.getRedisInstance()
).hSet(this.wrapWithConfigDomain(), { [key]: JSON.stringify(data) });
}

public async removeItem(key: string): Promise<void> {
async _removeItem(key: string): Promise<void> {
await (
await this.getRedisInstance()
).hDel(this.wrapWithConfigDomain(), key);
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/lib/storage/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sluggify } from "shared/lib/strings";
import { StorageService } from ".";

const PREFIX = "_app_config__";
const PREFIX = "__dp__";

const makeKey = (key: string): string => {
return sluggify(`${PREFIX}${key}`, "_");
Expand Down
4 changes: 3 additions & 1 deletion src/frontend/views/data/Details/RelationsDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ export function EntityRelationDetails() {
<SectionBox
title={title}
isLoading={
entityDataReference.isLoading || entityDataReference.isIdle
entityDataReference.isLoading ||
entityDataReference.isIdle ||
dataDetails.isLoading
}
backLink={backLink}
actionButtons={actionButtons}
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/views/data/Details/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function EntityDetails() {
entity,
id,
redirectAfterDelete: NAVIGATION_LINKS.ENTITY.TABLE(entity),
exclude: ["table"],
exclude: ["details"],
});

const portalActionButtons = usePortalActionButtons({
Expand Down
7 changes: 1 addition & 6 deletions src/frontend/views/data/Table/useTableColumns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,9 @@ const buildFilterConfigFromType = (prop: {
entityFieldSelections: IColorableSelection[];
isIdField: boolean;
referenceField?: string;
lean?: true; // TODO is it in use?
}): TableFilterType | undefined => {
const { entityType, entityFieldSelections, isIdField, referenceField, lean } =
prop;
const { entityType, entityFieldSelections, isIdField, referenceField } = prop;

if (lean) {
return undefined;
}
if (isIdField) {
return {
_type: "idField",
Expand Down

0 comments on commit 9e48cc0

Please sign in to comment.