diff options
author | 2023-05-09 15:17:43 +0100 | |
---|---|---|
committer | 2023-05-09 16:17:43 +0200 | |
commit | 8275d70e38579128f0680ee4001a944907a3772a (patch) | |
tree | ce97c3f6609bcbc6d24c7423ef7c0247011e2122 /internal | |
parent | [chore] tidy + update StatusToAPIStatus (#1754) (diff) | |
download | gotosocial-8275d70e38579128f0680ee4001a944907a3772a.tar.xz |
[bugfix] update go-cache version to support multi-keying (#1756)
* update go-cache version to support multi-keying
Signed-off-by: kim <grufwub@gmail.com>
* improved cache invalidation
Signed-off-by: kim <grufwub@gmail.com>
---------
Signed-off-by: kim <grufwub@gmail.com>
Diffstat (limited to 'internal')
-rw-r--r-- | internal/cache/cache.go | 14 | ||||
-rw-r--r-- | internal/cache/visibility.go | 4 | ||||
-rw-r--r-- | internal/db/bundb/relationship_follow_req.go | 22 |
3 files changed, 24 insertions, 16 deletions
diff --git a/internal/cache/cache.go b/internal/cache/cache.go index c1f419d22..e981d79b1 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -65,8 +65,9 @@ func (c *Caches) 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. +// as an invalidation indicates a database INSERT / UPDATE / DELETE. +// NOTE THEY ARE ONLY CALLED WHEN THE ITEM IS IN THE CACHE, SO FOR +// HOOKS TO BE CALLED ON DELETE YOU MUST FIRST POPULATE IT IN THE CACHE. func (c *Caches) setuphooks() { c.GTS.Account().SetInvalidateCallback(func(account *gtsmodel.Account) { // Invalidate account ID cached visibility. @@ -103,13 +104,18 @@ func (c *Caches) setuphooks() { c.Visibility.Invalidate("ItemID", followReq.TargetAccountID) c.Visibility.Invalidate("RequesterID", followReq.TargetAccountID) - // Invalidate any cached follow corresponding to this request. - c.GTS.Follow().Invalidate("AccountID.TargetAccountID", followReq.AccountID, followReq.TargetAccountID) + // Invalidate any cached follow with same ID. + c.GTS.Follow().Invalidate("ID", followReq.ID) }) c.GTS.Status().SetInvalidateCallback(func(status *gtsmodel.Status) { // Invalidate status ID cached visibility. c.Visibility.Invalidate("ItemID", status.ID) + + for _, id := range status.AttachmentIDs { + // Invalidate cache for attached media IDs, + c.GTS.Media().Invalidate("ID", id) + } }) c.GTS.User().SetInvalidateCallback(func(user *gtsmodel.User) { diff --git a/internal/cache/visibility.go b/internal/cache/visibility.go index 8706a8015..fd481eedc 100644 --- a/internal/cache/visibility.go +++ b/internal/cache/visibility.go @@ -30,8 +30,8 @@ type VisibilityCache struct { // NOTE: the cache MUST NOT be in use anywhere, this is not thread-safe. func (c *VisibilityCache) Init() { c.Cache = result.New([]result.Lookup{ - {Name: "ItemID"}, - {Name: "RequesterID"}, + {Name: "ItemID", Multi: true}, + {Name: "RequesterID", Multi: true}, {Name: "Type.RequesterID.ItemID"}, }, func(v1 *CachedVisibility) *CachedVisibility { v2 := new(CachedVisibility) diff --git a/internal/db/bundb/relationship_follow_req.go b/internal/db/bundb/relationship_follow_req.go index 4a6ec1ab8..c0f4bce3b 100644 --- a/internal/db/bundb/relationship_follow_req.go +++ b/internal/db/bundb/relationship_follow_req.go @@ -208,14 +208,17 @@ func (r *relationshipDB) AcceptFollowRequest(ctx context.Context, sourceAccountI Notify: followReq.Notify, } - // If the follow already exists, just - // replace the URI with the new one. - if _, err := r.conn. - NewInsert(). - Model(follow). - On("CONFLICT (?,?) DO UPDATE set ? = ?", bun.Ident("account_id"), bun.Ident("target_account_id"), bun.Ident("uri"), follow.URI). - Exec(ctx); err != nil { - return nil, r.conn.ProcessError(err) + if err := r.state.Caches.GTS.Follow().Store(follow, func() error { + // If the follow already exists, just + // replace the URI with the new one. + _, err := r.conn. + NewInsert(). + Model(follow). + On("CONFLICT (?,?) DO UPDATE set ? = ?", bun.Ident("account_id"), bun.Ident("target_account_id"), bun.Ident("uri"), follow.URI). + Exec(ctx) + return r.conn.ProcessError(err) + }); err != nil { + return nil, err } // Delete original follow request. @@ -227,8 +230,7 @@ func (r *relationshipDB) AcceptFollowRequest(ctx context.Context, sourceAccountI return nil, r.conn.ProcessError(err) } - // Invalidate follow request from cache lookups; this will - // invalidate the follow as well via the invalidate hook. + // Invalidate follow request from cache lookups r.state.Caches.GTS.FollowRequest().Invalidate("ID", followReq.ID) // Delete original follow request notification |