Review comments

This commit is contained in:
Neil Alexander 2020-04-22 12:23:08 +01:00
parent 243081831a
commit 1373929ffc
2 changed files with 5 additions and 4 deletions

View file

@ -3,7 +3,6 @@ package caching
import "github.com/matrix-org/gomatrixserverlib" import "github.com/matrix-org/gomatrixserverlib"
const ( const (
RoomVersionCachingEnabled = true
RoomVersionMaxCacheEntries = 128 RoomVersionMaxCacheEntries = 128
) )

View file

@ -1,6 +1,8 @@
package caching package caching
import ( import (
"fmt"
lru "github.com/hashicorp/golang-lru" lru "github.com/hashicorp/golang-lru"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
) )
@ -21,12 +23,12 @@ func NewImmutableInMemoryLRUCache() (*ImmutableInMemoryLRUCache, error) {
func checkForInvalidMutation(cache *lru.Cache, key string, value interface{}) { func checkForInvalidMutation(cache *lru.Cache, key string, value interface{}) {
if peek, ok := cache.Peek(key); ok && peek != value { if peek, ok := cache.Peek(key); ok && peek != value {
panic("invalid use of immutable cache tries to mutate existing 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) { func (c *ImmutableInMemoryLRUCache) GetRoomVersion(roomID string) (gomatrixserverlib.RoomVersion, bool) {
if c == nil || !RoomVersionCachingEnabled { if c == nil {
return "", false return "", false
} }
val, found := c.roomVersions.Get(roomID) val, found := c.roomVersions.Get(roomID)
@ -39,7 +41,7 @@ func (c *ImmutableInMemoryLRUCache) GetRoomVersion(roomID string) (gomatrixserve
} }
func (c *ImmutableInMemoryLRUCache) StoreRoomVersion(roomID string, roomVersion gomatrixserverlib.RoomVersion) { func (c *ImmutableInMemoryLRUCache) StoreRoomVersion(roomID string, roomVersion gomatrixserverlib.RoomVersion) {
if c == nil || !RoomVersionCachingEnabled { if c == nil {
return return
} }
checkForInvalidMutation(c.roomVersions, roomID, roomVersion) checkForInvalidMutation(c.roomVersions, roomID, roomVersion)