diff options
author | 2022-11-15 18:45:15 +0000 | |
---|---|---|
committer | 2022-11-15 18:45:15 +0000 | |
commit | 8598dea98b872647393117704659878d9b38d4fc (patch) | |
tree | 1940168912dc7f54af723439dbc9f6e0a42f30ae /internal/db/bundb/notification.go | |
parent | [docs] Both HTTP proxies and NAT can cause rate limiting issues (#1053) (diff) | |
download | gotosocial-8598dea98b872647393117704659878d9b38d4fc.tar.xz |
[chore] update database caching library (#1040)
* convert most of the caches to use result.Cache{}
* add caching of emojis
* fix issues causing failing tests
* update go-cache/v2 instances with v3
* fix getnotification
* add a note about the left-in StatusCreate comment
* update EmojiCategory db access to use new result.Cache{}
* fix possible panic in getstatusparents
* further proof that kim is not stinky
Diffstat (limited to 'internal/db/bundb/notification.go')
-rw-r--r-- | internal/db/bundb/notification.go | 54 |
1 files changed, 32 insertions, 22 deletions
diff --git a/internal/db/bundb/notification.go b/internal/db/bundb/notification.go index 69e3cf39f..1874f81ea 100644 --- a/internal/db/bundb/notification.go +++ b/internal/db/bundb/notification.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,31 +31,40 @@ import ( type notificationDB struct { conn *DBConn - cache cache.Cache[string, *gtsmodel.Notification] + cache *result.Cache[*gtsmodel.Notification] } -func (n *notificationDB) GetNotification(ctx context.Context, id string) (*gtsmodel.Notification, db.Error) { - if notification, ok := n.cache.Get(id); ok { - return notification, nil - } - - dst := gtsmodel.Notification{ID: id} - - q := n.conn.NewSelect(). - Model(&dst). - Relation("OriginAccount"). - Relation("TargetAccount"). - Relation("Status"). - Where("? = ?", bun.Ident("notification.id"), id) - - if err := q.Scan(ctx); err != nil { - return nil, n.conn.ProcessError(err) - } +func (n *notificationDB) init() { + // Initialize notification result cache + n.cache = result.NewSized([]result.Lookup{ + {Name: "ID"}, + }, func(n1 *gtsmodel.Notification) *gtsmodel.Notification { + n2 := new(gtsmodel.Notification) + *n2 = *n1 + return n2 + }, 1000) + + // Set cache TTL and start sweep routine + n.cache.SetTTL(time.Minute*5, false) + n.cache.Start(time.Second * 10) +} - copy := dst - n.cache.Set(id, ©) +func (n *notificationDB) GetNotification(ctx context.Context, id string) (*gtsmodel.Notification, db.Error) { + return n.cache.Load("ID", func() (*gtsmodel.Notification, error) { + var notif gtsmodel.Notification + + q := n.conn.NewSelect(). + Model(¬if). + Relation("OriginAccount"). + Relation("TargetAccount"). + Relation("Status"). + Where("? = ?", bun.Ident("notification.id"), id) + if err := q.Scan(ctx); err != nil { + return nil, n.conn.ProcessError(err) + } - return &dst, nil + return ¬if, nil + }, id) } func (n *notificationDB) GetNotifications(ctx context.Context, accountID string, excludeTypes []string, limit int, maxID string, sinceID string) ([]*gtsmodel.Notification, db.Error) { |