mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-12-07 03:48:07 -06:00
[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
This commit is contained in:
parent
9ab60136dd
commit
8598dea98b
55 changed files with 725 additions and 2289 deletions
|
|
@ -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) 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)
|
||||
}
|
||||
|
||||
func (n *notificationDB) GetNotification(ctx context.Context, id string) (*gtsmodel.Notification, db.Error) {
|
||||
if notification, ok := n.cache.Get(id); ok {
|
||||
return notification, nil
|
||||
}
|
||||
return n.cache.Load("ID", func() (*gtsmodel.Notification, error) {
|
||||
var notif gtsmodel.Notification
|
||||
|
||||
dst := gtsmodel.Notification{ID: id}
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
copy := dst
|
||||
n.cache.Set(id, ©)
|
||||
|
||||
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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue