Skip to content

Commit

Permalink
add more cache invalidation explanatory comments
Browse files Browse the repository at this point in the history
  • Loading branch information
NyaaaWhatsUpDoc committed Sep 16, 2024
1 parent 2c32240 commit a1d4e12
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 17 deletions.
53 changes: 47 additions & 6 deletions internal/cache/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,52 @@ type DBCaches struct {

// FollowIDs provides access to the follower / following IDs database cache.
// THIS CACHE IS KEYED AS THE FOLLOWING {prefix}{accountID} WHERE PREFIX IS:
// - '>' for following IDs
// - 'l>' for local following IDs
// - '<' for follower IDs
// - 'l<' for local follower IDs
//
// - '>{$accountID}' for following IDs
// e.g. FollowIDs.Load(">" + account.ID, func() {})
// which will load a slice of follows IDs FROM account.
//
// - 'l>{$accountID}' for local following IDs
// e.g. FollowIDs.Load("l>" + account.ID, func() {})
// which will load a slice of LOCAL follows IDs FROM account.
//
// - '<{$accountID}' for follower IDs
// e.g. FollowIDs.Load("<" + account.ID, func() {})
// which will load a slice of follows IDs TARGETTING account.
//
// - 'l<{$accountID}' for local follower IDs
// e.g. FollowIDs.Load("l<" + account.ID, func() {})
// which will load a slice of LOCAL follows IDs TARGETTING account.
//
FollowIDs SliceCache[string]

// FollowRequest provides access to the gtsmodel FollowRequest database cache.
FollowRequest StructCache[*gtsmodel.FollowRequest]

// FollowRequestIDs provides access to the follow requester / requesting IDs database
// cache. THIS CACHE IS KEYED AS THE FOLLOWING {prefix}{accountID} WHERE PREFIX IS:
// - '>' for following IDs
// - '<' for follower IDs
//
// - '>{$accountID}' for follow request IDs
// e.g. FollowRequestIDs.Load(">" + account.ID, func() {})
// which will load a slice of follow request IDs TARGETTING account.
//
// - '<{$accountID}' for follow request IDs
// e.g. FollowRequestIDs.Load("<" + account.ID, func() {})
// which will load a slice of follow request IDs FROM account.
//
FollowRequestIDs SliceCache[string]

// FollowingTagIDs provides access to account IDs following / tag IDs followed by
// account db cache. THIS CACHE IS KEYED AS THE FOLLOWING {prefix}{id} WHERE:
//
// - '>{$accountID}' for tag IDs followed by account
// e.g. FollowingTagIDs.Load(">" + account.ID, func() {})
// which will load a slice of tag IDs followed by account.
//
// - '<{$tagIDs}' for account IDs following tag
// e.g. FollowingTagIDs.Load("<" + tag.ID, func() {})
// which will load a slice of account IDs following tag.
//
FollowingTagIDs SliceCache[string]

// Instance provides access to the gtsmodel Instance database cache.
Expand All @@ -120,14 +147,28 @@ type DBCaches struct {

// ListIDs provides access to the list IDs owned by account / list IDs follow
// contained in db cache. THIS CACHE IS KEYED AS FOLLOWING {prefix}{id} WHERE:
//
// - 'a{$accountID}' for list IDs owned by account
// e.g. ListIDs.Load("a" + account.ID, func() {})
// which will load a slice of list IDs owned by account.
//
// - 'f{$followID}' for list IDs follow contained in
// e.g. ListIDs.Load("f" + follow.ID, func() {})
// which will load a slice of list IDs containing follow.
//
ListIDs SliceCache[string]

// ListedIDs provides access to the account IDs in list / follow IDs in
// list db cache. THIS CACHE IS KEYED AS FOLLOWING {prefix}{id} WHERE:
//
// - 'a{listID}' for account IDs in list ID
// e.g. ListedIDs.Load("a" + list.ID, func() {})
// which will load a slice of account IDs in list.
//
// - 'f{listID}' for follow IDs in list ID
// e.g. ListedIDs.Load("f" + list.ID, func() {})
// which will load a slice of follow IDs in list.
//
ListedIDs SliceCache[string]

// Marker provides access to the gtsmodel Marker database cache.
Expand Down
36 changes: 25 additions & 11 deletions internal/cache/invalidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,28 @@ func (c *Caches) OnInvalidateFollow(follow *gtsmodel.Follow) {
c.DB.FollowIDs.Invalidate(

// Invalidate follow ID lists
// FROM the origin account
// TARGETTING origin account
// (including local-only follows).
">"+follow.AccountID,
"l>"+follow.AccountID,

// Invalidate follow ID lists
// TARGETTING origin account
// FROM the origin account
// (including local-only follows).
"<"+follow.AccountID,
"l<"+follow.AccountID,

// Invalidate follow ID lists
// FROM the target account
// (including local-only follows).
"<"+follow.TargetAccountID,
"l<"+follow.TargetAccountID,

// Invalidate follow ID lists
// TARGETTING the target account
// (including local-only follows).
">"+follow.TargetAccountID,
"l>"+follow.TargetAccountID,

// Invalidate follow ID lists
// FROM the target account
// (including local-only follows).
"<"+follow.TargetAccountID,
"l<"+follow.TargetAccountID,
)

// Invalidate ID slice cache.
Expand All @@ -153,13 +153,27 @@ func (c *Caches) OnInvalidateFollowRequest(followReq *gtsmodel.FollowRequest) {
// Invalidate follow with this same ID.
c.DB.Follow.Invalidate("ID", followReq.ID)

// Invalidate source account's followreq
// lists, and destinations follow req lists.
// (see FollowRequestIDs() comment for details).
// Invalidate ID slice cache.
c.DB.FollowRequestIDs.Invalidate(

// Invalidate follow request ID
// lists TARGETTING origin account
// (including local-only follows).
">"+followReq.AccountID,

// Invalidate follow request ID
// lists FROM the origin account
// (including local-only follows).
"<"+followReq.AccountID,

// Invalidate follow request ID
// lists TARGETTING target account
// (including local-only follows).
">"+followReq.TargetAccountID,

// Invalidate follow request ID
// lists FROM the target account
// (including local-only follows).
"<"+followReq.TargetAccountID,
)
}
Expand Down

0 comments on commit a1d4e12

Please sign in to comment.