diff --git a/data.db b/data.db index 5335d8a..bd2481b 100644 Binary files a/data.db and b/data.db differ 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 (