mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-08-01 13:52:46 +00:00
LRU cache for room versions in RS query API (#976)
* Experimental LRU cache for room versions * Don't accidentally try to type-assert nil * Also reduce hits on query API * Use hashicorp implementation which mutexes for us * Define const for max cache entries * Rename to be specifically immutable, panic if we try to mutate a cache entry * Review comments * Remove nil guards, give roomserver integration test a cache * go mod tidy
This commit is contained in:
parent
71f9d35b7c
commit
a466e9e9cc
7 changed files with 112 additions and 22 deletions
12
common/caching/immutablecache.go
Normal file
12
common/caching/immutablecache.go
Normal file
|
@ -0,0 +1,12 @@
|
|||
package caching
|
||||
|
||||
import "github.com/matrix-org/gomatrixserverlib"
|
||||
|
||||
const (
|
||||
RoomVersionMaxCacheEntries = 128
|
||||
)
|
||||
|
||||
type ImmutableCache interface {
|
||||
GetRoomVersion(roomId string) (gomatrixserverlib.RoomVersion, bool)
|
||||
StoreRoomVersion(roomId string, roomVersion gomatrixserverlib.RoomVersion)
|
||||
}
|
43
common/caching/immutableinmemorylru.go
Normal file
43
common/caching/immutableinmemorylru.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package caching
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
lru "github.com/hashicorp/golang-lru"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
)
|
||||
|
||||
type ImmutableInMemoryLRUCache struct {
|
||||
roomVersions *lru.Cache
|
||||
}
|
||||
|
||||
func NewImmutableInMemoryLRUCache() (*ImmutableInMemoryLRUCache, error) {
|
||||
roomVersionCache, rvErr := lru.New(RoomVersionMaxCacheEntries)
|
||||
if rvErr != nil {
|
||||
return nil, rvErr
|
||||
}
|
||||
return &ImmutableInMemoryLRUCache{
|
||||
roomVersions: roomVersionCache,
|
||||
}, 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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue