diff options
Diffstat (limited to 'internal/cache/account.go')
-rw-r--r-- | internal/cache/account.go | 118 |
1 files changed, 34 insertions, 84 deletions
diff --git a/internal/cache/account.go b/internal/cache/account.go index 8dbb07848..474afbe44 100644 --- a/internal/cache/account.go +++ b/internal/cache/account.go @@ -1,103 +1,62 @@ package cache import ( - "sync" + "time" - "github.com/ReneKroon/ttlcache" - "github.com/sirupsen/logrus" + "codeberg.org/gruf/go-cache/v2" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -// AccountCache is a wrapper around ttlcache.Cache to provide URL and URI lookups for gtsmodel.Account +// AccountCache is a cache wrapper to provide URL and URI lookups for gtsmodel.Account type AccountCache struct { - cache *ttlcache.Cache // map of IDs -> cached accounts - urls map[string]string // map of account URLs -> IDs - uris map[string]string // map of account URIs -> IDs - mutex sync.Mutex + cache cache.LookupCache[string, string, *gtsmodel.Account] } // NewAccountCache returns a new instantiated AccountCache object func NewAccountCache() *AccountCache { - c := AccountCache{ - cache: ttlcache.NewCache(), - urls: make(map[string]string, 100), - uris: make(map[string]string, 100), - mutex: sync.Mutex{}, - } - - // Set callback to purge lookup maps on expiration - c.cache.SetExpirationCallback(func(key string, value interface{}) { - account, ok := value.(*gtsmodel.Account) - if !ok { - logrus.Panicf("AccountCache could not assert entry with key %s to *gtsmodel.Account", key) - } - - c.mutex.Lock() - delete(c.urls, account.URL) - delete(c.uris, account.URI) - c.mutex.Unlock() + c := &AccountCache{} + c.cache = cache.NewLookup(cache.LookupCfg[string, string, *gtsmodel.Account]{ + RegisterLookups: func(lm *cache.LookupMap[string, string]) { + lm.RegisterLookup("uri") + lm.RegisterLookup("url") + }, + + AddLookups: func(lm *cache.LookupMap[string, string], acc *gtsmodel.Account) { + if uri := acc.URI; uri != "" { + lm.Set("uri", uri, acc.ID) + } + if url := acc.URL; url != "" { + lm.Set("url", url, acc.ID) + } + }, + + DeleteLookups: func(lm *cache.LookupMap[string, string], acc *gtsmodel.Account) { + if uri := acc.URI; uri != "" { + lm.Delete("uri", uri) + } + if url := acc.URL; url != "" { + lm.Delete("url", url) + } + }, }) - - return &c + c.cache.SetTTL(time.Minute*5, false) + c.cache.Start(time.Second * 10) + return c } // GetByID attempts to fetch a account from the cache by its ID, you will receive a copy for thread-safety func (c *AccountCache) GetByID(id string) (*gtsmodel.Account, bool) { - c.mutex.Lock() - account, ok := c.getByID(id) - c.mutex.Unlock() - return account, ok + return c.cache.Get(id) } // GetByURL attempts to fetch a account from the cache by its URL, you will receive a copy for thread-safety func (c *AccountCache) GetByURL(url string) (*gtsmodel.Account, bool) { - // Perform safe ID lookup - c.mutex.Lock() - id, ok := c.urls[url] - - // Not found, unlock early - if !ok { - c.mutex.Unlock() - return nil, false - } - - // Attempt account lookup - account, ok := c.getByID(id) - c.mutex.Unlock() - return account, ok + return c.cache.GetBy("url", url) } // GetByURI attempts to fetch a account from the cache by its URI, you will receive a copy for thread-safety func (c *AccountCache) GetByURI(uri string) (*gtsmodel.Account, bool) { - // Perform safe ID lookup - c.mutex.Lock() - id, ok := c.uris[uri] - - // Not found, unlock early - if !ok { - c.mutex.Unlock() - return nil, false - } - - // Attempt account lookup - account, ok := c.getByID(id) - c.mutex.Unlock() - return account, ok -} - -// getByID performs an unsafe (no mutex locks) lookup of account by ID, returning a copy of account in cache -func (c *AccountCache) getByID(id string) (*gtsmodel.Account, bool) { - v, ok := c.cache.Get(id) - if !ok { - return nil, false - } - - a, ok := v.(*gtsmodel.Account) - if !ok { - panic("account cache entry was not an account") - } - - return copyAccount(a), true + return c.cache.GetBy("uri", uri) } // Put places a account in the cache, ensuring that the object place is a copy for thread-safety @@ -105,16 +64,7 @@ func (c *AccountCache) Put(account *gtsmodel.Account) { if account == nil || account.ID == "" { panic("invalid account") } - - c.mutex.Lock() c.cache.Set(account.ID, copyAccount(account)) - if account.URL != "" { - c.urls[account.URL] = account.ID - } - if account.URI != "" { - c.uris[account.URI] = account.ID - } - c.mutex.Unlock() } // copyAccount performs a surface-level copy of account, only keeping attached IDs intact, not the objects. |