2020-12-04 15:52:10 +01:00
|
|
|
package caching
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
FederationEventCacheName = "federation_event"
|
|
|
|
FederationEventCacheMaxEntries = 256
|
|
|
|
FederationEventCacheMutable = true // to allow use of Unset only
|
2022-03-01 17:59:52 +01:00
|
|
|
FederationEventCacheMaxAge = CacheNoMaxAge
|
2020-12-04 15:52:10 +01:00
|
|
|
)
|
|
|
|
|
2021-11-24 11:45:23 +01:00
|
|
|
// FederationCache contains the subset of functions needed for
|
2020-12-04 15:52:10 +01:00
|
|
|
// a federation event cache.
|
2021-11-24 11:45:23 +01:00
|
|
|
type FederationCache interface {
|
|
|
|
GetFederationQueuedPDU(eventNID int64) (event *gomatrixserverlib.HeaderedEvent, ok bool)
|
|
|
|
StoreFederationQueuedPDU(eventNID int64, event *gomatrixserverlib.HeaderedEvent)
|
|
|
|
EvictFederationQueuedPDU(eventNID int64)
|
2020-12-04 15:52:10 +01:00
|
|
|
|
2021-11-24 11:45:23 +01:00
|
|
|
GetFederationQueuedEDU(eventNID int64) (event *gomatrixserverlib.EDU, ok bool)
|
|
|
|
StoreFederationQueuedEDU(eventNID int64, event *gomatrixserverlib.EDU)
|
|
|
|
EvictFederationQueuedEDU(eventNID int64)
|
2020-12-04 15:52:10 +01:00
|
|
|
}
|
|
|
|
|
2021-11-24 11:45:23 +01:00
|
|
|
func (c Caches) GetFederationQueuedPDU(eventNID int64) (*gomatrixserverlib.HeaderedEvent, bool) {
|
2020-12-04 15:52:10 +01:00
|
|
|
key := fmt.Sprintf("%d", eventNID)
|
|
|
|
val, found := c.FederationEvents.Get(key)
|
|
|
|
if found && val != nil {
|
|
|
|
if event, ok := val.(*gomatrixserverlib.HeaderedEvent); ok {
|
|
|
|
return event, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2021-11-24 11:45:23 +01:00
|
|
|
func (c Caches) StoreFederationQueuedPDU(eventNID int64, event *gomatrixserverlib.HeaderedEvent) {
|
2020-12-04 15:52:10 +01:00
|
|
|
key := fmt.Sprintf("%d", eventNID)
|
|
|
|
c.FederationEvents.Set(key, event)
|
|
|
|
}
|
|
|
|
|
2021-11-24 11:45:23 +01:00
|
|
|
func (c Caches) EvictFederationQueuedPDU(eventNID int64) {
|
2020-12-04 15:52:10 +01:00
|
|
|
key := fmt.Sprintf("%d", eventNID)
|
|
|
|
c.FederationEvents.Unset(key)
|
|
|
|
}
|
|
|
|
|
2021-11-24 11:45:23 +01:00
|
|
|
func (c Caches) GetFederationQueuedEDU(eventNID int64) (*gomatrixserverlib.EDU, bool) {
|
2020-12-04 15:52:10 +01:00
|
|
|
key := fmt.Sprintf("%d", eventNID)
|
|
|
|
val, found := c.FederationEvents.Get(key)
|
|
|
|
if found && val != nil {
|
|
|
|
if event, ok := val.(*gomatrixserverlib.EDU); ok {
|
|
|
|
return event, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2021-11-24 11:45:23 +01:00
|
|
|
func (c Caches) StoreFederationQueuedEDU(eventNID int64, event *gomatrixserverlib.EDU) {
|
2020-12-04 15:52:10 +01:00
|
|
|
key := fmt.Sprintf("%d", eventNID)
|
|
|
|
c.FederationEvents.Set(key, event)
|
|
|
|
}
|
|
|
|
|
2021-11-24 11:45:23 +01:00
|
|
|
func (c Caches) EvictFederationQueuedEDU(eventNID int64) {
|
2020-12-04 15:52:10 +01:00
|
|
|
key := fmt.Sprintf("%d", eventNID)
|
|
|
|
c.FederationEvents.Unset(key)
|
|
|
|
}
|