one off hack to cache query results between requests... need generalization

This commit is contained in:
Ted Unangst 2019-10-30 00:20:41 -04:00
parent d416f10519
commit b4869cd9ab
1 changed files with 29 additions and 1 deletions

View File

@ -24,6 +24,7 @@ import (
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"humungus.tedunangst.com/r/webs/cache" "humungus.tedunangst.com/r/webs/cache"
@ -215,10 +216,37 @@ func gethonksforuserfirstclass(userid int64, wanted int64) []*Honk {
rows, err := stmtHonksForUserFirstClass.Query(wanted, userid, dt, userid, userid) rows, err := stmtHonksForUserFirstClass.Query(wanted, userid, dt, userid, userid)
return getsomehonks(rows, err) return getsomehonks(rows, err)
} }
var mehonks = make(map[int64][]*Honk)
var melock sync.Mutex
func gethonksforme(userid int64, wanted int64) []*Honk { func gethonksforme(userid int64, wanted int64) []*Honk {
if wanted > 0 {
dt := time.Now().UTC().Add(-7 * 24 * time.Hour).Format(dbtimeformat) dt := time.Now().UTC().Add(-7 * 24 * time.Hour).Format(dbtimeformat)
rows, err := stmtHonksForMe.Query(wanted, userid, dt, userid) rows, err := stmtHonksForMe.Query(wanted, userid, dt, userid)
return getsomehonks(rows, err) return getsomehonks(rows, err)
}
melock.Lock()
defer melock.Unlock()
honks := mehonks[userid]
if len(honks) == 0 {
dt := time.Now().UTC().Add(-7 * 24 * time.Hour).Format(dbtimeformat)
rows, err := stmtHonksForMe.Query(wanted, userid, dt, userid)
honks = getsomehonks(rows, err)
mehonks[userid] = honks
return honks
}
wanted = honks[0].ID
dt := time.Now().UTC().Add(-7 * 24 * time.Hour).Format(dbtimeformat)
rows, err := stmtHonksForMe.Query(wanted, userid, dt, userid)
honks = getsomehonks(rows, err)
honks = append(honks, mehonks[userid]...)
if len(honks) > 250 {
honks = honks[:250]
}
mehonks[userid] = honks
return honks
} }
func getsavedhonks(userid int64, wanted int64) []*Honk { func getsavedhonks(userid int64, wanted int64) []*Honk {
rows, err := stmtHonksISaved.Query(wanted, userid) rows, err := stmtHonksISaved.Query(wanted, userid)