Skip to content

Commit

Permalink
write cache only when changes were made in tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Heiss committed Aug 1, 2024
1 parent feacf50 commit 03ffba9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/settings/VaultTaskCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ import {PluginTask} from "../vaultSearcher/vaultSearcher";
export default class VaultTaskCache {
plugin: VikunjaPlugin;
app: App;
changesMade: boolean;

constructor(app: App, plugin: VikunjaPlugin) {
this.app = app;
this.plugin = plugin;
this.changesMade = false;
}

async saveCacheToDisk() {
await this.plugin.saveSettings();
if (this.changesMade) {
await this.plugin.saveSettings();
}
this.changesMade = false;
}

update(local: PluginTask) {
Expand All @@ -29,6 +34,7 @@ export default class VaultTaskCache {
local.task.updated = currentDate;

this.plugin.settings.cache.set(local.task.id, local);
this.changesMade = true;
}

get(id: number): PluginTask | undefined {
Expand All @@ -40,6 +46,7 @@ export default class VaultTaskCache {
*/
delete(id: number) {
this.plugin.settings.cache.delete(id);
this.changesMade = true;
}

/*
Expand All @@ -51,5 +58,6 @@ export default class VaultTaskCache {

reset() {
this.plugin.settings.cache.clear();
this.changesMade = true;
}
}
14 changes: 11 additions & 3 deletions src/settings/mainSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const DEFAULT_SETTINGS: VikunjaPluginSettings = {
selectedView: 0,
selectBucketForDoneTasks: 0,
cache: new Map<number, PluginTask>(),
saveCacheToDiskFrequency: 10,
saveCacheToDiskFrequency: 1,
}

export class MainSetting extends PluginSettingTab {
Expand Down Expand Up @@ -199,11 +199,19 @@ export class MainSetting extends PluginSettingTab {

new Setting(containerEl)
.setName("Save cache to disk frequency")
.setDesc("Set the interval in minutes to save the cache to disk. Lower values will result in more frequent saves, but may cause performance issues. Limits are 1 to 60. This will be enforced by the plugin.")
.setDesc("This plugin uses a cache to calculate correct dates. Set the interval in minutes to save the cache to disk. Lower values will result in more frequent saves, but may cause performance issues. Set too high, task dates are not correctly calculated, because they are missing in cache in next startup. If you make bulk edits of tasks in your vault, you should set higher value. Cache will be only written, if changes were made since last check. If you are unsure, try lowest value and increase it, if you experience performance issues. Limits are 1 to 60 minutes.")
.addText(text => text
.setValue(this.plugin.settings.saveCacheToDiskFrequency.toString())
.onChange(async (value: string) => {
this.plugin.settings.saveCacheToDiskFrequency = Math.max(Math.min(parseInt(value), 60), 1);
const parsedNumber = parseInt(value);
if (isNaN(parsedNumber)) {
return;
}
const lowerThanMax = Math.min(parsedNumber, 60);
if (this.plugin.settings.debugging) console.log("Save cache to disk frequency - high limits", lowerThanMax);
const higherThanMin = Math.max(lowerThanMax, 1);
if (this.plugin.settings.debugging) console.log("Save cache to disk frequency - low limits", higherThanMin);
this.plugin.settings.saveCacheToDiskFrequency = higherThanMin;
await this.plugin.saveSettings();
this.startCacheListener();
}
Expand Down

0 comments on commit 03ffba9

Please sign in to comment.