diff options
author | Neil Alexander <neilalexander@users.noreply.github.com> | 2021-02-17 13:50:27 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-17 13:50:27 +0000 |
commit | 2386e0c7af5b29b3d45373db812204cd720f7f88 (patch) | |
tree | 8d89b5daf4d3b071d1c91d9cb152b33f01d84862 | |
parent | 5d74a1757f652f1e367a036f931e71bd3da612dd (diff) |
Gradually evict oldest cache entries (#1768)
* Gradually evict oldest cache entries
* Keep the remaining 10% of cached entries
-rw-r--r-- | internal/caching/impl_inmemorylru.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/internal/caching/impl_inmemorylru.go b/internal/caching/impl_inmemorylru.go index cf05a8b5..f0915d7c 100644 --- a/internal/caching/impl_inmemorylru.go +++ b/internal/caching/impl_inmemorylru.go @@ -2,6 +2,7 @@ package caching import ( "fmt" + "time" lru "github.com/hashicorp/golang-lru" "github.com/prometheus/client_golang/prometheus" @@ -72,6 +73,11 @@ func NewInMemoryLRUCache(enablePrometheus bool) (*Caches, error) { if err != nil { return nil, err } + go cacheCleaner( + roomVersions, serverKeys, roomServerStateKeyNIDs, + roomServerEventTypeNIDs, roomServerRoomIDs, + roomInfos, federationEvents, + ) return &Caches{ RoomVersions: roomVersions, ServerKeys: serverKeys, @@ -83,6 +89,20 @@ func NewInMemoryLRUCache(enablePrometheus bool) (*Caches, error) { }, 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 { name string mutable bool |