perf(pfapi): add cacheOnLoad option for ModelCtrl

Add cacheOnLoad config flag to ModelCfg that enables in-memory caching
after the first IndexedDB load. Previously, the cache was only populated
on save(), causing repeated DB reads for frequently-accessed models.

Enable cacheOnLoad for archiveYoung, which is loaded on every route
change via work-view component's async pipes for displaying archived
time worked and done task counts.
This commit is contained in:
Johannes Millan 2025-12-26 15:43:40 +01:00
parent 266eecd6ff
commit 42b0f4ea1a
3 changed files with 24 additions and 5 deletions

View file

@ -128,11 +128,22 @@ export class ModelCtrl<MT extends ModelBase> {
PFLog.verbose(`${ModelCtrl.L}.${this.load.name}():${this.modelId}`, {
inMemoryData: this._inMemoryData,
});
return (
this._inMemoryData ||
((await this._db.load(this.modelId)) as Promise<MT>) ||
(this.modelCfg.defaultData as MT)
);
// Return cached data if available
if (this._inMemoryData) {
return this._inMemoryData;
}
// Load from database
const dbData = (await this._db.load(this.modelId)) as MT | undefined;
const result = dbData || (this.modelCfg.defaultData as MT);
// Cache if configured
if (this.modelCfg.cacheOnLoad && result) {
this._inMemoryData = result;
}
return result;
}
/**

View file

@ -28,6 +28,13 @@ export interface ModelCfg<T extends ModelBase> {
debounceDbWrite?: number;
isMainFileModel?: boolean;
/**
* When true, ModelCtrl.load() will cache the result from IndexedDB
* in memory for subsequent reads. Useful for frequently-read models.
* Default: false (current behavior - no caching on load).
*/
cacheOnLoad?: boolean;
validate?: <R>(data: R | T) => IValidation<R | T>;
repair?: <R>(data: R | unknown | any) => T;

View file

@ -196,6 +196,7 @@ export const PFAPI_MODEL_CFGS: PfapiAllModelCfg = {
task: fixEntityStateConsistency(d.task),
};
},
cacheOnLoad: true,
},
archiveOld: {
defaultData: {