dendrite/internal/caching/impl_inmemorylru.go

190 lines
4.5 KiB
Go
Raw Normal View History

2020-06-05 16:42:01 +01:00
package caching
import (
"fmt"
"time"
2020-06-05 16:42:01 +01:00
lru "github.com/hashicorp/golang-lru"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
func NewInMemoryLRUCache(enablePrometheus bool) (*Caches, error) {
2020-06-05 16:42:01 +01:00
roomVersions, err := NewInMemoryLRUCachePartition(
RoomVersionCacheName,
RoomVersionCacheMutable,
RoomVersionCacheMaxEntries,
RoomVersionCacheMaxAge,
enablePrometheus,
2020-06-05 16:42:01 +01:00
)
if err != nil {
return nil, err
}
serverKeys, err := NewInMemoryLRUCachePartition(
ServerKeyCacheName,
ServerKeyCacheMutable,
ServerKeyCacheMaxEntries,
ServerKeyCacheMaxAge,
enablePrometheus,
2020-06-05 16:42:01 +01:00
)
if err != nil {
return nil, err
}
roomServerRoomIDs, err := NewInMemoryLRUCachePartition(
RoomServerRoomIDsCacheName,
RoomServerRoomIDsCacheMutable,
RoomServerRoomIDsCacheMaxEntries,
RoomServerRoomIDsCacheMaxAge,
enablePrometheus,
)
if err != nil {
return nil, err
}
roomInfos, err := NewInMemoryLRUCachePartition(
RoomInfoCacheName,
RoomInfoCacheMutable,
RoomInfoCacheMaxEntries,
RoomInfoCacheMaxAge,
enablePrometheus,
)
if err != nil {
return nil, err
}
federationEvents, err := NewInMemoryLRUCachePartition(
FederationEventCacheName,
FederationEventCacheMutable,
FederationEventCacheMaxEntries,
FederationEventCacheMaxAge,
enablePrometheus,
)
if err != nil {
return nil, err
}
2022-03-01 16:32:48 +00:00
spaceRooms, err := NewInMemoryLRUCachePartition(
SpaceSummaryRoomsCacheName,
SpaceSummaryRoomsCacheMutable,
SpaceSummaryRoomsCacheMaxEntries,
SpaceSummaryRoomsCacheMaxAge,
2022-03-01 16:32:48 +00:00
enablePrometheus,
)
if err != nil {
return nil, err
}
2022-05-06 14:33:34 +01:00
lazyLoadCache, err := NewInMemoryLRUCachePartition(
LazyLoadCacheName,
LazyLoadCacheMutable,
LazyLoadCacheMaxEntries,
LazyLoadCacheMaxAge,
enablePrometheus,
)
if err != nil {
return nil, err
}
go cacheCleaner(
roomVersions, serverKeys, roomServerRoomIDs,
2022-05-06 14:33:34 +01:00
roomInfos, federationEvents, spaceRooms, lazyLoadCache,
)
2020-06-05 16:42:01 +01:00
return &Caches{
RoomVersions: roomVersions,
ServerKeys: serverKeys,
RoomServerRoomIDs: roomServerRoomIDs,
RoomInfos: roomInfos,
FederationEvents: federationEvents,
2022-03-01 16:32:48 +00:00
SpaceSummaryRooms: spaceRooms,
2022-05-06 14:33:34 +01:00
LazyLoading: lazyLoadCache,
2020-06-05 16:42:01 +01:00
}, nil
}
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 16:42:01 +01:00
type InMemoryLRUCachePartition struct {
name string
mutable bool
maxEntries int
maxAge time.Duration
2020-06-05 16:42:01 +01:00
lru *lru.Cache
}
type inMemoryLRUCacheEntry struct {
value interface{}
created time.Time
}
func NewInMemoryLRUCachePartition(name string, mutable bool, maxEntries int, maxAge time.Duration, enablePrometheus bool) (*InMemoryLRUCachePartition, error) {
2020-06-05 16:42:01 +01:00
var err error
cache := InMemoryLRUCachePartition{
name: name,
mutable: mutable,
maxEntries: maxEntries,
maxAge: maxAge,
2020-06-05 16:42:01 +01:00
}
cache.lru, err = lru.New(maxEntries)
if err != nil {
return nil, err
}
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 16:42:01 +01:00
return &cache, nil
}
func (c *InMemoryLRUCachePartition) Set(key string, value interface{}) {
if !c.mutable {
if peek, ok := c.lru.Peek(key); ok {
if entry, ok := peek.(*inMemoryLRUCacheEntry); ok && entry.value != value {
panic(fmt.Sprintf("invalid use of immutable cache tries to mutate existing value of %q", key))
}
2020-06-05 16:42:01 +01:00
}
}
c.lru.Add(key, &inMemoryLRUCacheEntry{
value: value,
created: time.Now(),
})
2020-06-05 16:42:01 +01:00
}
2020-06-12 11:07:26 +01: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 16:42:01 +01:00
func (c *InMemoryLRUCachePartition) Get(key string) (value interface{}, ok bool) {
v, ok := c.lru.Get(key)
if !ok {
return nil, false
}
entry, ok := v.(*inMemoryLRUCacheEntry)
switch {
case ok && c.maxAge == CacheNoMaxAge:
return entry.value, ok // There's no maximum age policy
case ok && time.Since(entry.created) < c.maxAge:
return entry.value, ok // The value for the key isn't stale
default:
// Either the key was found and it was stale, or the key
// wasn't found at all
c.lru.Remove(key)
return nil, false
}
2020-06-05 16:42:01 +01:00
}