diff --git a/internal/caching/cache_federationevents.go b/internal/caching/cache_federationevents.go index 6ace29760..24af51bdc 100644 --- a/internal/caching/cache_federationevents.go +++ b/internal/caching/cache_federationevents.go @@ -4,13 +4,6 @@ import ( "github.com/matrix-org/gomatrixserverlib" ) -const ( - FederationEventCacheName = "federation_event" - FederationEventCacheMaxEntries = 256 - FederationEventCacheMutable = true // to allow use of Unset only - FederationEventCacheMaxAge = CacheNoMaxAge -) - // FederationCache contains the subset of functions needed for // a federation event cache. type FederationCache interface { diff --git a/internal/caching/cache_roomevents.go b/internal/caching/cache_roomevents.go new file mode 100644 index 000000000..8afef42db --- /dev/null +++ b/internal/caching/cache_roomevents.go @@ -0,0 +1,21 @@ +package caching + +import ( + "github.com/matrix-org/dendrite/roomserver/types" + "github.com/matrix-org/gomatrixserverlib" +) + +// RoomServerNIDsCache contains the subset of functions needed for +// a roomserver NID cache. +type RoomServerEventsCache interface { + GetRoomServerEvent(eventNID types.EventNID) (*gomatrixserverlib.Event, bool) + StoreRoomServerEvent(eventNID types.EventNID, event *gomatrixserverlib.Event) +} + +func (c Caches) GetRoomServerEvent(eventNID types.EventNID) (*gomatrixserverlib.Event, bool) { + return c.RoomServerEvents.Get(int64(eventNID)) +} + +func (c Caches) StoreRoomServerEvent(eventNID types.EventNID, event *gomatrixserverlib.Event) { + c.RoomServerEvents.Set(int64(eventNID), event) +} diff --git a/internal/caching/cache_roominfo.go b/internal/caching/cache_roominfo.go index 480973f68..d03a61077 100644 --- a/internal/caching/cache_roominfo.go +++ b/internal/caching/cache_roominfo.go @@ -1,8 +1,6 @@ package caching import ( - "time" - "github.com/matrix-org/dendrite/roomserver/types" ) @@ -14,13 +12,6 @@ import ( // used from other components as we currently have no way to invalidate // the cache in downstream components. -const ( - RoomInfoCacheName = "roominfo" - RoomInfoCacheMaxEntries = 1024 - RoomInfoCacheMutable = true - RoomInfoCacheMaxAge = time.Minute * 5 -) - // RoomInfosCache contains the subset of functions needed for // a room Info cache. It must only be used from the roomserver only // It is not safe for use from other components. diff --git a/internal/caching/cache_roomservernids.go b/internal/caching/cache_roomservernids.go index da6d19b5a..dcdf8c970 100644 --- a/internal/caching/cache_roomservernids.go +++ b/internal/caching/cache_roomservernids.go @@ -15,6 +15,7 @@ type RoomServerCaches interface { RoomServerNIDsCache RoomVersionCache RoomInfoCache + RoomServerEventsCache } // RoomServerNIDsCache contains the subset of functions needed for diff --git a/internal/caching/cache_roomversions.go b/internal/caching/cache_roomversions.go index af4258008..afc3d36da 100644 --- a/internal/caching/cache_roomversions.go +++ b/internal/caching/cache_roomversions.go @@ -2,13 +2,6 @@ package caching import "github.com/matrix-org/gomatrixserverlib" -const ( - RoomVersionCacheName = "room_versions" - RoomVersionCacheMaxEntries = 1024 - RoomVersionCacheMutable = false - RoomVersionCacheMaxAge = CacheNoMaxAge -) - // RoomVersionsCache contains the subset of functions needed for // a room version cache. type RoomVersionCache interface { diff --git a/internal/caching/cache_serverkeys.go b/internal/caching/cache_serverkeys.go index 836272502..cffa101d5 100644 --- a/internal/caching/cache_serverkeys.go +++ b/internal/caching/cache_serverkeys.go @@ -6,13 +6,6 @@ import ( "github.com/matrix-org/gomatrixserverlib" ) -const ( - ServerKeyCacheName = "server_key" - ServerKeyCacheMaxEntries = 4096 - ServerKeyCacheMutable = true - ServerKeyCacheMaxAge = CacheNoMaxAge -) - // ServerKeyCache contains the subset of functions needed for // a server key cache. type ServerKeyCache interface { diff --git a/internal/caching/cache_space_rooms.go b/internal/caching/cache_space_rooms.go index cfb8de5e4..697f99269 100644 --- a/internal/caching/cache_space_rooms.go +++ b/internal/caching/cache_space_rooms.go @@ -1,18 +1,9 @@ package caching import ( - "time" - "github.com/matrix-org/gomatrixserverlib" ) -const ( - SpaceSummaryRoomsCacheName = "space_summary_rooms" - SpaceSummaryRoomsCacheMaxEntries = 100 - SpaceSummaryRoomsCacheMutable = true - SpaceSummaryRoomsCacheMaxAge = time.Minute * 5 -) - type SpaceSummaryRoomsCache interface { GetSpaceSummary(roomID string) (r gomatrixserverlib.MSC2946SpacesResponse, ok bool) StoreSpaceSummary(roomID string, r gomatrixserverlib.MSC2946SpacesResponse) diff --git a/internal/caching/caches.go b/internal/caching/caches.go index 1b456717c..2c5ef682e 100644 --- a/internal/caching/caches.go +++ b/internal/caching/caches.go @@ -15,6 +15,7 @@ type Caches struct { ServerKeys Cache[string, gomatrixserverlib.PublicKeyLookupResult] RoomServerRoomNIDs Cache[string, types.RoomNID] RoomServerRoomIDs Cache[int64, string] + RoomServerEvents Cache[int64, *gomatrixserverlib.Event] RoomInfos Cache[string, types.RoomInfo] FederationPDUs Cache[int64, *gomatrixserverlib.HeaderedEvent] FederationEDUs Cache[int64, *gomatrixserverlib.EDU] diff --git a/internal/caching/impl_ristretto.go b/internal/caching/impl_ristretto.go index 0251504b4..b55785136 100644 --- a/internal/caching/impl_ristretto.go +++ b/internal/caching/impl_ristretto.go @@ -51,6 +51,10 @@ func NewRistrettoCache(maxCost CacheSize, enablePrometheus bool) (*Caches, error cache: cache, Name: "room_ids", }, + RoomServerEvents: &RistrettoCachePartition[int64, *gomatrixserverlib.Event]{ + cache: cache, + Name: "room_events", + }, RoomInfos: &RistrettoCachePartition[string, types.RoomInfo]{ cache: cache, Name: "room_infos", diff --git a/roomserver/storage/shared/storage.go b/roomserver/storage/shared/storage.go index 67dcfdf38..cbb1f0d64 100644 --- a/roomserver/storage/shared/storage.go +++ b/roomserver/storage/shared/storage.go @@ -433,8 +433,20 @@ func (d *Database) Events( } func (d *Database) events( - ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID, + ctx context.Context, txn *sql.Tx, inputEventNIDs []types.EventNID, ) ([]types.Event, error) { + results := make([]types.Event, len(inputEventNIDs)) + eventNIDs := make([]types.EventNID, 0, len(results)) + for _, nid := range eventNIDs { + if event, ok := d.Cache.GetRoomServerEvent(nid); ok { + results = append(results, types.Event{ + EventNID: nid, + Event: event, + }) + } else { + eventNIDs = append(eventNIDs, nid) + } + } eventJSONs, err := d.EventJSONTable.BulkSelectEventJSON(ctx, txn, eventNIDs) if err != nil { return nil, err @@ -470,7 +482,6 @@ func (d *Database) events( for n, v := range dbRoomVersions { roomVersions[n] = v } - results := make([]types.Event, len(eventJSONs)) for i, eventJSON := range eventJSONs { result := &results[i] result.EventNID = eventJSON.EventNID @@ -482,6 +493,7 @@ func (d *Database) events( if err != nil { return nil, err } + d.Cache.StoreRoomServerEvent(result.EventNID, result.Event) } if !redactionsArePermanent { d.applyRedactions(results)