summaryrefslogtreecommitdiff
path: root/internal/cache/status.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2022-07-10 16:18:21 +0100
committerLibravatar GitHub <noreply@github.com>2022-07-10 17:18:21 +0200
commit7cc40302a578264dff4edb3b007fc192b840c7d4 (patch)
tree82d78254373f1c18fdd6271aa324100cedaacc2d /internal/cache/status.go
parent[bugfix] Fix display names in thread view causing wrapping issues on small sc... (diff)
downloadgotosocial-7cc40302a578264dff4edb3b007fc192b840c7d4.tar.xz
[chore] consolidate caching libraries (#704)
* add miekg/dns dependency * set/validate accountDomain * move finger to dereferencer * totally break GetRemoteAccount * start reworking finger func a bit * start reworking getRemoteAccount a bit * move mention parts to namestring * rework webfingerget * use util function to extract webfinger parts * use accountDomain * rework finger again, final form * just a real nasty commit, the worst * remove refresh from account * use new ASRepToAccount signature * fix incorrect debug call * fix for new getRemoteAccount * rework GetRemoteAccount * start updating tests to remove repetition * break a lot of tests Move shared test logic into the testrig, rather than having it scattered all over the place. This allows us to just mock the transport controller once, and have all tests use it (unless they need not to for some other reason). * fix up tests to use main mock httpclient * webfinger only if necessary * cheeky linting with the lads * update mentionName regex recognize instance accounts * don't finger instance accounts * test webfinger part extraction * increase default worker count to 4 per cpu * don't repeat regex parsing * final search for discovered accountDomain * be more permissive in namestring lookup * add more extraction tests * simplify GetParseMentionFunc * skip long search if local account * fix broken test * consolidate to all use same caching libraries Signed-off-by: kim <grufwub@gmail.com> * perform more caching in the database layer Signed-off-by: kim <grufwub@gmail.com> * remove ASNote cache Signed-off-by: kim <grufwub@gmail.com> * update cache library, improve db tracing hooks Signed-off-by: kim <grufwub@gmail.com> * return ErrNoEntries if no account status IDs found, small formatting changes Signed-off-by: kim <grufwub@gmail.com> * fix tests, thanks tobi! Signed-off-by: kim <grufwub@gmail.com> Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
Diffstat (limited to 'internal/cache/status.go')
-rw-r--r--internal/cache/status.go118
1 files changed, 34 insertions, 84 deletions
diff --git a/internal/cache/status.go b/internal/cache/status.go
index 9a698c608..6c0b5aa9f 100644
--- a/internal/cache/status.go
+++ b/internal/cache/status.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"
)
-// StatusCache is a wrapper around ttlcache.Cache to provide URL and URI lookups for gtsmodel.Status
+// StatusCache is a cache wrapper to provide URL and URI lookups for gtsmodel.Status
type StatusCache struct {
- cache *ttlcache.Cache // map of IDs -> cached statuses
- urls map[string]string // map of status URLs -> IDs
- uris map[string]string // map of status URIs -> IDs
- mutex sync.Mutex
+ cache cache.LookupCache[string, string, *gtsmodel.Status]
}
// NewStatusCache returns a new instantiated statusCache object
func NewStatusCache() *StatusCache {
- c := StatusCache{
- 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{}) {
- status, ok := value.(*gtsmodel.Status)
- if !ok {
- logrus.Panicf("StatusCache could not assert entry with key %s to *gtsmodel.Status", key)
- }
-
- c.mutex.Lock()
- delete(c.urls, status.URL)
- delete(c.uris, status.URI)
- c.mutex.Unlock()
+ c := &StatusCache{}
+ c.cache = cache.NewLookup(cache.LookupCfg[string, string, *gtsmodel.Status]{
+ RegisterLookups: func(lm *cache.LookupMap[string, string]) {
+ lm.RegisterLookup("uri")
+ lm.RegisterLookup("url")
+ },
+
+ AddLookups: func(lm *cache.LookupMap[string, string], status *gtsmodel.Status) {
+ if uri := status.URI; uri != "" {
+ lm.Set("uri", uri, status.ID)
+ }
+ if url := status.URL; url != "" {
+ lm.Set("url", url, status.ID)
+ }
+ },
+
+ DeleteLookups: func(lm *cache.LookupMap[string, string], status *gtsmodel.Status) {
+ if uri := status.URI; uri != "" {
+ lm.Delete("uri", uri)
+ }
+ if url := status.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 status from the cache by its ID, you will receive a copy for thread-safety
func (c *StatusCache) GetByID(id string) (*gtsmodel.Status, bool) {
- c.mutex.Lock()
- status, ok := c.getByID(id)
- c.mutex.Unlock()
- return status, ok
+ return c.cache.Get(id)
}
// GetByURL attempts to fetch a status from the cache by its URL, you will receive a copy for thread-safety
func (c *StatusCache) GetByURL(url string) (*gtsmodel.Status, 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 status lookup
- status, ok := c.getByID(id)
- c.mutex.Unlock()
- return status, ok
+ return c.cache.GetBy("url", url)
}
// GetByURI attempts to fetch a status from the cache by its URI, you will receive a copy for thread-safety
func (c *StatusCache) GetByURI(uri string) (*gtsmodel.Status, 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 status lookup
- status, ok := c.getByID(id)
- c.mutex.Unlock()
- return status, ok
-}
-
-// getByID performs an unsafe (no mutex locks) lookup of status by ID, returning a copy of status in cache
-func (c *StatusCache) getByID(id string) (*gtsmodel.Status, bool) {
- v, ok := c.cache.Get(id)
- if !ok {
- return nil, false
- }
-
- s, ok := v.(*gtsmodel.Status)
- if !ok {
- panic("status cache entry was not a status")
- }
-
- return copyStatus(s), true
+ return c.cache.GetBy("uri", uri)
}
// Put places a status in the cache, ensuring that the object place is a copy for thread-safety
@@ -105,16 +64,7 @@ func (c *StatusCache) Put(status *gtsmodel.Status) {
if status == nil || status.ID == "" {
panic("invalid status")
}
-
- c.mutex.Lock()
c.cache.Set(status.ID, copyStatus(status))
- if status.URL != "" {
- c.urls[status.URL] = status.ID
- }
- if status.URI != "" {
- c.uris[status.URI] = status.ID
- }
- c.mutex.Unlock()
}
// copyStatus performs a surface-level copy of status, only keeping attached IDs intact, not the objects.