Berechnungs-Cache eingebaut, um Anzeige zu beschleunigen. Standard-Wert für Cache ist 6 Stunden und ist In-Memory
All checks were successful
release-tag / release-image (push) Successful in 2m18s
All checks were successful
release-tag / release-image (push) Successful in 2m18s
This commit is contained in:
85
main.go
85
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 (
|
||||
|
Reference in New Issue
Block a user