From 41cee8af1d0e026ab87fc5361c81aaff35e2915a Mon Sep 17 00:00:00 2001 From: jbergner Date: Thu, 31 Jul 2025 22:32:19 +0200 Subject: [PATCH] =?UTF-8?q?Berechnungs-Cache=20eingebaut,=20um=20Anzeige?= =?UTF-8?q?=20zu=20beschleunigen.=20Standard-Wert=20f=C3=BCr=20Cache=20ist?= =?UTF-8?q?=206=20Stunden=20und=20ist=20In-Memory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data.db | Bin 12288 -> 12288 bytes main.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/data.db b/data.db index 5335d8a27f67d8019b06ce2bd4fc89ade7779f38..bd2481b892ad55ba9696004a9d50ef5909970162 100644 GIT binary patch delta 64 zcmZojXh@hK&6qh+#+fm5W5PmyUOsjPX5PyT{C<2Zc`t9~6FAGu$T|6rtTqb+1H+=p U+;Yo-+|6=J7&$ga$gdCp09vgQTmS$7 delta 64 zcmZojXh@hK&6qk-#+fm7W5PmyUIqpR7T(JY{C<2Z`Pet}37q9+0dceC6QM$XL<@+$-YJs}Z; diff --git a/main.go b/main.go index bae657c..2258a50 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "sort" "strconv" "strings" + "sync" "time" _ "modernc.org/sqlite" // statt github.com/mattn/go-sqlite3 @@ -285,6 +286,11 @@ func main() { if id != "" { db.Exec("DELETE FROM eintraege WHERE id = ?", id) } + + cacheMutex.Lock() + cache.LastComputed = time.Time{} // auf null zurücksetzen + cacheMutex.Unlock() + http.Redirect(w, r, "/", http.StatusSeeOther) }) @@ -298,6 +304,11 @@ func main() { // Auto-Increment-Zähler zurücksetzen db.Exec("DELETE FROM sqlite_sequence WHERE name='eintraege'") + + cacheMutex.Lock() + cache.LastComputed = time.Time{} // auf null zurücksetzen + cacheMutex.Unlock() + http.Redirect(w, r, "/", http.StatusSeeOther) }) @@ -311,6 +322,11 @@ func main() { if id != "" { db.Exec("UPDATE eintraege SET bezahlt = 1 WHERE id = ?", id) } + + cacheMutex.Lock() + cache.LastComputed = time.Time{} // auf null zurücksetzen + cacheMutex.Unlock() + http.Redirect(w, r, "/", http.StatusSeeOther) }) @@ -324,6 +340,11 @@ func main() { if id != "" { db.Exec("UPDATE eintraege SET bezahlt = 0 WHERE id = ?", id) } + + cacheMutex.Lock() + cache.LastComputed = time.Time{} // auf null zurücksetzen + cacheMutex.Unlock() + http.Redirect(w, r, "/", http.StatusSeeOther) }) @@ -351,10 +372,25 @@ func main() { http.Error(w, "Fehler beim Einfügen", http.StatusInternalServerError) return } + + cacheMutex.Lock() + cache.LastComputed = time.Time{} // auf null zurücksetzen + cacheMutex.Unlock() + http.Redirect(w, r, "/", http.StatusSeeOther) return } + cacheMutex.RLock() + validCache := time.Since(cache.LastComputed) < 6*time.Hour + cachedData := cache.Data + cacheMutex.RUnlock() + + if validCache { + tmpl.Execute(w, cachedData) + return + } + rows, err := db.Query(`SELECT id, anfangsbestand, endbestand, prozentwert, abgabe, bezahlt, created_at, startort, zielort, schiff, ware, zeitaufwand FROM eintraege`) if err != nil { http.Error(w, "Fehler beim Abrufen", http.StatusInternalServerError) @@ -487,7 +523,29 @@ func main() { abteilungen[i].WertOffen = (abteilungen[i].Anteil / 100) * offeneSumme } - tmpl.Execute(w, struct { + computed := TemplateData{ + Entries: eintraege, + Summe: summe, + OffeneSumme: offeneSumme, + Abteilungen: abteilungen, + Monatsstatistik: monatsStat, + LoggedIn: isAuthenticated(r), + Member: membername, + HasImpressum: hasimpressum, + Impressum: impressum, + Orte: orte, + Schiffe: schiffe, + Waren: waren, + } + + cacheMutex.Lock() + cache.Data = computed + cache.LastComputed = time.Now() + cacheMutex.Unlock() + + tmpl.Execute(w, computed) + + /*tmpl.Execute(w, struct { Entries []Entry Summe float64 OffeneSumme float64 @@ -513,7 +571,7 @@ func main() { Orte: orte, Schiffe: schiffe, Waren: waren, - }) + })*/ }) @@ -521,6 +579,29 @@ func main() { http.ListenAndServe(":8080", nil) } +type TemplateData struct { + Entries []Entry + Summe float64 + OffeneSumme float64 + Abteilungen []Abteilung + Monatsstatistik []Monatsstatistik + LoggedIn bool + Member string + HasImpressum bool + Impressum string + Orte []string + Schiffe []string + Waren []string +} + +type CachedData struct { + Data TemplateData // das Struct, das du an tmpl.Execute übergibst + LastComputed time.Time +} + +var cache CachedData +var cacheMutex sync.RWMutex + func createTable(db *sql.DB) { _, err := db.Exec(` CREATE TABLE IF NOT EXISTS eintraege (