diff options
Diffstat (limited to 'internal/cache')
| -rw-r--r-- | internal/cache/cache.go | 1 | ||||
| -rw-r--r-- | internal/cache/db.go | 48 | ||||
| -rw-r--r-- | internal/cache/invalidate.go | 23 | ||||
| -rw-r--r-- | internal/cache/size.go | 20 |
4 files changed, 58 insertions, 34 deletions
diff --git a/internal/cache/cache.go b/internal/cache/cache.go index 54777441f..d3d2d5f2b 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -86,6 +86,7 @@ func (c *Caches) Init() { c.initDomainPermissionExclude() c.initEmoji() c.initEmojiCategory() + c.initFilterIDs() c.initFilter() c.initFilterKeyword() c.initFilterStatus() diff --git a/internal/cache/db.go b/internal/cache/db.go index 5592ca493..6b482d5f8 100644 --- a/internal/cache/db.go +++ b/internal/cache/db.go @@ -82,6 +82,10 @@ type DBCaches struct { // Filter provides access to the gtsmodel Filter database cache. Filter StructCache[*gtsmodel.Filter] + // FilterIDs provides access to the filter IDs database cache. + // This cache is keyed as: {accountID} -> []{filterIDs}. + FilterIDs SliceCache[string] + // FilterKeyword provides access to the gtsmodel FilterKeyword database cache. FilterKeyword StructCache[*gtsmodel.FilterKeyword] @@ -691,12 +695,23 @@ func (c *Caches) initFilter() { {Fields: "ID"}, {Fields: "AccountID", Multiple: true}, }, - MaxSize: cap, - IgnoreErr: ignoreErrors, - Copy: copyF, + MaxSize: cap, + IgnoreErr: ignoreErrors, + Copy: copyF, + Invalidate: c.OnInvalidateFilter, }) } +func (c *Caches) initFilterIDs() { + cap := calculateSliceCacheMax( + config.GetCacheFilterIDsMemRatio(), + ) + + log.Infof(nil, "cache size = %d", cap) + + c.DB.FilterIDs.Init(0, cap) +} + func (c *Caches) initFilterKeyword() { // Calculate maximum cache size. cap := calculateResultCacheMax( @@ -710,11 +725,6 @@ func (c *Caches) initFilterKeyword() { filterKeyword2 := new(gtsmodel.FilterKeyword) *filterKeyword2 = *filterKeyword1 - // Don't include ptr fields that - // will be populated separately. - // See internal/db/bundb/filter.go. - filterKeyword2.Filter = nil - // We specifically DO NOT unset // the regexp field here, as any // regexp.Regexp instance is safe @@ -726,12 +736,12 @@ func (c *Caches) initFilterKeyword() { c.DB.FilterKeyword.Init(structr.CacheConfig[*gtsmodel.FilterKeyword]{ Indices: []structr.IndexConfig{ {Fields: "ID"}, - {Fields: "AccountID", Multiple: true}, {Fields: "FilterID", Multiple: true}, }, - MaxSize: cap, - IgnoreErr: ignoreErrors, - Copy: copyF, + MaxSize: cap, + IgnoreErr: ignoreErrors, + Copy: copyF, + Invalidate: c.OnInvalidateFilterKeyword, }) } @@ -747,24 +757,18 @@ func (c *Caches) initFilterStatus() { copyF := func(filterStatus1 *gtsmodel.FilterStatus) *gtsmodel.FilterStatus { filterStatus2 := new(gtsmodel.FilterStatus) *filterStatus2 = *filterStatus1 - - // Don't include ptr fields that - // will be populated separately. - // See internal/db/bundb/filter.go. - filterStatus2.Filter = nil - return filterStatus2 } c.DB.FilterStatus.Init(structr.CacheConfig[*gtsmodel.FilterStatus]{ Indices: []structr.IndexConfig{ {Fields: "ID"}, - {Fields: "AccountID", Multiple: true}, {Fields: "FilterID", Multiple: true}, }, - MaxSize: cap, - IgnoreErr: ignoreErrors, - Copy: copyF, + MaxSize: cap, + IgnoreErr: ignoreErrors, + Copy: copyF, + Invalidate: c.OnInvalidateFilterStatus, }) } diff --git a/internal/cache/invalidate.go b/internal/cache/invalidate.go index 88b7415ae..4941b2540 100644 --- a/internal/cache/invalidate.go +++ b/internal/cache/invalidate.go @@ -112,6 +112,29 @@ func (c *Caches) OnInvalidateEmojiCategory(category *gtsmodel.EmojiCategory) { c.DB.Emoji.Invalidate("CategoryID", category.ID) } +func (c *Caches) OnInvalidateFilter(filter *gtsmodel.Filter) { + // Invalidate list of filters for account. + c.DB.FilterIDs.Invalidate(filter.AccountID) + + // Invalidate all associated keywords and statuses. + c.DB.FilterKeyword.InvalidateIDs("ID", filter.KeywordIDs) + c.DB.FilterStatus.InvalidateIDs("ID", filter.StatusIDs) + + // Invalidate account's timelines (in case local). + c.Timelines.Home.Unprepare(filter.AccountID) + c.Timelines.List.Unprepare(filter.AccountID) +} + +func (c *Caches) OnInvalidateFilterKeyword(filterKeyword *gtsmodel.FilterKeyword) { + // Invalidate filter that keyword associated with. + c.DB.Filter.Invalidate("ID", filterKeyword.FilterID) +} + +func (c *Caches) OnInvalidateFilterStatus(filterStatus *gtsmodel.FilterStatus) { + // Invalidate filter that status associated with. + c.DB.Filter.Invalidate("ID", filterStatus.FilterID) +} + func (c *Caches) OnInvalidateFollow(follow *gtsmodel.Follow) { // Invalidate follow request with this same ID. c.DB.FollowRequest.Invalidate("ID", follow.ID) diff --git a/internal/cache/size.go b/internal/cache/size.go index ef9259f88..8a6c9e9ad 100644 --- a/internal/cache/size.go +++ b/internal/cache/size.go @@ -19,6 +19,7 @@ package cache import ( "crypto/rsa" + "regexp" "strings" "time" "unsafe" @@ -348,8 +349,6 @@ func sizeofEmojiCategory() uintptr { func sizeofFilter() uintptr { return uintptr(size.Of(>smodel.Filter{ ID: exampleID, - CreatedAt: exampleTime, - UpdatedAt: exampleTime, ExpiresAt: exampleTime, AccountID: exampleID, Title: exampleTextSmall, @@ -359,21 +358,18 @@ func sizeofFilter() uintptr { func sizeofFilterKeyword() uintptr { return uintptr(size.Of(>smodel.FilterKeyword{ - ID: exampleID, - CreatedAt: exampleTime, - UpdatedAt: exampleTime, - FilterID: exampleID, - Keyword: exampleTextSmall, + ID: exampleID, + FilterID: exampleID, + Keyword: exampleTextSmall, + Regexp: regexp.MustCompile("^match (this)? .*"), })) } func sizeofFilterStatus() uintptr { return uintptr(size.Of(>smodel.FilterStatus{ - ID: exampleID, - CreatedAt: exampleTime, - UpdatedAt: exampleTime, - FilterID: exampleID, - StatusID: exampleID, + ID: exampleID, + FilterID: exampleID, + StatusID: exampleID, })) } |
