From de6e3e5f2a8ea639d76e310a11cb9bc093fef3a9 Mon Sep 17 00:00:00 2001 From: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com> Date: Tue, 28 Mar 2023 14:03:14 +0100 Subject: [performance] refactoring + add fave / follow / request / visibility caching (#1607) * refactor visibility checking, add caching for visibility * invalidate visibility cache items on account / status deletes * fix requester ID passed to visibility cache nil ptr * de-interface caches, fix home / public timeline caching + visibility * finish adding code comments for visibility filter * fix angry goconst linter warnings * actually finish adding filter visibility code comments for timeline functions * move home timeline status author check to after visibility * remove now-unused code * add more code comments * add TODO code comment, update printed cache start names * update printed cache names on stop * start adding separate follow(request) delete db functions, add specific visibility cache tests * add relationship type caching * fix getting local account follows / followed-bys, other small codebase improvements * simplify invalidation using cache hooks, add more GetAccountBy___() functions * fix boosting to return 404 if not boostable but no error (to not leak status ID) * remove dead code * improved placement of cache invalidation * update license headers * add example follow, follow-request config entries * add example visibility cache configuration to config file * use specific PutFollowRequest() instead of just Put() * add tests for all GetAccountBy() * add GetBlockBy() tests * update block to check primitive fields * update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests * fix copy-pasted code * update envparsing test * whitespace * fix bun struct tag * add license header to gtscontext * fix old license header * improved error creation to not use fmt.Errorf() when not needed * fix various rebase conflicts, fix account test * remove commented-out code, fix-up mention caching * fix mention select bun statement * ensure mention target account populated, pass in context to customrenderer logging * remove more uncommented code, fix typeutil test * add statusfave database model caching * add status fave cache configuration * add status fave cache example config * woops, catch missed error. nice catch linter! * add back testrig panic on nil db * update example configuration to match defaults, slight tweak to cache configuration defaults * update envparsing test with new defaults * fetch followingget to use the follow target account * use accounnt.IsLocal() instead of empty domain check * use constants for the cache visibility type check * use bun.In() for notification type restriction in db query * include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable()) * use bun query building for nested select statements to ensure working with postgres * update public timeline future status checks to match visibility filter * same as previous, for home timeline * update public timeline tests to dynamically check for appropriate statuses * migrate accounts to allow unique constraint on public_key * provide minimal account with publicKey --------- Signed-off-by: kim Co-authored-by: tsmethurst --- internal/cache/cache.go | 80 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 69 insertions(+), 11 deletions(-) (limited to 'internal/cache/cache.go') diff --git a/internal/cache/cache.go b/internal/cache/cache.go index 834542a52..913d6eca7 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -17,13 +17,23 @@ package cache +import ( + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" +) + type Caches struct { // GTS provides access to the collection of gtsmodel object caches. + // (used by the database). GTS GTSCaches // AP provides access to the collection of ActivityPub object caches. + // (planned to be used by the typeconverter). AP APCaches + // Visibility provides access to the item visibility cache. + // (used by the visibility filter). + Visibility VisibilityCache + // prevent pass-by-value. _ nocopy } @@ -31,29 +41,77 @@ type Caches struct { // Init will (re)initialize both the GTS and AP cache collections. // NOTE: the cache MUST NOT be in use anywhere, this is not thread-safe. func (c *Caches) Init() { - if c.GTS == nil { - // use default impl - c.GTS = NewGTS() - } - - if c.AP == nil { - // use default impl - c.AP = NewAP() - } - - // initialize caches c.GTS.Init() c.AP.Init() + c.Visibility.Init() + + // Setup cache invalidate hooks. + // !! READ THE METHOD COMMENT + c.setuphooks() } // Start will start both the GTS and AP cache collections. func (c *Caches) Start() { c.GTS.Start() c.AP.Start() + c.Visibility.Start() } // Stop will stop both the GTS and AP cache collections. func (c *Caches) Stop() { c.GTS.Stop() c.AP.Stop() + c.Visibility.Stop() +} + +// setuphooks sets necessary cache invalidation hooks between caches, +// as an invalidation indicates a database UPDATE / DELETE. INSERT is +// not handled by invalidation hooks and must be invalidated manually. +func (c *Caches) setuphooks() { + c.GTS.Account().SetInvalidateCallback(func(account *gtsmodel.Account) { + // Invalidate account ID cached visibility. + c.Visibility.Invalidate("ItemID", account.ID) + c.Visibility.Invalidate("RequesterID", account.ID) + }) + + c.GTS.Block().SetInvalidateCallback(func(block *gtsmodel.Block) { + // Invalidate block origin account ID cached visibility. + c.Visibility.Invalidate("ItemID", block.AccountID) + c.Visibility.Invalidate("RequesterID", block.AccountID) + + // Invalidate block target account ID cached visibility. + c.Visibility.Invalidate("ItemID", block.TargetAccountID) + c.Visibility.Invalidate("RequesterID", block.TargetAccountID) + }) + + c.GTS.Follow().SetInvalidateCallback(func(follow *gtsmodel.Follow) { + // Invalidate follow origin account ID cached visibility. + c.Visibility.Invalidate("ItemID", follow.AccountID) + c.Visibility.Invalidate("RequesterID", follow.AccountID) + + // Invalidate follow target account ID cached visibility. + c.Visibility.Invalidate("ItemID", follow.TargetAccountID) + c.Visibility.Invalidate("RequesterID", follow.TargetAccountID) + }) + + c.GTS.FollowRequest().SetInvalidateCallback(func(followReq *gtsmodel.FollowRequest) { + // Invalidate follow request origin account ID cached visibility. + c.Visibility.Invalidate("ItemID", followReq.AccountID) + c.Visibility.Invalidate("RequesterID", followReq.AccountID) + + // Invalidate follow request target account ID cached visibility. + c.Visibility.Invalidate("ItemID", followReq.TargetAccountID) + c.Visibility.Invalidate("RequesterID", followReq.TargetAccountID) + }) + + c.GTS.Status().SetInvalidateCallback(func(status *gtsmodel.Status) { + // Invalidate status ID cached visibility. + c.Visibility.Invalidate("ItemID", status.ID) + }) + + c.GTS.User().SetInvalidateCallback(func(user *gtsmodel.User) { + // Invalidate local account ID cached visibility. + c.Visibility.Invalidate("ItemID", user.AccountID) + c.Visibility.Invalidate("RequesterID", user.AccountID) + }) } -- cgit v1.2.3