mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-07 06:03:09 -06:00
Try caching events
This commit is contained in:
parent
1fd4ec1bb6
commit
c4c2c9d0ff
|
|
@ -4,13 +4,6 @@ import (
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"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
|
// FederationCache contains the subset of functions needed for
|
||||||
// a federation event cache.
|
// a federation event cache.
|
||||||
type FederationCache interface {
|
type FederationCache interface {
|
||||||
|
|
|
||||||
21
internal/caching/cache_roomevents.go
Normal file
21
internal/caching/cache_roomevents.go
Normal 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)
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
package caching
|
package caching
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/roomserver/types"
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -14,13 +12,6 @@ import (
|
||||||
// used from other components as we currently have no way to invalidate
|
// used from other components as we currently have no way to invalidate
|
||||||
// the cache in downstream components.
|
// the cache in downstream components.
|
||||||
|
|
||||||
const (
|
|
||||||
RoomInfoCacheName = "roominfo"
|
|
||||||
RoomInfoCacheMaxEntries = 1024
|
|
||||||
RoomInfoCacheMutable = true
|
|
||||||
RoomInfoCacheMaxAge = time.Minute * 5
|
|
||||||
)
|
|
||||||
|
|
||||||
// RoomInfosCache contains the subset of functions needed for
|
// RoomInfosCache contains the subset of functions needed for
|
||||||
// a room Info cache. It must only be used from the roomserver only
|
// a room Info cache. It must only be used from the roomserver only
|
||||||
// It is not safe for use from other components.
|
// It is not safe for use from other components.
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ type RoomServerCaches interface {
|
||||||
RoomServerNIDsCache
|
RoomServerNIDsCache
|
||||||
RoomVersionCache
|
RoomVersionCache
|
||||||
RoomInfoCache
|
RoomInfoCache
|
||||||
|
RoomServerEventsCache
|
||||||
}
|
}
|
||||||
|
|
||||||
// RoomServerNIDsCache contains the subset of functions needed for
|
// RoomServerNIDsCache contains the subset of functions needed for
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,6 @@ package caching
|
||||||
|
|
||||||
import "github.com/matrix-org/gomatrixserverlib"
|
import "github.com/matrix-org/gomatrixserverlib"
|
||||||
|
|
||||||
const (
|
|
||||||
RoomVersionCacheName = "room_versions"
|
|
||||||
RoomVersionCacheMaxEntries = 1024
|
|
||||||
RoomVersionCacheMutable = false
|
|
||||||
RoomVersionCacheMaxAge = CacheNoMaxAge
|
|
||||||
)
|
|
||||||
|
|
||||||
// RoomVersionsCache contains the subset of functions needed for
|
// RoomVersionsCache contains the subset of functions needed for
|
||||||
// a room version cache.
|
// a room version cache.
|
||||||
type RoomVersionCache interface {
|
type RoomVersionCache interface {
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,6 @@ import (
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
ServerKeyCacheName = "server_key"
|
|
||||||
ServerKeyCacheMaxEntries = 4096
|
|
||||||
ServerKeyCacheMutable = true
|
|
||||||
ServerKeyCacheMaxAge = CacheNoMaxAge
|
|
||||||
)
|
|
||||||
|
|
||||||
// ServerKeyCache contains the subset of functions needed for
|
// ServerKeyCache contains the subset of functions needed for
|
||||||
// a server key cache.
|
// a server key cache.
|
||||||
type ServerKeyCache interface {
|
type ServerKeyCache interface {
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,9 @@
|
||||||
package caching
|
package caching
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
SpaceSummaryRoomsCacheName = "space_summary_rooms"
|
|
||||||
SpaceSummaryRoomsCacheMaxEntries = 100
|
|
||||||
SpaceSummaryRoomsCacheMutable = true
|
|
||||||
SpaceSummaryRoomsCacheMaxAge = time.Minute * 5
|
|
||||||
)
|
|
||||||
|
|
||||||
type SpaceSummaryRoomsCache interface {
|
type SpaceSummaryRoomsCache interface {
|
||||||
GetSpaceSummary(roomID string) (r gomatrixserverlib.MSC2946SpacesResponse, ok bool)
|
GetSpaceSummary(roomID string) (r gomatrixserverlib.MSC2946SpacesResponse, ok bool)
|
||||||
StoreSpaceSummary(roomID string, r gomatrixserverlib.MSC2946SpacesResponse)
|
StoreSpaceSummary(roomID string, r gomatrixserverlib.MSC2946SpacesResponse)
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ type Caches struct {
|
||||||
ServerKeys Cache[string, gomatrixserverlib.PublicKeyLookupResult]
|
ServerKeys Cache[string, gomatrixserverlib.PublicKeyLookupResult]
|
||||||
RoomServerRoomNIDs Cache[string, types.RoomNID]
|
RoomServerRoomNIDs Cache[string, types.RoomNID]
|
||||||
RoomServerRoomIDs Cache[int64, string]
|
RoomServerRoomIDs Cache[int64, string]
|
||||||
|
RoomServerEvents Cache[int64, *gomatrixserverlib.Event]
|
||||||
RoomInfos Cache[string, types.RoomInfo]
|
RoomInfos Cache[string, types.RoomInfo]
|
||||||
FederationPDUs Cache[int64, *gomatrixserverlib.HeaderedEvent]
|
FederationPDUs Cache[int64, *gomatrixserverlib.HeaderedEvent]
|
||||||
FederationEDUs Cache[int64, *gomatrixserverlib.EDU]
|
FederationEDUs Cache[int64, *gomatrixserverlib.EDU]
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,10 @@ func NewRistrettoCache(maxCost CacheSize, enablePrometheus bool) (*Caches, error
|
||||||
cache: cache,
|
cache: cache,
|
||||||
Name: "room_ids",
|
Name: "room_ids",
|
||||||
},
|
},
|
||||||
|
RoomServerEvents: &RistrettoCachePartition[int64, *gomatrixserverlib.Event]{
|
||||||
|
cache: cache,
|
||||||
|
Name: "room_events",
|
||||||
|
},
|
||||||
RoomInfos: &RistrettoCachePartition[string, types.RoomInfo]{
|
RoomInfos: &RistrettoCachePartition[string, types.RoomInfo]{
|
||||||
cache: cache,
|
cache: cache,
|
||||||
Name: "room_infos",
|
Name: "room_infos",
|
||||||
|
|
|
||||||
|
|
@ -433,8 +433,20 @@ func (d *Database) Events(
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
) ([]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)
|
eventJSONs, err := d.EventJSONTable.BulkSelectEventJSON(ctx, txn, eventNIDs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -470,7 +482,6 @@ func (d *Database) events(
|
||||||
for n, v := range dbRoomVersions {
|
for n, v := range dbRoomVersions {
|
||||||
roomVersions[n] = v
|
roomVersions[n] = v
|
||||||
}
|
}
|
||||||
results := make([]types.Event, len(eventJSONs))
|
|
||||||
for i, eventJSON := range eventJSONs {
|
for i, eventJSON := range eventJSONs {
|
||||||
result := &results[i]
|
result := &results[i]
|
||||||
result.EventNID = eventJSON.EventNID
|
result.EventNID = eventJSON.EventNID
|
||||||
|
|
@ -482,6 +493,7 @@ func (d *Database) events(
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
d.Cache.StoreRoomServerEvent(result.EventNID, result.Event)
|
||||||
}
|
}
|
||||||
if !redactionsArePermanent {
|
if !redactionsArePermanent {
|
||||||
d.applyRedactions(results)
|
d.applyRedactions(results)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue