Cap cache growth

This commit is contained in:
Till Faelligen 2022-04-13 15:17:11 +02:00
parent ae54c7f159
commit 0b5cbd977a
2 changed files with 38 additions and 20 deletions

View file

@ -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
} }

View file

@ -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()))