2020-04-22 12:00:05 +00:00
|
|
|
package caching
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
lru "github.com/hashicorp/golang-lru"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ImmutableInMemoryLRUCache struct {
|
|
|
|
roomVersions *lru.Cache
|
2020-05-15 08:32:40 +00:00
|
|
|
serverKeys *lru.Cache
|
2020-04-22 12:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewImmutableInMemoryLRUCache() (*ImmutableInMemoryLRUCache, error) {
|
|
|
|
roomVersionCache, rvErr := lru.New(RoomVersionMaxCacheEntries)
|
|
|
|
if rvErr != nil {
|
|
|
|
return nil, rvErr
|
|
|
|
}
|
2020-05-15 08:32:40 +00:00
|
|
|
serverKeysCache, rvErr := lru.New(ServerKeysMaxCacheEntries)
|
|
|
|
if rvErr != nil {
|
|
|
|
return nil, rvErr
|
|
|
|
}
|
2020-04-22 12:00:05 +00:00
|
|
|
return &ImmutableInMemoryLRUCache{
|
|
|
|
roomVersions: roomVersionCache,
|
2020-05-15 08:32:40 +00:00
|
|
|
serverKeys: serverKeysCache,
|
2020-04-22 12:00:05 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkForInvalidMutation(cache *lru.Cache, key string, value interface{}) {
|
|
|
|
if peek, ok := cache.Peek(key); ok && peek != value {
|
|
|
|
panic(fmt.Sprintf("invalid use of immutable cache tries to mutate existing value of %q", key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ImmutableInMemoryLRUCache) GetRoomVersion(roomID string) (gomatrixserverlib.RoomVersion, bool) {
|
|
|
|
val, found := c.roomVersions.Get(roomID)
|
|
|
|
if found && val != nil {
|
|
|
|
if roomVersion, ok := val.(gomatrixserverlib.RoomVersion); ok {
|
|
|
|
return roomVersion, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ImmutableInMemoryLRUCache) StoreRoomVersion(roomID string, roomVersion gomatrixserverlib.RoomVersion) {
|
|
|
|
checkForInvalidMutation(c.roomVersions, roomID, roomVersion)
|
|
|
|
c.roomVersions.Add(roomID, roomVersion)
|
|
|
|
}
|
2020-05-15 08:32:40 +00:00
|
|
|
|
|
|
|
func (c *ImmutableInMemoryLRUCache) GetServerKey(
|
|
|
|
request gomatrixserverlib.PublicKeyLookupRequest,
|
|
|
|
) (gomatrixserverlib.PublicKeyLookupResult, bool) {
|
|
|
|
key := fmt.Sprintf("%s/%s", request.ServerName, request.KeyID)
|
|
|
|
val, found := c.serverKeys.Get(key)
|
|
|
|
if found && val != nil {
|
|
|
|
if keyLookupResult, ok := val.(gomatrixserverlib.PublicKeyLookupResult); ok {
|
|
|
|
return keyLookupResult, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gomatrixserverlib.PublicKeyLookupResult{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ImmutableInMemoryLRUCache) StoreServerKey(
|
|
|
|
request gomatrixserverlib.PublicKeyLookupRequest,
|
|
|
|
response gomatrixserverlib.PublicKeyLookupResult,
|
|
|
|
) {
|
|
|
|
key := fmt.Sprintf("%s/%s", request.ServerName, request.KeyID)
|
|
|
|
checkForInvalidMutation(c.roomVersions, key, response)
|
|
|
|
c.serverKeys.Add(request, response)
|
|
|
|
}
|