mirror of
https://github.com/johannesjo/super-productivity.git
synced 2026-01-23 02:36:05 +00:00
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:
parent
266eecd6ff
commit
42b0f4ea1a
3 changed files with 24 additions and 5 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -196,6 +196,7 @@ export const PFAPI_MODEL_CFGS: PfapiAllModelCfg = {
|
|||
task: fixEntityStateConsistency(d.task),
|
||||
};
|
||||
},
|
||||
cacheOnLoad: true,
|
||||
},
|
||||
archiveOld: {
|
||||
defaultData: {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue