mirror of
https://github.com/1f349/dendrite.git
synced 2024-11-08 18:16:59 +00:00
Gradually evict oldest cache entries (#1768)
* Gradually evict oldest cache entries * Keep the remaining 10% of cached entries
This commit is contained in:
parent
5d74a1757f
commit
2386e0c7af
@ -2,6 +2,7 @@ package caching
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
lru "github.com/hashicorp/golang-lru"
|
lru "github.com/hashicorp/golang-lru"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
@ -72,6 +73,11 @@ func NewInMemoryLRUCache(enablePrometheus bool) (*Caches, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
go cacheCleaner(
|
||||||
|
roomVersions, serverKeys, roomServerStateKeyNIDs,
|
||||||
|
roomServerEventTypeNIDs, roomServerRoomIDs,
|
||||||
|
roomInfos, federationEvents,
|
||||||
|
)
|
||||||
return &Caches{
|
return &Caches{
|
||||||
RoomVersions: roomVersions,
|
RoomVersions: roomVersions,
|
||||||
ServerKeys: serverKeys,
|
ServerKeys: serverKeys,
|
||||||
@ -83,6 +89,20 @@ func NewInMemoryLRUCache(enablePrometheus bool) (*Caches, error) {
|
|||||||
}, nil
|
}, 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type InMemoryLRUCachePartition struct {
|
type InMemoryLRUCachePartition struct {
|
||||||
name string
|
name string
|
||||||
mutable bool
|
mutable bool
|
||||||
|
Loading…
Reference in New Issue
Block a user