diff options
author | 2022-11-18 17:29:25 +0000 | |
---|---|---|
committer | 2022-11-18 18:29:25 +0100 | |
commit | 45ae719bd9d7bbffad0e2338149d87c47f18a4e6 (patch) | |
tree | 3b02835da1e9da0b6a53f102c8ec8d2d16983673 | |
parent | [performance] replace status query relationals with separate calls in order t... (diff) | |
download | gotosocial-45ae719bd9d7bbffad0e2338149d87c47f18a4e6.tar.xz |
[performance] replace account emojis relational query with separate calls to emojiDB to rely on cache (#1074)
Signed-off-by: kim <grufwub@gmail.com>
Signed-off-by: kim <grufwub@gmail.com>
-rw-r--r-- | internal/db/bundb/account.go | 20 | ||||
-rw-r--r-- | internal/db/bundb/bundb.go | 1 |
2 files changed, 18 insertions, 3 deletions
diff --git a/internal/db/bundb/account.go b/internal/db/bundb/account.go index 1e9c390d8..52316a7a7 100644 --- a/internal/db/bundb/account.go +++ b/internal/db/bundb/account.go @@ -36,6 +36,7 @@ import ( type accountDB struct { conn *DBConn cache *result.Cache[*gtsmodel.Account] + emojis *emojiDB status *statusDB } @@ -63,8 +64,7 @@ func (a *accountDB) newAccountQ(account *gtsmodel.Account) *bun.SelectQuery { NewSelect(). Model(account). Relation("AvatarMediaAttachment"). - Relation("HeaderMediaAttachment"). - Relation("Emojis") + Relation("HeaderMediaAttachment") } func (a *accountDB) GetAccountByID(ctx context.Context, id string) (*gtsmodel.Account, db.Error) { @@ -149,7 +149,8 @@ func (a *accountDB) GetInstanceAccount(ctx context.Context, domain string) (*gts } func (a *accountDB) getAccount(ctx context.Context, lookup string, dbQuery func(*gtsmodel.Account) error, keyParts ...any) (*gtsmodel.Account, db.Error) { - return a.cache.Load(lookup, func() (*gtsmodel.Account, error) { + // Fetch account from database cache with loader callback + account, err := a.cache.Load(lookup, func() (*gtsmodel.Account, error) { var account gtsmodel.Account // Not cached! Perform database query @@ -159,6 +160,19 @@ func (a *accountDB) getAccount(ctx context.Context, lookup string, dbQuery func( return &account, nil }, keyParts...) + if err != nil { + return nil, err + } + + if len(account.EmojiIDs) > 0 { + // Set the account's related emojis + account.Emojis, err = a.emojis.emojisFromIDs(ctx, account.EmojiIDs) + if err != nil { + return nil, err + } + } + + return account, nil } func (a *accountDB) PutAccount(ctx context.Context, account *gtsmodel.Account) db.Error { diff --git a/internal/db/bundb/bundb.go b/internal/db/bundb/bundb.go index 44b0b1807..b316f2106 100644 --- a/internal/db/bundb/bundb.go +++ b/internal/db/bundb/bundb.go @@ -171,6 +171,7 @@ func NewBunDBService(ctx context.Context) (db.DB, error) { user := &userDB{conn: conn} // Setup DB cross-referencing + account.emojis = emoji account.status = status admin.users = user status.accounts = account |