diff options
Diffstat (limited to 'internal/db/bundb/mention.go')
-rw-r--r-- | internal/db/bundb/mention.go | 48 |
1 files changed, 28 insertions, 20 deletions
diff --git a/internal/db/bundb/mention.go b/internal/db/bundb/mention.go index 355078021..303e16484 100644 --- a/internal/db/bundb/mention.go +++ b/internal/db/bundb/mention.go @@ -20,8 +20,9 @@ package bundb import ( "context" + "time" - "codeberg.org/gruf/go-cache/v2" + "codeberg.org/gruf/go-cache/v3/result" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/log" @@ -30,7 +31,22 @@ import ( type mentionDB struct { conn *DBConn - cache cache.Cache[string, *gtsmodel.Mention] + cache *result.Cache[*gtsmodel.Mention] +} + +func (m *mentionDB) init() { + // Initialize notification result cache + m.cache = result.NewSized([]result.Lookup{ + {Name: "ID"}, + }, func(m1 *gtsmodel.Mention) *gtsmodel.Mention { + m2 := new(gtsmodel.Mention) + *m2 = *m1 + return m2 + }, 1000) + + // Set cache TTL and start sweep routine + m.cache.SetTTL(time.Minute*5, false) + m.cache.Start(time.Second * 10) } func (m *mentionDB) newMentionQ(i interface{}) *bun.SelectQuery { @@ -42,27 +58,19 @@ func (m *mentionDB) newMentionQ(i interface{}) *bun.SelectQuery { Relation("TargetAccount") } -func (m *mentionDB) getMentionDB(ctx context.Context, id string) (*gtsmodel.Mention, db.Error) { - mention := gtsmodel.Mention{} - - q := m.newMentionQ(&mention). - Where("? = ?", bun.Ident("mention.id"), id) - - if err := q.Scan(ctx); err != nil { - return nil, m.conn.ProcessError(err) - } +func (m *mentionDB) GetMention(ctx context.Context, id string) (*gtsmodel.Mention, db.Error) { + return m.cache.Load("ID", func() (*gtsmodel.Mention, error) { + var mention gtsmodel.Mention - copy := mention - m.cache.Set(mention.ID, ©) + q := m.newMentionQ(&mention). + Where("? = ?", bun.Ident("mention.id"), id) - return &mention, nil -} + if err := q.Scan(ctx); err != nil { + return nil, m.conn.ProcessError(err) + } -func (m *mentionDB) GetMention(ctx context.Context, id string) (*gtsmodel.Mention, db.Error) { - if mention, ok := m.cache.Get(id); ok { - return mention, nil - } - return m.getMentionDB(ctx, id) + return &mention, nil + }, id) } func (m *mentionDB) GetMentions(ctx context.Context, ids []string) ([]*gtsmodel.Mention, db.Error) { |