From 14bf8e62f81d7eac637f2097a88b4c3c32a8a7b5 Mon Sep 17 00:00:00 2001 From: kim Date: Fri, 17 Oct 2025 19:41:06 +0200 Subject: [performance] reduce account stats database calls (#4496) Reduces both code complexity and the number of separate database transactions we need to make by moving account statistics operations into the database as side-effects of the operations that effect them. In contrast to currently, where we manually update account statistics at the application layer. Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4496 Co-authored-by: kim Co-committed-by: kim --- internal/cache/invalidate.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'internal/cache') diff --git a/internal/cache/invalidate.go b/internal/cache/invalidate.go index 58c427050..acb602736 100644 --- a/internal/cache/invalidate.go +++ b/internal/cache/invalidate.go @@ -138,15 +138,20 @@ func (c *Caches) OnInvalidateFilterStatus(filterStatus *gtsmodel.FilterStatus) { } func (c *Caches) OnInvalidateFollow(follow *gtsmodel.Follow) { + bothAccountIDs := []string{ + follow.TargetAccountID, + follow.AccountID, + } + // Invalidate follow request with this same ID. c.DB.FollowRequest.Invalidate("ID", follow.ID) + // Invalidate both follow origin and target account stats. + c.DB.AccountStats.InvalidateIDs("AccountID", bothAccountIDs) + // Invalidate both follow origin and target as // possible lookup targets for visibility results. - c.Visibility.InvalidateIDs("ItemID", []string{ - follow.TargetAccountID, - follow.AccountID, - }) + c.Visibility.InvalidateIDs("ItemID", bothAccountIDs) // Track which of follow / target are local. localAccountIDs := make([]string, 0, 2) @@ -217,6 +222,10 @@ func (c *Caches) OnInvalidateFollowRequest(followReq *gtsmodel.FollowRequest) { // Invalidate follow with this same ID. c.DB.Follow.Invalidate("ID", followReq.ID) + // Invalidate follow target account stats. + c.DB.AccountStats.Invalidate("AccountID", + followReq.TargetAccountID) + // Invalidate ID slice cache. c.DB.FollowRequestIDs.Invalidate( -- cgit v1.2.3