From 75ec9710049f7d11eeb58441d2650c11c3429b41 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Wed, 30 Oct 2019 00:32:23 -0400 Subject: [PATCH] no wonder caching is a bad idea, honks mutate... make a copy. --- database.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/database.go b/database.go index 39fa01a..2ceb492 100644 --- a/database.go +++ b/database.go @@ -220,6 +220,16 @@ func gethonksforuserfirstclass(userid int64, wanted int64) []*Honk { var mehonks = make(map[int64][]*Honk) 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 { if wanted > 0 { 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) rows, err := stmtHonksForMe.Query(wanted, userid, dt, userid) honks = getsomehonks(rows, err) - mehonks[userid] = honks + mehonks[userid] = copyhonks(honks) return honks } wanted = honks[0].ID @@ -245,7 +255,7 @@ func gethonksforme(userid int64, wanted int64) []*Honk { if len(honks) > 250 { honks = honks[:250] } - mehonks[userid] = honks + mehonks[userid] = copyhonks(honks) return honks } func getsavedhonks(userid int64, wanted int64) []*Honk {