diff options
author | 2020-06-22 19:33:07 +0200 | |
---|---|---|
committer | 2020-06-22 19:33:07 +0200 | |
commit | b9d384f9d788f83c4b21b708bc5920c58efb3ce5 (patch) | |
tree | 0856ec2b5eb420f5b2a4dd86a6fcbb90508573dc | |
parent | Improve the performance of the importer (diff) | |
download | archives-b9d384f9d788f83c4b21b708bc5920c58efb3ce5.tar.gz archives-b9d384f9d788f83c4b21b708bc5920c58efb3ce5.tar.bz2 archives-b9d384f9d788f83c4b21b708bc5920c58efb3ce5.zip |
Cache pages to improve the performance
Some pages as the landing page or the lists
overview page are cached now to improve the
performance.
Signed-off-by: Max Magorsch <arzano@gentoo.org>
-rw-r--r-- | pkg/app/cache/update.go | 29 | ||||
-rw-r--r-- | pkg/app/home/home.go | 18 | ||||
-rw-r--r-- | pkg/app/list/browse.go | 17 | ||||
-rw-r--r-- | pkg/app/list/show.go | 19 | ||||
-rw-r--r-- | pkg/app/popular/threads.go | 17 | ||||
-rw-r--r-- | pkg/app/serve.go | 7 | ||||
-rw-r--r-- | pkg/cache/storage.go | 15 |
7 files changed, 100 insertions, 22 deletions
diff --git a/pkg/app/cache/update.go b/pkg/app/cache/update.go new file mode 100644 index 0000000..c7d8c5a --- /dev/null +++ b/pkg/app/cache/update.go @@ -0,0 +1,29 @@ +package cache + +import ( + "archives/pkg/app/home" + "archives/pkg/app/list" + "archives/pkg/app/popular" + "archives/pkg/cache" + "archives/pkg/config" + "net/http" +) + +func UpdateHandler(w http.ResponseWriter, r *http.Request) { + Update() + w.Write([]byte("Updated.")) +} + +func Init(){ + cache.Init() +} + +func Update(){ + cache.Put("/", home.ComputeTemplateData()) + cache.Put("/lists", list.ComputeBrowseTemplateData()) + cache.Put("/popular", popular.ComputeThreadsTemplateData()) + for _, listName := range config.AllPublicMailingLists() { + cache.Put("/"+listName+"/", list.ComputeShowTemplateData(listName)) + cache.Put("/"+listName+"/", list.ComputeShowTemplateData(listName)) + } +} diff --git a/pkg/app/home/home.go b/pkg/app/home/home.go index fe8a56e..6d86095 100644 --- a/pkg/app/home/home.go +++ b/pkg/app/home/home.go @@ -4,6 +4,7 @@ package home import ( "archives/pkg/app/popular" + "archives/pkg/cache" "archives/pkg/config" "archives/pkg/database" "archives/pkg/models" @@ -14,7 +15,15 @@ import ( // Show renders a template to show the landing page of the application func Show(w http.ResponseWriter, r *http.Request) { + templateData := cache.Get("/") + if templateData == nil { + http.NotFound(w,r) + return + } + renderIndexTemplate(w, templateData) +} +func ComputeTemplateData() interface{} { var mailingLists []models.MailingList for _, mailingList := range config.IndexMailingLists() { @@ -48,14 +57,13 @@ func Show(w http.ResponseWriter, r *http.Request) { // popularThreads, err := popular.GetPopularThreads(10, "2020-06-01") if err != nil { - http.NotFound(w, r) - return + return nil } if len(popularThreads) > 5 { popularThreads = popularThreads[:5] } - templateData := struct { + return struct { MailingLists []models.MailingList PopularThreads []*models.Message MessageCount string @@ -66,6 +74,4 @@ func Show(w http.ResponseWriter, r *http.Request) { MessageCount: formatMessageCount(getAllMessagesCount()), CurrentMonth: time.Now().Format("2006-01"), } - - renderIndexTemplate(w, templateData) -} +}
\ No newline at end of file diff --git a/pkg/app/list/browse.go b/pkg/app/list/browse.go index fdc3ab1..5d8457e 100644 --- a/pkg/app/list/browse.go +++ b/pkg/app/list/browse.go @@ -1,6 +1,7 @@ package list import ( + "archives/pkg/cache" "archives/pkg/config" "archives/pkg/database" "archives/pkg/models" @@ -8,7 +9,15 @@ import ( ) func Browse(w http.ResponseWriter, r *http.Request) { + templateData := cache.Get("/lists") + if templateData == nil { + http.NotFound(w,r) + return + } + renderBrowseTemplate(w, templateData) +} +func ComputeBrowseTemplateData() interface{} { var res []struct { Name string MessageCount int @@ -19,8 +28,7 @@ func Browse(w http.ResponseWriter, r *http.Request) { Select(&res) if err != nil { - http.NotFound(w,r) - return + return nil } var currentMailingLists []models.MailingList @@ -40,18 +48,15 @@ func Browse(w http.ResponseWriter, r *http.Request) { } } - browseData := struct { + return struct { CurrentMailingLists []models.MailingList FrozenArchives []models.MailingList }{ CurrentMailingLists: currentMailingLists, FrozenArchives: frozenArchives, } - - renderBrowseTemplate(w, browseData) } - func contains(s []string, e string) bool { for _, a := range s { if a == e { diff --git a/pkg/app/list/show.go b/pkg/app/list/show.go index d90f236..5231b5b 100644 --- a/pkg/app/list/show.go +++ b/pkg/app/list/show.go @@ -1,6 +1,7 @@ package list import ( + "archives/pkg/cache" "archives/pkg/database" "archives/pkg/models" "github.com/go-pg/pg/v10/orm" @@ -9,9 +10,17 @@ import ( ) func Show(w http.ResponseWriter, r *http.Request) { - listName := strings.ReplaceAll(r.URL.Path, "/", "") + templateData := cache.Get("/" + listName + "/") + if templateData == nil { + http.NotFound(w,r) + return + } + renderShowTemplate(w, listName, templateData) +} + +func ComputeShowTemplateData(listName string) interface{} { var res []struct { CombinedDate string MessageCount int @@ -36,9 +45,7 @@ func Show(w http.ResponseWriter, r *http.Request) { Select(&res) if err != nil { - http.NotFound(w, r) - return + return nil } - - renderShowTemplate(w, listName, res) -} + return res +}
\ No newline at end of file diff --git a/pkg/app/popular/threads.go b/pkg/app/popular/threads.go index 5c12c8a..5d0788a 100644 --- a/pkg/app/popular/threads.go +++ b/pkg/app/popular/threads.go @@ -1,14 +1,23 @@ package popular import ( + "archives/pkg/cache" "net/http" ) func Threads(w http.ResponseWriter, r *http.Request) { - threads, err := GetPopularThreads(25, "2020-06-01") - if err != nil { - http.NotFound(w, r) + templateData := cache.Get("/popular") + if templateData == nil { + http.NotFound(w,r) return } - renderPopularThreads(w, threads) + renderPopularThreads(w, templateData) } + +func ComputeThreadsTemplateData() interface{} { + threads, err := GetPopularThreads(25, "2020-06-01") + if err != nil { + return nil + } + return threads +}
\ No newline at end of file diff --git a/pkg/app/serve.go b/pkg/app/serve.go index 62eac33..fed6435 100644 --- a/pkg/app/serve.go +++ b/pkg/app/serve.go @@ -3,6 +3,7 @@ package app import ( + "archives/pkg/app/cache" "archives/pkg/app/home" "archives/pkg/app/list" "archives/pkg/app/message" @@ -17,6 +18,10 @@ import ( // Serve is used to serve the web application func Serve() { + // init caches + cache.Init() + cache.Update() + fmt.Println("Serving on Port " + config.Port()) for _, mailingList := range config.AllPublicMailingLists() { @@ -37,6 +42,8 @@ func Serve() { fs := http.StripPrefix("/assets/", http.FileServer(http.Dir("assets"))) http.Handle("/assets/", fs) + setRoute("/cache/update", cache.UpdateHandler) + log.Fatal(http.ListenAndServe(":"+config.Port(), nil)) } diff --git a/pkg/cache/storage.go b/pkg/cache/storage.go new file mode 100644 index 0000000..c5b273b --- /dev/null +++ b/pkg/cache/storage.go @@ -0,0 +1,15 @@ +package cache + +var data map[string]interface{} + +func Init(){ + data = make(map[string]interface{}) +} + +func Put(key string, value interface{}) { + data[key] = value +} + +func Get(key string) interface{} { + return data[key] +} |