mirror of
https://github.com/syncthing/syncthing.git
synced 2024-11-17 10:58:49 -07:00
Cache template result, not report
This commit is contained in:
parent
ed240145c7
commit
e38fcbb566
52
main.go
52
main.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -183,18 +184,37 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
cacheData []byte
|
||||||
|
cacheTime time.Time
|
||||||
|
cacheMut sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
|
const maxCacheTime = 5 * 60 * time.Second
|
||||||
|
|
||||||
func rootHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
|
func rootHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
|
||||||
if r.URL.Path == "/" || r.URL.Path == "/index.html" {
|
if r.URL.Path == "/" || r.URL.Path == "/index.html" {
|
||||||
k := timestamp()
|
cacheMut.Lock()
|
||||||
rep := getReport(db, k)
|
defer cacheMut.Unlock()
|
||||||
|
|
||||||
|
if time.Since(cacheTime) > maxCacheTime {
|
||||||
|
rep := getReport(db)
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
err := tpl.Execute(buf, rep)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, "Template Error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cacheData = buf.Bytes()
|
||||||
|
cacheTime = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
err := tpl.Execute(w, rep)
|
w.Write(cacheData)
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
http.Error(w, "Not found", 404)
|
http.Error(w, "Not found", 404)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,17 +254,7 @@ type category struct {
|
|||||||
Binary bool
|
Binary bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var reportCache map[string]interface{}
|
func getReport(db *sql.DB) map[string]interface{} {
|
||||||
var reportMutex sync.Mutex
|
|
||||||
|
|
||||||
func getReport(db *sql.DB, key string) map[string]interface{} {
|
|
||||||
reportMutex.Lock()
|
|
||||||
defer reportMutex.Unlock()
|
|
||||||
|
|
||||||
if k := reportCache["key"]; k == key {
|
|
||||||
return reportCache
|
|
||||||
}
|
|
||||||
|
|
||||||
nodes := 0
|
nodes := 0
|
||||||
var versions []string
|
var versions []string
|
||||||
var platforms []string
|
var platforms []string
|
||||||
@ -370,15 +380,12 @@ func getReport(db *sql.DB, key string) map[string]interface{} {
|
|||||||
})
|
})
|
||||||
|
|
||||||
r := make(map[string]interface{})
|
r := make(map[string]interface{})
|
||||||
r["key"] = key
|
|
||||||
r["nodes"] = nodes
|
r["nodes"] = nodes
|
||||||
r["categories"] = categories
|
r["categories"] = categories
|
||||||
r["versions"] = analyticsFor(versions, 10)
|
r["versions"] = analyticsFor(versions, 10)
|
||||||
r["platforms"] = analyticsFor(platforms, 0)
|
r["platforms"] = analyticsFor(platforms, 0)
|
||||||
r["os"] = analyticsFor(oses, 0)
|
r["os"] = analyticsFor(oses, 0)
|
||||||
|
|
||||||
reportCache = r
|
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -419,8 +426,3 @@ func transformVersion(v string) string {
|
|||||||
|
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
// timestamp returns a time stamp for the current hour, to be used as a cache key
|
|
||||||
func timestamp() string {
|
|
||||||
return time.Now().Format("20060102T15")
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user