Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
repository has now first level cache
zadam5c0355718f9b
diff --git a/package.json b/package.jsonindex d2b2f0d4da..40ce85d31d 100644--- a/package.json+++ b/package.json@@ -50,7 +50,7 @@"imagemin-pngquant": "8.0.0","ini": "1.3.5","is-svg": "4.2.1",- "jimp": "0.10.0",+ "jimp": "0.10.1","mime-types": "2.1.26","multer": "1.4.2","node-abi": "2.15.0",@@ -77,7 +77,7 @@"yazl": "^2.5.1"},"devDependencies": {- "electron": "9.0.0-beta.13",+ "electron": "9.0.0-beta.14","electron-builder": "22.4.1","electron-packager": "14.2.1","electron-rebuild": "1.10.1",diff --git a/src/entities/attribute.js b/src/entities/attribute.jsindex 411ed704bb..b401fd27e1 100644--- a/src/entities/attribute.js+++ b/src/entities/attribute.js@@ -45,11 +45,7 @@ class Attribute extends Entity {* @returns {Promise<Note|null>}*/async getNote() {- if (!this.__note) {- this.__note = await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]);- }-- return this.__note;+ return await repository.getNote(this.noteId);}/**@@ -64,11 +60,7 @@ class Attribute extends Entity {return null;}- if (!this.__targetNote) {- this.__targetNote = await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.value]);- }-- return this.__targetNote;+ return await repository.getNote(this.value);}/**diff --git a/src/entities/branch.js b/src/entities/branch.jsindex 95edccdb54..7950526dcc 100644--- a/src/entities/branch.js+++ b/src/entities/branch.js@@ -28,9 +28,14 @@ class Branch extends Entity {// notePosition is not part of hash because it would produce a lot of updates in case of reorderingstatic get hashedProperties() { return ["branchId", "noteId", "parentNoteId", "isDeleted", "deleteId", "prefix"]; }- /** @returns {Note|null} */+ /** @returns {Promise<Note|null>} */async getNote() {- return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]);+ return await repository.getNote(this.noteId);+ }++ /** @returns {Promise<Note|null>} */+ async getParentNote() {+ return await repository.getNote(this.parentNoteId);}async beforeSaving() {diff --git a/src/entities/note_revision.js b/src/entities/note_revision.jsindex 3220107f19..488e258859 100644--- a/src/entities/note_revision.js+++ b/src/entities/note_revision.js@@ -48,7 +48,7 @@ class NoteRevision extends Entity {}async getNote() {- return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]);+ return await repository.getNote(this.noteId);}/** @returns {boolean} true if the note has string content (not binary) */diff --git a/src/services/cls.js b/src/services/cls.jsindex 44201d951f..d0d3c8a170 100644--- a/src/services/cls.js+++ b/src/services/cls.js@@ -37,6 +37,14 @@ function reset() {clsHooked.reset();}+function getEntityFromCache(entityName, entityId) {+ return namespace.get(entityName + '-' + entityId);+}++function setEntityToCache(entityName, entityId, entity) {+ return namespace.set(entityName + '-' + entityId, entity);+}+module.exports = {init,wrap,@@ -46,5 +54,7 @@ module.exports = {isEntityEventsDisabled,reset,getSyncRows,- addSyncRow+ addSyncRow,+ getEntityFromCache,+ setEntityToCache};\ No newline at end of filediff --git a/src/services/notes.js b/src/services/notes.jsindex 4a46887c82..10c113a13e 100644--- a/src/services/notes.js+++ b/src/services/notes.js@@ -544,16 +544,22 @@ async function deleteBranch(branch, deleteId, taskContext) {note.deleteId = deleteId;await note.save();+ console.log("Deleting note", note.noteId);+for (const attribute of await note.getOwnedAttributes()) {attribute.isDeleted = true;attribute.deleteId = deleteId;await attribute.save();++ console.log("Deleting note's", note.noteId, "attribute", attribute.attributeId);}for (const relation of await note.getTargetRelations()) {relation.isDeleted = true;relation.deleteId = deleteId;await relation.save();++ console.log("Deleting note's", note.noteId, "target relation", relation.attributeId);}return true;diff --git a/src/services/repository.js b/src/services/repository.jsindex 98effc1a61..75fba38c5e 100644--- a/src/services/repository.js+++ b/src/services/repository.js@@ -37,9 +37,21 @@ async function getEntity(query, params = []) {return entityConstructor.createEntityFromRow(row);}+async function getCachedEntity(entityName, entityId, query) {+ let entity = cls.getEntityFromCache(entityName, entityId);++ if (!entity) {+ entity = await getEntity(query, [entityId]);++ cls.setEntityToCache(entityName, entityId, entity);+ }++ return entity;+}+/** @returns {Promise<Note|null>} */async function getNote(noteId) {- return await getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]);+ return await getCachedEntity('notes', noteId, "SELECT * FROM notes WHERE noteId = ?");}/** @returns {Promise<Note[]>} */@@ -59,17 +71,17 @@ async function getNotes(noteIds) {/** @returns {Promise<NoteRevision|null>} */async function getNoteRevision(noteRevisionId) {- return await getEntity("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [noteRevisionId]);+ return await getCachedEntity('note_revisions', noteRevisionId, "SELECT * FROM note_revisions WHERE noteRevisionId = ?");}/** @returns {Promise<Branch|null>} */async function getBranch(branchId) {- return await getEntity("SELECT * FROM branches WHERE branchId = ?", [branchId]);+ return await getCachedEntity('branches', branchId, "SELECT * FROM branches WHERE branchId = ?", [branchId]);}/** @returns {Promise<Attribute|null>} */async function getAttribute(attributeId) {- return await getEntity("SELECT * FROM attributes WHERE attributeId = ?", [attributeId]);+ return await getCachedEntity('attributes', attributeId, "SELECT * FROM attributes WHERE attributeId = ?");}/** @returns {Promise<Option|null>} */