no wonder caching is a bad idea, honks mutate... make a copy.

This commit is contained in:
Ted Unangst 2019-10-30 00:32:23 -04:00
parent b4869cd9ab
commit 75ec971004
1 changed files with 12 additions and 2 deletions

View File

@ -220,6 +220,16 @@ func gethonksforuserfirstclass(userid int64, wanted int64) []*Honk {
var mehonks = make(map[int64][]*Honk) var mehonks = make(map[int64][]*Honk)
var melock sync.Mutex var melock sync.Mutex
func copyhonks(honks []*Honk) []*Honk {
rv := make([]*Honk, len(honks))
for i, h := range honks {
dupe := new(Honk)
*dupe = *h
rv[i] = dupe
}
return rv
}
func gethonksforme(userid int64, wanted int64) []*Honk { func gethonksforme(userid int64, wanted int64) []*Honk {
if wanted > 0 { 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)
@ -234,7 +244,7 @@ func gethonksforme(userid int64, wanted int64) []*Honk {
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)
honks = getsomehonks(rows, err) honks = getsomehonks(rows, err)
mehonks[userid] = honks mehonks[userid] = copyhonks(honks)
return honks return honks
} }
wanted = honks[0].ID wanted = honks[0].ID
@ -245,7 +255,7 @@ func gethonksforme(userid int64, wanted int64) []*Honk {
if len(honks) > 250 { if len(honks) > 250 {
honks = honks[:250] honks = honks[:250]
} }
mehonks[userid] = honks mehonks[userid] = copyhonks(honks)
return honks return honks
} }
func getsavedhonks(userid int64, wanted int64) []*Honk { func getsavedhonks(userid int64, wanted int64) []*Honk {