mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-10-31 22:18:52 +00:00
improved wrapping of the SliceCache type
This commit is contained in:
parent
e7ea0b4ce3
commit
7e7b877a39
3 changed files with 37 additions and 18 deletions
8
internal/cache/invalidate.go
vendored
8
internal/cache/invalidate.go
vendored
|
@ -37,7 +37,7 @@ func (c *Caches) OnInvalidateAccount(account *gtsmodel.Account) {
|
|||
// Invalidate this account's
|
||||
// following / follower lists.
|
||||
// (see FollowIDs() comment for details).
|
||||
c.GTS.FollowIDs.InvalidateAll(
|
||||
c.GTS.FollowIDs.Invalidate(
|
||||
">"+account.ID,
|
||||
"l>"+account.ID,
|
||||
"<"+account.ID,
|
||||
|
@ -47,7 +47,7 @@ func (c *Caches) OnInvalidateAccount(account *gtsmodel.Account) {
|
|||
// Invalidate this account's
|
||||
// follow requesting / request lists.
|
||||
// (see FollowRequestIDs() comment for details).
|
||||
c.GTS.FollowRequestIDs.InvalidateAll(
|
||||
c.GTS.FollowRequestIDs.Invalidate(
|
||||
">"+account.ID,
|
||||
"<"+account.ID,
|
||||
)
|
||||
|
@ -96,7 +96,7 @@ func (c *Caches) OnInvalidateFollow(follow *gtsmodel.Follow) {
|
|||
// Invalidate source account's following
|
||||
// lists, and destination's follwer lists.
|
||||
// (see FollowIDs() comment for details).
|
||||
c.GTS.FollowIDs.InvalidateAll(
|
||||
c.GTS.FollowIDs.Invalidate(
|
||||
">"+follow.AccountID,
|
||||
"l>"+follow.AccountID,
|
||||
"<"+follow.AccountID,
|
||||
|
@ -115,7 +115,7 @@ func (c *Caches) OnInvalidateFollowRequest(followReq *gtsmodel.FollowRequest) {
|
|||
// Invalidate source account's followreq
|
||||
// lists, and destinations follow req lists.
|
||||
// (see FollowRequestIDs() comment for details).
|
||||
c.GTS.FollowRequestIDs.InvalidateAll(
|
||||
c.GTS.FollowRequestIDs.Invalidate(
|
||||
">"+followReq.AccountID,
|
||||
"<"+followReq.AccountID,
|
||||
">"+followReq.TargetAccountID,
|
||||
|
|
41
internal/cache/wrappers.go
vendored
41
internal/cache/wrappers.go
vendored
|
@ -27,19 +27,19 @@ import (
|
|||
// SliceCache wraps a simple.Cache to provide simple loader-callback
|
||||
// functions for fetching + caching slices of objects (e.g. IDs).
|
||||
type SliceCache[T any] struct {
|
||||
simple.Cache[string, []T]
|
||||
cache simple.Cache[string, []T]
|
||||
}
|
||||
|
||||
// Init ...
|
||||
func (c *SliceCache[T]) Init(len, cap int) {
|
||||
c.Cache = simple.Cache[string, []T]{}
|
||||
c.Cache.Init(len, cap)
|
||||
c.cache = simple.Cache[string, []T]{}
|
||||
c.cache.Init(len, cap)
|
||||
}
|
||||
|
||||
// Load will attempt to load an existing slice from the cache for the given key, else calling the provided load function and caching the result.
|
||||
func (c *SliceCache[T]) Load(key string, load func() ([]T, error)) ([]T, error) {
|
||||
// Look for follow IDs list in cache under this key.
|
||||
data, ok := c.Get(key)
|
||||
// Look for cached values.
|
||||
data, ok := c.cache.Get(key)
|
||||
|
||||
if !ok {
|
||||
var err error
|
||||
|
@ -51,13 +51,38 @@ func (c *SliceCache[T]) Load(key string, load func() ([]T, error)) ([]T, error)
|
|||
}
|
||||
|
||||
// Store the data.
|
||||
c.Set(key, data)
|
||||
c.cache.Set(key, data)
|
||||
}
|
||||
|
||||
// Return data clone for safety.
|
||||
return slices.Clone(data), nil
|
||||
}
|
||||
|
||||
// Invalidate ...
|
||||
func (c *SliceCache[T]) Invalidate(keys ...string) {
|
||||
_ = c.cache.InvalidateAll(keys...)
|
||||
}
|
||||
|
||||
// Trim ...
|
||||
func (c *SliceCache[T]) Trim(perc float64) {
|
||||
c.cache.Trim(perc)
|
||||
}
|
||||
|
||||
// Clear ...
|
||||
func (c *SliceCache[T]) Clear() {
|
||||
c.cache.Clear()
|
||||
}
|
||||
|
||||
// Len ...
|
||||
func (c *SliceCache[T]) Len() int {
|
||||
return c.cache.Len()
|
||||
}
|
||||
|
||||
// Cap ...
|
||||
func (c *SliceCache[T]) Cap() int {
|
||||
return c.cache.Cap()
|
||||
}
|
||||
|
||||
// StructCache ...
|
||||
type StructCache[StructType any] struct {
|
||||
cache structr.Cache[StructType]
|
||||
|
@ -154,10 +179,6 @@ func (c *StructCache[T]) InvalidateIDs(index string, ids []string) {
|
|||
c.cache.Invalidate(i, keys...)
|
||||
}
|
||||
|
||||
func (c *StructCache[T]) with(index string, with func(index *structr.Index)) {
|
||||
with(c.index[index])
|
||||
}
|
||||
|
||||
// Trim ...
|
||||
func (c *StructCache[T]) Trim(perc float64) {
|
||||
c.cache.Trim(perc)
|
||||
|
|
|
@ -308,10 +308,8 @@ func (s *statusFaveDB) DeleteStatusFaves(ctx context.Context, targetAccountID st
|
|||
// Invalidate any cached status faves for this status ID.
|
||||
s.state.Caches.GTS.StatusFave.InvalidateIDs("ID", statusIDs)
|
||||
|
||||
for _, id := range statusIDs {
|
||||
// Invalidate any cached status fave IDs for this status.
|
||||
s.state.Caches.GTS.StatusFaveIDs.Invalidate(id)
|
||||
}
|
||||
// Invalidate any cached status fave IDs for this status ID.
|
||||
s.state.Caches.GTS.StatusFaveIDs.Invalidate(statusIDs...)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue