diff --git a/internal/caching/cache_lazy_load_members.go b/internal/caching/cache_lazy_load_members.go index 37c1802cb..0d7009c94 100644 --- a/internal/caching/cache_lazy_load_members.go +++ b/internal/caching/cache_lazy_load_members.go @@ -4,61 +4,32 @@ import ( userapi "github.com/matrix-org/dendrite/userapi/api" ) +type lazyLoadingCacheKey struct { + UserID string // the user we're querying on behalf of + DeviceID string // the user we're querying on behalf of + RoomID string // the room in question + TargetUserID string // the user whose membership we're asking about +} + type LazyLoadCache interface { StoreLazyLoadedUser(device *userapi.Device, roomID, userID, eventID string) IsLazyLoadedUserCached(device *userapi.Device, roomID, userID string) (string, bool) } -func (c Caches) lazyLoadCacheForUser(device *userapi.Device) (*RistrettoCachePartition[string, any], error) { - return nil, nil - /* - cacheName := fmt.Sprintf("%s/%s", device.UserID, device.ID) - userCache, ok := c.LazyLoading.Get(cacheName) - if ok && userCache != nil { - if cache, ok := userCache.(*InMemoryLRUCachePartition); ok { - return cache, nil - } - } - cache, err := NewInMemoryLRUCachePartition( - LazyLoadCacheName, - LazyLoadCacheMutable, - LazyLoadCacheMaxUserEntries, - LazyLoadCacheMaxAge, - false, - ) - if err != nil { - return nil, err - } - c.LazyLoading.Set(cacheName, cache) - go cacheCleaner(cache) - return cache, nil - */ -} - func (c Caches) StoreLazyLoadedUser(device *userapi.Device, roomID, userID, eventID string) { - /* - cache, err := c.lazyLoadCacheForUser(device) - if err != nil { - return - } - cacheKey := fmt.Sprintf("%s/%s/%s/%s", device.UserID, device.ID, roomID, userID) - cache.Set(cacheKey, eventID) - */ + c.LazyLoading.Set(lazyLoadingCacheKey{ + UserID: device.UserID, + DeviceID: device.ID, + RoomID: roomID, + TargetUserID: userID, + }, eventID) } func (c Caches) IsLazyLoadedUserCached(device *userapi.Device, roomID, userID string) (string, bool) { - return "", false - /* - cache, err := c.lazyLoadCacheForUser(device) - if err != nil { - return "", false - } - - cacheKey := fmt.Sprintf("%s/%s/%s/%s", device.UserID, device.ID, roomID, userID) - val, ok := cache.Get(cacheKey) - if !ok { - return "", ok - } - return val.(string), ok - */ + return c.LazyLoading.Get(lazyLoadingCacheKey{ + UserID: device.UserID, + DeviceID: device.ID, + RoomID: roomID, + TargetUserID: userID, + }) } diff --git a/internal/caching/caches.go b/internal/caching/caches.go index 1a5133790..5bc851775 100644 --- a/internal/caching/caches.go +++ b/internal/caching/caches.go @@ -18,7 +18,7 @@ type Caches struct { FederationPDUs Cache[int64, *gomatrixserverlib.HeaderedEvent] FederationEDUs Cache[int64, *gomatrixserverlib.EDU] SpaceSummaryRooms Cache[string, gomatrixserverlib.MSC2946SpacesResponse] - LazyLoading Cache[string, any] + LazyLoading Cache[lazyLoadingCacheKey, string] } // Cache is the interface that an implementation must satisfy. @@ -30,7 +30,7 @@ type Cache[K keyable, T any] interface { type keyable interface { // from https://github.com/dgraph-io/ristretto/blob/8e850b710d6df0383c375ec6a7beae4ce48fc8d5/z/z.go#L34 - uint64 | string | []byte | byte | int | int32 | uint32 | int64 + uint64 | string | []byte | byte | int | int32 | uint32 | int64 | lazyLoadingCacheKey } type costable interface { diff --git a/internal/caching/impl_ristretto.go b/internal/caching/impl_ristretto.go index 4c5e5a6ef..6964d5ded 100644 --- a/internal/caching/impl_ristretto.go +++ b/internal/caching/impl_ristretto.go @@ -111,7 +111,7 @@ func NewRistrettoCache(maxCost config.DataUnit, maxAge time.Duration, enableProm Mutable: true, MaxAge: maxAge, }, - LazyLoading: &RistrettoCachePartition[string, any]{ // TODO: type + LazyLoading: &RistrettoCachePartition[lazyLoadingCacheKey, string]{ cache: cache, Prefix: lazyLoadingCache, Mutable: true,