Experimental LRU cache for room versions

This commit is contained in:
Neil Alexander 2020-04-22 10:05:44 +01:00
parent 71f9d35b7c
commit 8642c0b2fa
5 changed files with 63 additions and 2 deletions

8
common/caching/cache.go Normal file
View file

@ -0,0 +1,8 @@
package caching
import "github.com/matrix-org/gomatrixserverlib"
type Cache interface {
GetRoomVersion(roomId string) (gomatrixserverlib.RoomVersion, bool)
StoreRoomVersion(roomId string, roomVersion gomatrixserverlib.RoomVersion)
}

View file

@ -0,0 +1,41 @@
package caching
import (
"fmt"
"sync"
"github.com/golang/groupcache/lru"
"github.com/matrix-org/gomatrixserverlib"
)
type InMemoryLRUCache struct {
roomVersions *lru.Cache
roomVersionsMutex sync.RWMutex
}
func NewInMemoryLRUCache() *InMemoryLRUCache {
return &InMemoryLRUCache{
roomVersions: lru.New(128),
}
}
func (c *InMemoryLRUCache) GetRoomVersion(roomID string) (gomatrixserverlib.RoomVersion, bool) {
fmt.Println("Cache hit for", roomID)
if c == nil {
return "", false
}
c.roomVersionsMutex.RLock()
defer c.roomVersionsMutex.RUnlock()
val, found := c.roomVersions.Get(roomID)
return val.(gomatrixserverlib.RoomVersion), found
}
func (c *InMemoryLRUCache) StoreRoomVersion(roomID string, roomVersion gomatrixserverlib.RoomVersion) {
fmt.Println("Cache store for", roomID)
if c == nil {
return
}
c.roomVersionsMutex.Lock()
defer c.roomVersionsMutex.Unlock()
c.roomVersions.Add(roomID, roomVersion)
}

1
go.mod
View file

@ -1,6 +1,7 @@
module github.com/matrix-org/dendrite module github.com/matrix-org/dendrite
require ( require (
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6
github.com/gorilla/mux v1.7.3 github.com/gorilla/mux v1.7.3
github.com/hashicorp/golang-lru v0.5.4 github.com/hashicorp/golang-lru v0.5.4
github.com/kr/pretty v0.2.0 // indirect github.com/kr/pretty v0.2.0 // indirect

View file

@ -22,6 +22,7 @@ import (
"net/http" "net/http"
"github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/common/caching"
"github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/roomserver/auth" "github.com/matrix-org/dendrite/roomserver/auth"
"github.com/matrix-org/dendrite/roomserver/state" "github.com/matrix-org/dendrite/roomserver/state"
@ -97,7 +98,8 @@ type RoomserverQueryAPIDatabase interface {
// RoomserverQueryAPI is an implementation of api.RoomserverQueryAPI // RoomserverQueryAPI is an implementation of api.RoomserverQueryAPI
type RoomserverQueryAPI struct { type RoomserverQueryAPI struct {
DB RoomserverQueryAPIDatabase DB RoomserverQueryAPIDatabase
Cache caching.Cache
} }
// QueryLatestEventsAndState implements api.RoomserverQueryAPI // QueryLatestEventsAndState implements api.RoomserverQueryAPI
@ -896,11 +898,16 @@ func (r *RoomserverQueryAPI) QueryRoomVersionForRoom(
request *api.QueryRoomVersionForRoomRequest, request *api.QueryRoomVersionForRoomRequest,
response *api.QueryRoomVersionForRoomResponse, response *api.QueryRoomVersionForRoomResponse,
) error { ) error {
if roomVersion, ok := r.Cache.GetRoomVersion(request.RoomID); ok {
response.RoomVersion = roomVersion
return nil
}
roomVersion, err := r.DB.GetRoomVersionForRoom(ctx, request.RoomID) roomVersion, err := r.DB.GetRoomVersionForRoom(ctx, request.RoomID)
if err != nil { if err != nil {
return err return err
} }
response.RoomVersion = roomVersion response.RoomVersion = roomVersion
r.Cache.StoreRoomVersion(request.RoomID, response.RoomVersion)
return nil return nil
} }

View file

@ -21,6 +21,7 @@ import (
asQuery "github.com/matrix-org/dendrite/appservice/query" asQuery "github.com/matrix-org/dendrite/appservice/query"
"github.com/matrix-org/dendrite/common/basecomponent" "github.com/matrix-org/dendrite/common/basecomponent"
"github.com/matrix-org/dendrite/common/caching"
"github.com/matrix-org/dendrite/roomserver/alias" "github.com/matrix-org/dendrite/roomserver/alias"
"github.com/matrix-org/dendrite/roomserver/input" "github.com/matrix-org/dendrite/roomserver/input"
"github.com/matrix-org/dendrite/roomserver/query" "github.com/matrix-org/dendrite/roomserver/query"
@ -48,7 +49,10 @@ func SetupRoomServerComponent(
inputAPI.SetupHTTP(http.DefaultServeMux) inputAPI.SetupHTTP(http.DefaultServeMux)
queryAPI := query.RoomserverQueryAPI{DB: roomserverDB} queryAPI := query.RoomserverQueryAPI{
DB: roomserverDB,
Cache: caching.NewInMemoryLRUCache(),
}
queryAPI.SetupHTTP(http.DefaultServeMux) queryAPI.SetupHTTP(http.DefaultServeMux)