2020-06-05 15:42:01 +00:00
|
|
|
package caching
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-02-17 13:50:27 +00:00
|
|
|
"time"
|
2020-06-05 15:42:01 +00:00
|
|
|
|
|
|
|
lru "github.com/hashicorp/golang-lru"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
|
|
)
|
|
|
|
|
2020-06-16 12:11:20 +00:00
|
|
|
func NewInMemoryLRUCache(enablePrometheus bool) (*Caches, error) {
|
2020-06-05 15:42:01 +00:00
|
|
|
roomVersions, err := NewInMemoryLRUCachePartition(
|
|
|
|
RoomVersionCacheName,
|
|
|
|
RoomVersionCacheMutable,
|
|
|
|
RoomVersionCacheMaxEntries,
|
2020-06-16 12:11:20 +00:00
|
|
|
enablePrometheus,
|
2020-06-05 15:42:01 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
serverKeys, err := NewInMemoryLRUCachePartition(
|
|
|
|
ServerKeyCacheName,
|
|
|
|
ServerKeyCacheMutable,
|
|
|
|
ServerKeyCacheMaxEntries,
|
2020-06-16 12:11:20 +00:00
|
|
|
enablePrometheus,
|
2020-06-05 15:42:01 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-25 11:32:29 +00:00
|
|
|
roomServerStateKeyNIDs, err := NewInMemoryLRUCachePartition(
|
|
|
|
RoomServerStateKeyNIDsCacheName,
|
|
|
|
RoomServerStateKeyNIDsCacheMutable,
|
|
|
|
RoomServerStateKeyNIDsCacheMaxEntries,
|
|
|
|
enablePrometheus,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
roomServerEventTypeNIDs, err := NewInMemoryLRUCachePartition(
|
|
|
|
RoomServerEventTypeNIDsCacheName,
|
|
|
|
RoomServerEventTypeNIDsCacheMutable,
|
|
|
|
RoomServerEventTypeNIDsCacheMaxEntries,
|
|
|
|
enablePrometheus,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-16 12:15:12 +00:00
|
|
|
roomServerRoomIDs, err := NewInMemoryLRUCachePartition(
|
|
|
|
RoomServerRoomIDsCacheName,
|
|
|
|
RoomServerRoomIDsCacheMutable,
|
|
|
|
RoomServerRoomIDsCacheMaxEntries,
|
2020-08-25 11:32:29 +00:00
|
|
|
enablePrometheus,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-16 12:15:12 +00:00
|
|
|
roomInfos, err := NewInMemoryLRUCachePartition(
|
|
|
|
RoomInfoCacheName,
|
|
|
|
RoomInfoCacheMutable,
|
|
|
|
RoomInfoCacheMaxEntries,
|
2020-08-25 11:32:29 +00:00
|
|
|
enablePrometheus,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-04 14:52:10 +00:00
|
|
|
federationEvents, err := NewInMemoryLRUCachePartition(
|
|
|
|
FederationEventCacheName,
|
|
|
|
FederationEventCacheMutable,
|
|
|
|
FederationEventCacheMaxEntries,
|
|
|
|
enablePrometheus,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-17 13:50:27 +00:00
|
|
|
go cacheCleaner(
|
|
|
|
roomVersions, serverKeys, roomServerStateKeyNIDs,
|
|
|
|
roomServerEventTypeNIDs, roomServerRoomIDs,
|
|
|
|
roomInfos, federationEvents,
|
|
|
|
)
|
2020-06-05 15:42:01 +00:00
|
|
|
return &Caches{
|
2020-08-25 11:32:29 +00:00
|
|
|
RoomVersions: roomVersions,
|
|
|
|
ServerKeys: serverKeys,
|
|
|
|
RoomServerStateKeyNIDs: roomServerStateKeyNIDs,
|
|
|
|
RoomServerEventTypeNIDs: roomServerEventTypeNIDs,
|
|
|
|
RoomServerRoomIDs: roomServerRoomIDs,
|
2020-12-16 12:15:12 +00:00
|
|
|
RoomInfos: roomInfos,
|
2020-12-04 14:52:10 +00:00
|
|
|
FederationEvents: federationEvents,
|
2020-06-05 15:42:01 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-02-17 13:50:27 +00:00
|
|
|
func cacheCleaner(caches ...*InMemoryLRUCachePartition) {
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Minute)
|
|
|
|
for _, cache := range caches {
|
|
|
|
// Hold onto the last 10% of the cache entries, since
|
|
|
|
// otherwise a quiet period might cause us to evict all
|
|
|
|
// cache entries entirely.
|
|
|
|
if cache.lru.Len() > cache.maxEntries/10 {
|
|
|
|
cache.lru.RemoveOldest()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 15:42:01 +00:00
|
|
|
type InMemoryLRUCachePartition struct {
|
|
|
|
name string
|
|
|
|
mutable bool
|
|
|
|
maxEntries int
|
|
|
|
lru *lru.Cache
|
|
|
|
}
|
|
|
|
|
2020-06-16 12:11:20 +00:00
|
|
|
func NewInMemoryLRUCachePartition(name string, mutable bool, maxEntries int, enablePrometheus bool) (*InMemoryLRUCachePartition, error) {
|
2020-06-05 15:42:01 +00:00
|
|
|
var err error
|
|
|
|
cache := InMemoryLRUCachePartition{
|
|
|
|
name: name,
|
|
|
|
mutable: mutable,
|
|
|
|
maxEntries: maxEntries,
|
|
|
|
}
|
|
|
|
cache.lru, err = lru.New(maxEntries)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-16 12:11:20 +00:00
|
|
|
if enablePrometheus {
|
|
|
|
promauto.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
Namespace: "dendrite",
|
|
|
|
Subsystem: "caching_in_memory_lru",
|
|
|
|
Name: name,
|
|
|
|
}, func() float64 {
|
|
|
|
return float64(cache.lru.Len())
|
|
|
|
})
|
|
|
|
}
|
2020-06-05 15:42:01 +00:00
|
|
|
return &cache, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *InMemoryLRUCachePartition) Set(key string, value interface{}) {
|
|
|
|
if !c.mutable {
|
|
|
|
if peek, ok := c.lru.Peek(key); ok && peek != value {
|
|
|
|
panic(fmt.Sprintf("invalid use of immutable cache tries to mutate existing value of %q", key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.lru.Add(key, value)
|
|
|
|
}
|
|
|
|
|
2020-06-12 10:07:26 +00:00
|
|
|
func (c *InMemoryLRUCachePartition) Unset(key string) {
|
|
|
|
if !c.mutable {
|
|
|
|
panic(fmt.Sprintf("invalid use of immutable cache tries to unset value of %q", key))
|
|
|
|
}
|
|
|
|
c.lru.Remove(key)
|
|
|
|
}
|
|
|
|
|
2020-06-05 15:42:01 +00:00
|
|
|
func (c *InMemoryLRUCachePartition) Get(key string) (value interface{}, ok bool) {
|
|
|
|
return c.lru.Get(key)
|
|
|
|
}
|