mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-11-14 02:27:28 -06:00
[chore] move caches to a separate State{} structure (#1078)
* move caches to a separate State{} structure
Signed-off-by: kim <grufwub@gmail.com>
* fix call to log.Panic not using formatted call
Signed-off-by: kim <grufwub@gmail.com>
* move caches to use interfaces, to make switchouts easier in future
Signed-off-by: kim <grufwub@gmail.com>
* fix rebase issue
Signed-off-by: kim <grufwub@gmail.com>
* improve code comment
Signed-off-by: kim <grufwub@gmail.com>
* fix further issues after rebase
Signed-off-by: kim <grufwub@gmail.com>
* heh
Signed-off-by: kim <grufwub@gmail.com>
* add missing license text
Signed-off-by: kim <grufwub@gmail.com>
Signed-off-by: kim <grufwub@gmail.com>
This commit is contained in:
parent
dd1a4cd892
commit
e58d2d8122
27 changed files with 725 additions and 332 deletions
|
|
@ -22,38 +22,19 @@ import (
|
|||
"context"
|
||||
"time"
|
||||
|
||||
"codeberg.org/gruf/go-cache/v3/result"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/state"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type userDB struct {
|
||||
conn *DBConn
|
||||
cache *result.Cache[*gtsmodel.User]
|
||||
}
|
||||
|
||||
func (u *userDB) init() {
|
||||
// Initialize user result cache
|
||||
u.cache = result.NewSized([]result.Lookup{
|
||||
{Name: "ID"},
|
||||
{Name: "AccountID"},
|
||||
{Name: "Email"},
|
||||
{Name: "ConfirmationToken"},
|
||||
{Name: "ExternalID"},
|
||||
}, func(u1 *gtsmodel.User) *gtsmodel.User {
|
||||
u2 := new(gtsmodel.User)
|
||||
*u2 = *u1
|
||||
return u2
|
||||
}, 1000)
|
||||
|
||||
// Set cache TTL and start sweep routine
|
||||
u.cache.SetTTL(time.Minute*5, false)
|
||||
u.cache.Start(time.Second * 10)
|
||||
state *state.State
|
||||
}
|
||||
|
||||
func (u *userDB) GetUserByID(ctx context.Context, id string) (*gtsmodel.User, db.Error) {
|
||||
return u.cache.Load("ID", func() (*gtsmodel.User, error) {
|
||||
return u.state.Caches.GTS.User().Load("ID", func() (*gtsmodel.User, error) {
|
||||
var user gtsmodel.User
|
||||
|
||||
q := u.conn.
|
||||
|
|
@ -71,7 +52,7 @@ func (u *userDB) GetUserByID(ctx context.Context, id string) (*gtsmodel.User, db
|
|||
}
|
||||
|
||||
func (u *userDB) GetUserByAccountID(ctx context.Context, accountID string) (*gtsmodel.User, db.Error) {
|
||||
return u.cache.Load("AccountID", func() (*gtsmodel.User, error) {
|
||||
return u.state.Caches.GTS.User().Load("AccountID", func() (*gtsmodel.User, error) {
|
||||
var user gtsmodel.User
|
||||
|
||||
q := u.conn.
|
||||
|
|
@ -89,7 +70,7 @@ func (u *userDB) GetUserByAccountID(ctx context.Context, accountID string) (*gts
|
|||
}
|
||||
|
||||
func (u *userDB) GetUserByEmailAddress(ctx context.Context, emailAddress string) (*gtsmodel.User, db.Error) {
|
||||
return u.cache.Load("Email", func() (*gtsmodel.User, error) {
|
||||
return u.state.Caches.GTS.User().Load("Email", func() (*gtsmodel.User, error) {
|
||||
var user gtsmodel.User
|
||||
|
||||
q := u.conn.
|
||||
|
|
@ -105,9 +86,9 @@ func (u *userDB) GetUserByEmailAddress(ctx context.Context, emailAddress string)
|
|||
return &user, nil
|
||||
}, emailAddress)
|
||||
}
|
||||
func (u *userDB) GetUserByExternalID(ctx context.Context, id string) (*gtsmodel.User, db.Error) {
|
||||
|
||||
return u.cache.Load("ExternalID", func() (*gtsmodel.User, error) {
|
||||
func (u *userDB) GetUserByExternalID(ctx context.Context, id string) (*gtsmodel.User, db.Error) {
|
||||
return u.state.Caches.GTS.User().Load("ExternalID", func() (*gtsmodel.User, error) {
|
||||
var user gtsmodel.User
|
||||
|
||||
q := u.conn.
|
||||
|
|
@ -125,7 +106,7 @@ func (u *userDB) GetUserByExternalID(ctx context.Context, id string) (*gtsmodel.
|
|||
}
|
||||
|
||||
func (u *userDB) GetUserByConfirmationToken(ctx context.Context, confirmationToken string) (*gtsmodel.User, db.Error) {
|
||||
return u.cache.Load("ConfirmationToken", func() (*gtsmodel.User, error) {
|
||||
return u.state.Caches.GTS.User().Load("ConfirmationToken", func() (*gtsmodel.User, error) {
|
||||
var user gtsmodel.User
|
||||
|
||||
q := u.conn.
|
||||
|
|
@ -143,7 +124,7 @@ func (u *userDB) GetUserByConfirmationToken(ctx context.Context, confirmationTok
|
|||
}
|
||||
|
||||
func (u *userDB) PutUser(ctx context.Context, user *gtsmodel.User) db.Error {
|
||||
return u.cache.Store(user, func() error {
|
||||
return u.state.Caches.GTS.User().Store(user, func() error {
|
||||
_, err := u.conn.
|
||||
NewInsert().
|
||||
Model(user).
|
||||
|
|
@ -172,8 +153,8 @@ func (u *userDB) UpdateUser(ctx context.Context, user *gtsmodel.User, columns ..
|
|||
return u.conn.ProcessError(err)
|
||||
}
|
||||
|
||||
// Invalidate in cache
|
||||
u.cache.Invalidate("ID", user.ID)
|
||||
// Invalidate user from cache
|
||||
u.state.Caches.GTS.User().Invalidate("ID", user.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -187,6 +168,6 @@ func (u *userDB) DeleteUserByID(ctx context.Context, userID string) db.Error {
|
|||
}
|
||||
|
||||
// Invalidate user from cache
|
||||
u.cache.Invalidate("ID", userID)
|
||||
u.state.Caches.GTS.User().Invalidate("ID", userID)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue