aboutsummaryrefslogtreecommitdiff
path: root/modules/grils
diff options
context:
space:
mode:
authorjan <jan@ruken.pw>2016-11-14 20:55:41 (UTC)
committerjan <jan@ruken.pw>2016-11-14 20:55:41 (UTC)
commite02ad854383265db216ed92b10c4f12408999d6a (patch)
treef9622d16b369ce0202d38b4c6ede7c4cde4f591f /modules/grils
parentf5eb019e0883e4bbacd762ab2ab1a4647e74e750 (diff)
grilist wieder schnell machen
Diffstat (limited to 'modules/grils')
-rw-r--r--modules/grils/grils.go60
1 files changed, 25 insertions, 35 deletions
diff --git a/modules/grils/grils.go b/modules/grils/grils.go
index d68c6df..0ba4c61 100644
--- a/modules/grils/grils.go
+++ b/modules/grils/grils.go
@@ -65,16 +65,23 @@ func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*mo
65 gril := &models.Gril{} 65 gril := &models.Gril{}
66 var tags []byte 66 var tags []byte
67 var otherNames []byte 67 var otherNames []byte
68 err = rows.Scan(&gril.ID, &gril.KanjiName, &gril.RomajiName, &otherNames, &gril.UpdatedAt, &gril.Age, &gril.Birthday, &tags) 68
69 var kanjiName *string
70 err = rows.Scan(&gril.ID, &kanjiName, &gril.RomajiName, &otherNames, &gril.UpdatedAt, &gril.Age, &gril.Birthday, &tags)
69 if err != nil { 71 if err != nil {
70 log.Println(gril.ID) 72 log.Println(gril.ID)
71 log.Println("error scanning in getGrils:", err) 73 log.Println("error scanning in getGrils:", err)
72 continue 74 continue
73 } 75 }
76 if kanjiName != nil {
77 gril.KanjiName = *kanjiName
78 }
74 79
75 gril.Tags = pgArray(tags) 80 gril.Tags = pgArray(tags)
76 gril.OtherNames = pgArray(otherNames) 81 gril.OtherNames = pgArray(otherNames)
77 82
83 m.c.Insert(gril.ID, gril)
84
78 grils = append(grils, gril) 85 grils = append(grils, gril)
79 } 86 }
80 87
@@ -166,7 +173,7 @@ func (m *GrilsModule) FromID(id int) (*models.Gril, error) {
166 defer rows.Close() 173 defer rows.Close()
167 174
168 for rows.Next() { 175 for rows.Next() {
169 var name string 176 var name *string
170 var name_type int 177 var name_type int
171 if err := rows.Scan(&name, &name_type); err != nil { 178 if err := rows.Scan(&name, &name_type); err != nil {
172 return nil, err 179 return nil, err
@@ -174,43 +181,14 @@ func (m *GrilsModule) FromID(id int) (*models.Gril, error) {
174 181
175 switch name_type { 182 switch name_type {
176 case 0: 183 case 0:
177 gril.KanjiName = name 184 gril.KanjiName = *name
178 break 185 break
179 case 1: 186 case 1:
180 gril.RomajiName = name 187 gril.RomajiName = *name
181 default: 188 default:
182 gril.OtherNames = append(gril.OtherNames, name) 189 gril.OtherNames = append(gril.OtherNames, *name)
183 } 190 }
184 } 191 }
185 /*rows, err := m.g.DB.Query(`SELECT name FROM grilist.gril_names WHERE gril_id = $1 AND name_type > 1`, id)
186 if err != nil {
187 return nil, err
188 }
189 defer rows.Close()
190
191 for rows.Next() {
192 var name string
193 if err := rows.Scan(&name); err != nil {
194 return nil, err
195 }
196 gril.OtherNames = append(gril.OtherNames, name)
197 }
198 rows.Close()
199
200 // Tags holen
201 rows, err := m.g.DB.Query(`SELECT name FROM grilist.tags WHERE id IN (SELECT tag_id FROM grilist.grils_tags WHERE gril_id = $1)`, id)
202 if err != nil {
203 return nil, err
204 }
205 defer rows.Close()
206
207 for rows.Next() {
208 var name string
209 if err := rows.Scan(&name); err != nil {
210 return nil, err
211 }
212 gril.Tags = append(gril.Tags, name)
213 }*/
214 192
215 m.c.Insert(id, gril) 193 m.c.Insert(id, gril)
216 log.Printf("get_gril_from_id: %dms", time.Since(t1).Nanoseconds()/1000000) 194 log.Printf("get_gril_from_id: %dms", time.Since(t1).Nanoseconds()/1000000)
@@ -222,9 +200,16 @@ func (m *GrilsModule) FromIDs(ids []int) ([]*models.Gril, error) {
222 return make([]*models.Gril, 0), nil 200 return make([]*models.Gril, 0), nil
223 } 201 }
224 202
203 t1 := time.Now()
225 idList := "(" 204 idList := "("
226 first := true 205 first := true
206 var list []*models.Gril
227 for _, v := range ids { 207 for _, v := range ids {
208 if m.c.Has(v) {
209 g, _ := m.c.Get(v)
210 list = append(list, g.(*models.Gril))
211 continue
212 }
228 if first { 213 if first {
229 first = false 214 first = false
230 } else { 215 } else {
@@ -234,7 +219,12 @@ func (m *GrilsModule) FromIDs(ids []int) ([]*models.Gril, error) {
234 } 219 }
235 idList += ")" 220 idList += ")"
236 grils, err := m.getGrils("id IN " + idList) 221 grils, err := m.getGrils("id IN " + idList)
237 return grils, err 222
223 for _, g := range grils {
224 list = append(list, g)
225 }
226 log.Printf("get_gril_from_ids: %dms", time.Since(t1).Nanoseconds()/1000000)
227 return list, err
238} 228}
239 229
240func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 230func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) {