Try caching events

This commit is contained in:
Neil Alexander 2022-06-14 16:45:13 +01:00
parent 1fd4ec1bb6
commit c4c2c9d0ff
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
10 changed files with 41 additions and 41 deletions

View file

@ -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 {

View file

@ -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)
}

View file

@ -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.

View file

@ -15,6 +15,7 @@ type RoomServerCaches interface {
RoomServerNIDsCache
RoomVersionCache
RoomInfoCache
RoomServerEventsCache
}
// RoomServerNIDsCache contains the subset of functions needed for

View file

@ -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 {

View file

@ -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 {

View file

@ -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)

View file

@ -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]

View file

@ -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",

View file

@ -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)