mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-03 12:13:09 -06:00
Cap cache growth
This commit is contained in:
parent
ae54c7f159
commit
0b5cbd977a
|
|
@ -8,41 +8,56 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
LazyLoadCacheName = "lazy_load_members"
|
LazyLoadCacheName = "lazy_load_members"
|
||||||
LazyLoadCacheMaxEntries = 128
|
LazyLoadCacheMaxEntries = 128
|
||||||
LazyLoadCacheMutable = true
|
LazyLoadCacheMaxUserEntries = 128
|
||||||
LazyLoadCacheMaxAge = time.Minute * 30
|
LazyLoadCacheMutable = true
|
||||||
|
LazyLoadCacheMaxAge = time.Minute * 30
|
||||||
)
|
)
|
||||||
|
|
||||||
type LazyLoadCache struct {
|
type LazyLoadCache struct {
|
||||||
// Mapping from userID/deviceID to InMemoryLRUCachePartition
|
// InMemoryLRUCachePartition containing other InMemoryLRUCachePartitions
|
||||||
userCaches map[string]*InMemoryLRUCachePartition
|
// with the actual cached members
|
||||||
|
userCaches *InMemoryLRUCachePartition
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLazyLoadCache creates a new LazyLoadCache.
|
// NewLazyLoadCache creates a new LazyLoadCache.
|
||||||
func NewLazyLoadCache() *LazyLoadCache {
|
func NewLazyLoadCache() (*LazyLoadCache, error) {
|
||||||
return &LazyLoadCache{
|
|
||||||
userCaches: make(map[string]*InMemoryLRUCachePartition),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *LazyLoadCache) lazyLoadCacheForUser(device *userapi.Device) (*InMemoryLRUCachePartition, error) {
|
|
||||||
cacheName := fmt.Sprintf("%s/%s", device.UserID, device.ID)
|
|
||||||
cache, ok := c.userCaches[cacheName]
|
|
||||||
if ok {
|
|
||||||
return cache, nil
|
|
||||||
}
|
|
||||||
cache, err := NewInMemoryLRUCachePartition(
|
cache, err := NewInMemoryLRUCachePartition(
|
||||||
LazyLoadCacheName,
|
LazyLoadCacheName,
|
||||||
LazyLoadCacheMutable,
|
LazyLoadCacheMutable,
|
||||||
LazyLoadCacheMaxEntries,
|
LazyLoadCacheMaxEntries,
|
||||||
LazyLoadCacheMaxAge,
|
LazyLoadCacheMaxAge,
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
go cacheCleaner(cache)
|
||||||
|
return &LazyLoadCache{
|
||||||
|
userCaches: cache,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *LazyLoadCache) lazyLoadCacheForUser(device *userapi.Device) (*InMemoryLRUCachePartition, error) {
|
||||||
|
cacheName := fmt.Sprintf("%s/%s", device.UserID, device.ID)
|
||||||
|
userCache, ok := c.userCaches.Get(cacheName)
|
||||||
|
if ok && userCache != nil {
|
||||||
|
if cache, ok := userCache.(*InMemoryLRUCachePartition); ok {
|
||||||
|
return cache, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cache, err := NewInMemoryLRUCachePartition(
|
||||||
|
LazyLoadCacheName,
|
||||||
|
LazyLoadCacheMutable,
|
||||||
|
LazyLoadCacheMaxUserEntries,
|
||||||
|
LazyLoadCacheMaxAge,
|
||||||
false,
|
false,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
c.userCaches[cacheName] = cache
|
c.userCaches.Set(cacheName, cache)
|
||||||
go cacheCleaner(cache)
|
go cacheCleaner(cache)
|
||||||
return cache, nil
|
return cache, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,10 @@ func AddPublicRoutes(
|
||||||
}
|
}
|
||||||
|
|
||||||
eduCache := caching.NewTypingCache()
|
eduCache := caching.NewTypingCache()
|
||||||
lazyLoadCache := caching.NewLazyLoadCache()
|
lazyLoadCache, err := caching.NewLazyLoadCache()
|
||||||
|
if err != nil {
|
||||||
|
logrus.WithError(err).Panicf("failed to create lazy loading cache")
|
||||||
|
}
|
||||||
notifier := notifier.NewNotifier()
|
notifier := notifier.NewNotifier()
|
||||||
streams := streams.NewSyncStreamProviders(syncDB, userAPI, rsAPI, keyAPI, eduCache, lazyLoadCache, notifier)
|
streams := streams.NewSyncStreamProviders(syncDB, userAPI, rsAPI, keyAPI, eduCache, lazyLoadCache, notifier)
|
||||||
notifier.SetCurrentPosition(streams.Latest(context.Background()))
|
notifier.SetCurrentPosition(streams.Latest(context.Background()))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue