aboutsummaryrefslogtreecommitdiff
path: root/modules/lists
diff options
context:
space:
mode:
authorJan C <jan@ruken.pw>2016-03-28 12:30:15 (UTC)
committerJan C <jan@ruken.pw>2016-03-28 12:30:15 (UTC)
commite8f2412efe55c969390168e3ce0b41200f780a1e (patch)
tree41a0e692d0ded0113394712f750b730c4f4fa864 /modules/lists
parentc7f28c0032163075bd21f899c37ae992e3eece80 (diff)
cache hinzugefuegt. wird derzeit vom lists und grils modul verwendet. versuch die loadingzeit zu verringern (anscheinend wird im hintergrund auf jeder route der index requested, was ziemlich doof ist).
Diffstat (limited to 'modules/lists')
-rw-r--r--modules/lists/lists.go44
1 files changed, 38 insertions, 6 deletions
diff --git a/modules/lists/lists.go b/modules/lists/lists.go
index 836f6af..910162a 100644
--- a/modules/lists/lists.go
+++ b/modules/lists/lists.go
@@ -7,6 +7,7 @@ import (
7 "fagott.pw/charakterin" 7 "fagott.pw/charakterin"
8 "fagott.pw/grilist/frontend" 8 "fagott.pw/grilist/frontend"
9 "fagott.pw/grilist/grilist" 9 "fagott.pw/grilist/grilist"
10 "fagott.pw/grilist/cache"
10 "fagott.pw/grilist/modules/grils" 11 "fagott.pw/grilist/modules/grils"
11 "fmt" 12 "fmt"
12 "github.com/julienschmidt/httprouter" 13 "github.com/julienschmidt/httprouter"
@@ -22,6 +23,7 @@ import (
22// Module und so. 23// Module und so.
23type Module struct { 24type Module struct {
24 g *grilist.Grilist 25 g *grilist.Grilist
26 c *cache.Cache
25 grils *grils.GrilsModule 27 grils *grils.GrilsModule
26} 28}
27 29
@@ -76,6 +78,8 @@ func (m *Module) Init(g *grilist.Grilist) {
76 m.g.Router.POST("/new/list", m.createList) 78 m.g.Router.POST("/new/list", m.createList)
77 79
78 m.g.Router.GET("/api/lists/user", m.APIgetUserLists) 80 m.g.Router.GET("/api/lists/user", m.APIgetUserLists)
81
82 m.c = cache.New()
79} 83}
80 84
81func (m *Module) getListGrils(list *List) error { 85func (m *Module) getListGrils(list *List) error {
@@ -85,6 +89,7 @@ func (m *Module) getListGrils(list *List) error {
85 } 89 }
86 90
87 defer rows.Close() 91 defer rows.Close()
92 list.Grils = list.Grils[:0]
88 for rows.Next() { 93 for rows.Next() {
89 var grilID int 94 var grilID int
90 lg := &ListGril{} 95 lg := &ListGril{}
@@ -123,6 +128,12 @@ func (m *Module) getLists(whereClause string, params ...interface{}) ([]*List, e
123 log.Println("error scanning row in getLists:", err) 128 log.Println("error scanning row in getLists:", err)
124 continue 129 continue
125 } 130 }
131
132 if clist, ok := m.c.Get(list.ID); ok {
133 // weiteres parsen abbrechen
134 lists = append(lists, clist.(*List))
135 continue
136 }
126 137
127 // Owner kriegn 138 // Owner kriegn
128 owner, err := m.g.Charakterin.GetUserByID(ownerID) 139 owner, err := m.g.Charakterin.GetUserByID(ownerID)
@@ -131,7 +142,8 @@ func (m *Module) getLists(whereClause string, params ...interface{}) ([]*List, e
131 continue 142 continue
132 } 143 }
133 list.Owner = owner 144 list.Owner = owner
134 145
146 m.c.Insert(list.ID, list)
135 lists = append(lists, list) 147 lists = append(lists, list)
136 } 148 }
137 149
@@ -157,6 +169,17 @@ func (m *Module) GetUserLists(u *charakterin.User, withGrils bool) []*List {
157 169
158// FromID sucht nach der Liste mit der gegebenen ID und gibt sie, falls sie existiert, zurück. 170// FromID sucht nach der Liste mit der gegebenen ID und gibt sie, falls sie existiert, zurück.
159func (m *Module) FromID(id int, withGrils bool) (*List, error) { 171func (m *Module) FromID(id int, withGrils bool) (*List, error) {
172 if lst, ok := m.c.Get(id); ok {
173 l := lst.(*List)
174 // Potenzieller Optimierungsbedarf: neue query, wenn die Liste leer ist. updateGrilOrder nutzt derzeit aber genau dieses verhalten.
175 if withGrils && len(l.Grils) == 0 {
176 if err := m.getListGrils(l); err != nil {
177 return nil, err
178 }
179 }
180 return l, nil
181 }
182
160 lists, err := m.getLists(`id = $1`, id) 183 lists, err := m.getLists(`id = $1`, id)
161 if err != nil { 184 if err != nil {
162 return nil, err 185 return nil, err
@@ -210,11 +233,12 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar
210 log.Println(err) 233 log.Println(err)
211 return categories 234 return categories
212 } 235 }
213 //for _, list := range lists { 236
214 //if err := m.getListGrils(list); err != nil { 237 for _, list := range lists {
215 // log.Println(err) 238 if err := m.getListGrils(list); err != nil {
216 //} 239 log.Println(err)
217 //} 240 }
241 }
218 242
219 categories = append(categories, grilist.DashboardCategory{ 243 categories = append(categories, grilist.DashboardCategory{
220 Title: "Neueste Listen", 244 Title: "Neueste Listen",
@@ -242,12 +266,14 @@ func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.P
242 266
243 id, err := strconv.Atoi(sid) 267 id, err := strconv.Atoi(sid)
244 if err != nil { 268 if err != nil {
269 log.Println("redir")
245 http.Redirect(w, r, "/", 302) 270 http.Redirect(w, r, "/", 302)
246 return 271 return
247 } 272 }
248 273
249 list, err := m.FromID(id, true) 274 list, err := m.FromID(id, true)
250 if err != nil { 275 if err != nil {
276 log.Println("redir")
251 http.Redirect(w, r, "/", 302) 277 http.Redirect(w, r, "/", 302)
252 return 278 return
253 } 279 }
@@ -406,6 +432,12 @@ func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httpr
406 http.Error(w, "could not update gril order", 500) 432 http.Error(w, "could not update gril order", 500)
407 return 433 return
408 } 434 }
435
436 // wenn die liste im cache ist, die Grils clearen, damit beim naechsten aufruf die Gril-Liste neu geholt wird.
437 if l, ok := m.c.Get(listID); ok {
438 ls := l.(*List)
439 ls.Grils = ls.Grils[:0]
440 }
409 441
410 w.WriteHeader(200) 442 w.WriteHeader(200)
411 w.Write([]byte("ok")) 443 w.Write([]byte("ok"))