From 8211a4cef58b808c94ea396d4542b14a4b2b3722 Mon Sep 17 00:00:00 2001 From: Alexander Huck Date: Mon, 16 Oct 2023 18:26:42 +0200 Subject: [PATCH 1/5] feat: load decrypted storage file into memory so we don't need to decrypt multiple times --- internal/auth/yaml_storage.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/internal/auth/yaml_storage.go b/internal/auth/yaml_storage.go index 30a6fd3..4bd532f 100644 --- a/internal/auth/yaml_storage.go +++ b/internal/auth/yaml_storage.go @@ -8,6 +8,7 @@ import ( "io/fs" "os" + "github.com/charmbracelet/log" "github.com/inovex/CalendarSync/internal/config" "gopkg.in/yaml.v3" ) @@ -15,11 +16,22 @@ import ( type YamlStorage struct { StoragePath string StorageEncryptionKey string + // Holds the decrypted CalendarAuth Config in memory, so the file does not have to be read multiple times + DecryptedAuth []CalendarAuth } func (y *YamlStorage) Setup(config config.AuthStorage, encryptionPassphrase string) error { y.StorageEncryptionKey = encryptionPassphrase y.StoragePath = config.Config["path"].(string) + + log.Debug("Loading saved auth data into memory") + stor, err := y.readAndParseFile() + if errors.Is(err, os.ErrNotExist) { + log.Debug("No storage file found, skipping loading from memory") + } else { + // Put the data into DecryptedAuth + y.DecryptedAuth = stor.Calendars + } return nil } @@ -47,10 +59,25 @@ func (y *YamlStorage) WriteCalendarAuth(newCal CalendarAuth) (bool, error) { return false, err } + // Adding freshly written CalendarAuth to memory + // Probably unneeded, the next time this data will be retrieved is on the next calendarsync run + log.Debugf("Adding calendar auth for cal %s to memory", newCal.CalendarID) + y.DecryptedAuth = append(y.DecryptedAuth, newCal) + return true, nil } func (y *YamlStorage) ReadCalendarAuth(calendarID string) (*CalendarAuth, error) { + // if we already decrypted the file, read from memory + if len(y.DecryptedAuth) > 0 { + for _, cal := range y.DecryptedAuth { + if cal.CalendarID == calendarID { + log.Debug("loaded auth data from memory", "calendarID", cal.CalendarID) + return &cal, nil + } + } + } + file, err := y.readAndParseFile() if err != nil { return nil, ignoreNoFile(err) From c72ac3ab0dc7bf54824efb0816bea21802d88d94 Mon Sep 17 00:00:00 2001 From: Alexander Huck Date: Fri, 27 Oct 2023 12:27:39 +0200 Subject: [PATCH 2/5] fix: rename DecryptedAuth to CachedAuth --- internal/auth/yaml_storage.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/auth/yaml_storage.go b/internal/auth/yaml_storage.go index 4bd532f..9a728b7 100644 --- a/internal/auth/yaml_storage.go +++ b/internal/auth/yaml_storage.go @@ -17,7 +17,7 @@ type YamlStorage struct { StoragePath string StorageEncryptionKey string // Holds the decrypted CalendarAuth Config in memory, so the file does not have to be read multiple times - DecryptedAuth []CalendarAuth + CachedAuth []CalendarAuth } func (y *YamlStorage) Setup(config config.AuthStorage, encryptionPassphrase string) error { From 3cb767c5b1689587df0282efa4140f51eab5c2a0 Mon Sep 17 00:00:00 2001 From: Alexander Huck Date: Fri, 27 Oct 2023 12:27:59 +0200 Subject: [PATCH 3/5] fix: lazy load auth data --- internal/auth/yaml_storage.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/internal/auth/yaml_storage.go b/internal/auth/yaml_storage.go index 9a728b7..968675a 100644 --- a/internal/auth/yaml_storage.go +++ b/internal/auth/yaml_storage.go @@ -23,15 +23,6 @@ type YamlStorage struct { func (y *YamlStorage) Setup(config config.AuthStorage, encryptionPassphrase string) error { y.StorageEncryptionKey = encryptionPassphrase y.StoragePath = config.Config["path"].(string) - - log.Debug("Loading saved auth data into memory") - stor, err := y.readAndParseFile() - if errors.Is(err, os.ErrNotExist) { - log.Debug("No storage file found, skipping loading from memory") - } else { - // Put the data into DecryptedAuth - y.DecryptedAuth = stor.Calendars - } return nil } From 3ca63fad5e1af4280add91732fbf6b304b02f6de Mon Sep 17 00:00:00 2001 From: Alexander Huck Date: Fri, 27 Oct 2023 12:34:47 +0200 Subject: [PATCH 4/5] fix: simplify cachedauth data write --- internal/auth/yaml_storage.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/auth/yaml_storage.go b/internal/auth/yaml_storage.go index 968675a..bc9ca6c 100644 --- a/internal/auth/yaml_storage.go +++ b/internal/auth/yaml_storage.go @@ -53,7 +53,7 @@ func (y *YamlStorage) WriteCalendarAuth(newCal CalendarAuth) (bool, error) { // Adding freshly written CalendarAuth to memory // Probably unneeded, the next time this data will be retrieved is on the next calendarsync run log.Debugf("Adding calendar auth for cal %s to memory", newCal.CalendarID) - y.DecryptedAuth = append(y.DecryptedAuth, newCal) + y.CachedAuth = cals return true, nil } From 695695dc3005313c90115f6492128747cba21053 Mon Sep 17 00:00:00 2001 From: Alexander Huck Date: Fri, 27 Oct 2023 12:36:44 +0200 Subject: [PATCH 5/5] fix: no need to duplicate id filtering --- internal/auth/yaml_storage.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/internal/auth/yaml_storage.go b/internal/auth/yaml_storage.go index bc9ca6c..1304e92 100644 --- a/internal/auth/yaml_storage.go +++ b/internal/auth/yaml_storage.go @@ -59,22 +59,24 @@ func (y *YamlStorage) WriteCalendarAuth(newCal CalendarAuth) (bool, error) { } func (y *YamlStorage) ReadCalendarAuth(calendarID string) (*CalendarAuth, error) { + var calendars []CalendarAuth // if we already decrypted the file, read from memory - if len(y.DecryptedAuth) > 0 { - for _, cal := range y.DecryptedAuth { - if cal.CalendarID == calendarID { - log.Debug("loaded auth data from memory", "calendarID", cal.CalendarID) - return &cal, nil - } + if len(y.CachedAuth) > 0 { + log.Debug("Loading Auth Data from memory") + calendars = y.CachedAuth + } else { + log.Debug("Loading Auth Data from file") + file, err := y.readAndParseFile() + if err != nil { + return nil, ignoreNoFile(err) } + // Load data from file into cache + y.CachedAuth = file.Calendars + // use cached data from now on + calendars = y.CachedAuth } - file, err := y.readAndParseFile() - if err != nil { - return nil, ignoreNoFile(err) - } - - for _, cal := range file.Calendars { + for _, cal := range calendars { if cal.CalendarID == calendarID { return &cal, nil }