From 2fc3103ad46d3cd19c790e274f51e6c033cb0ec7 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Mon, 7 Oct 2019 15:22:10 -0400 Subject: [PATCH] cache jonks for AP fetches --- activity.go | 27 +++++++++++++++++++++++++++ database.go | 3 ++- web.go | 29 +++++++++++++++-------------- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/activity.go b/activity.go index b415c19..ac6b853 100644 --- a/activity.go +++ b/activity.go @@ -1101,6 +1101,33 @@ func jonkjonk(user *WhatAbout, h *Honk) (junk.Junk, junk.Junk) { return j, jo } +var oldjonks = cacheNew(cacheOptions{Filler: func(xid string) ([]byte, bool) { + row := stmtAnyXonk.QueryRow(xid) + honk := scanhonk(row) + if honk == nil || !honk.Public { + return nil, true + } + user, _ := butwhatabout(honk.Username) + rawhonks := gethonksbyconvoy(honk.UserID, honk.Convoy) + for _, h := range rawhonks { + if h.RID == honk.XID && h.Public && (h.Whofore == 2 || h.IsAcked()) { + honk.Replies = append(honk.Replies, h) + } + } + donksforhonks([]*Honk{honk}) + _, j := jonkjonk(user, honk) + j["@context"] = itiswhatitis + var buf bytes.Buffer + j.Write(&buf) + return buf.Bytes(), true +}}) + +func gimmejonk(xid string) ([]byte, bool) { + var j []byte + ok := oldjonks.Get(xid, &j) + return j, ok +} + func honkworldwide(user *WhatAbout, honk *Honk) { jonk, _ := jonkjonk(user, honk) jonk["@context"] = itiswhatitis diff --git a/database.go b/database.go index efdab01..794727b 100644 --- a/database.go +++ b/database.go @@ -509,7 +509,7 @@ func cleanupdb(arg string) { } var stmtHonkers, stmtDubbers, stmtSaveHonker, stmtUpdateFlavor, stmtUpdateHonker *sql.Stmt -var stmtOneXonk, stmtPublicHonks, stmtUserHonks, stmtHonksByCombo, stmtHonksByConvoy *sql.Stmt +var stmtAnyXonk, stmtOneXonk, stmtPublicHonks, stmtUserHonks, stmtHonksByCombo, stmtHonksByConvoy *sql.Stmt var stmtHonksByOntology, stmtHonksForUser, stmtHonksForMe, stmtSaveDub, stmtHonksByXonker *sql.Stmt var stmtHonksBySearch, stmtHonksByHonker, stmtSaveHonk, stmtWhatAbout *sql.Stmt var stmtOneBonk, stmtFindZonk, stmtFindXonk, stmtSaveDonk *sql.Stmt @@ -541,6 +541,7 @@ func prepareStatements(db *sql.DB) { limit := " order by honks.honkid desc limit 250" butnotthose := " and convoy not in (select name from zonkers where userid = ? and wherefore = 'zonvoy' order by zonkerid desc limit 100)" stmtOneXonk = preparetodie(db, selecthonks+"where honks.userid = ? and xid = ?") + stmtAnyXonk = preparetodie(db, selecthonks+"where xid = ? order by honks.honkid asc") stmtOneBonk = preparetodie(db, selecthonks+"where honks.userid = ? and xid = ? and what = 'bonk' and whofore = 2") stmtPublicHonks = preparetodie(db, selecthonks+"where whofore = 2 and dt > ?"+limit) stmtUserHonks = preparetodie(db, selecthonks+"where (whofore = 2 or whofore = ?) and username = ? and dt > ?"+limit) diff --git a/web.go b/web.go index c940afd..06a2910 100644 --- a/web.go +++ b/web.go @@ -659,8 +659,18 @@ func showhonk(w http.ResponseWriter, r *http.Request) { http.NotFound(w, r) return } - xid := fmt.Sprintf("https://%s%s", serverName, r.URL.Path) + + if friendorfoe(r.Header.Get("Accept")) { + j, ok := gimmejonk(xid) + if ok { + w.Header().Set("Content-Type", theonetruename) + w.Write(j) + } else { + http.NotFound(w, r) + } + return + } honk := getxonk(user.ID, xid) if honk == nil { http.NotFound(w, r) @@ -683,19 +693,6 @@ func showhonk(w http.ResponseWriter, r *http.Request) { return } rawhonks := gethonksbyconvoy(honk.UserID, honk.Convoy) - if friendorfoe(r.Header.Get("Accept")) { - for _, h := range rawhonks { - if h.RID == honk.XID && h.Public && (h.Whofore == 2 || h.IsAcked()) { - honk.Replies = append(honk.Replies, h) - } - } - donksforhonks([]*Honk{honk}) - _, j := jonkjonk(user, honk) - j["@context"] = itiswhatitis - w.Header().Set("Content-Type", theonetruename) - j.Write(w) - return - } var honks []*Honk for _, h := range rawhonks { if h.Public && (h.Whofore == 2 || h.IsAcked()) { @@ -816,6 +813,9 @@ func zonkit(w http.ResponseWriter, r *http.Request) { userinfo := login.GetUserInfo(r) user, _ := butwhatabout(userinfo.Username) + // my hammer is too big, oh well + defer oldjonks.Flush() + if wherefore == "ack" { xonk := getxonk(userinfo.UserID, what) if xonk != nil { @@ -1127,6 +1127,7 @@ func submithonk(w http.ResponseWriter, r *http.Request) { if updatexid != "" { updatehonk(honk) + oldjonks.Clear(honk.XID) } else { err := savehonk(honk) if err != nil {