From 1536880c73cceff11b8b0b62e2d571c29fbdea33 Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Thu, 3 Feb 2022 19:01:21 -0500 Subject: [PATCH] set the cache limits from settings --- VARIABLES.md | 1 + .../Settings/InstanceSettings.cs | 2 ++ src/BirdsiteLive.Twitter/CachedTwitterService.cs | 15 +++++++++------ src/BirdsiteLive/appsettings.json | 3 ++- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/VARIABLES.md b/VARIABLES.md index 57d854c..2ef0cdf 100644 --- a/VARIABLES.md +++ b/VARIABLES.md @@ -50,6 +50,7 @@ If both whitelisting and blacklisting are set, only the whitelisting will be act * `Instance:SensitiveTwitterAccounts` (default: null) mark all media from given accounts as sensitive by default, separated by `;`. * `Instance:FailingTwitterUserCleanUpThreshold` (default: 700) set the max allowed errors (due to a banned/deleted/private account) from a Twitter Account retrieval before auto-removal. (by default an account is called every 15 mins) * `Instance:FailingFollowerCleanUpThreshold` (default: 30000) set the max allowed errors from a Follower (Fediverse) Account before auto-removal. (often due to account suppression, instance issues, etc) +* `Instance:UserCacheCapacity` (default: 10000) set the caching limit of the Twitter User retrieval. Must be higher than the number of synchronized accounts on the instance. # Docker Compose full example diff --git a/src/BirdsiteLive.Common/Settings/InstanceSettings.cs b/src/BirdsiteLive.Common/Settings/InstanceSettings.cs index eb77d1f..0f701ff 100644 --- a/src/BirdsiteLive.Common/Settings/InstanceSettings.cs +++ b/src/BirdsiteLive.Common/Settings/InstanceSettings.cs @@ -14,5 +14,7 @@ public int FailingTwitterUserCleanUpThreshold { get; set; } public int FailingFollowerCleanUpThreshold { get; set; } = -1; + + public int UserCacheCapacity { get; set; } } } diff --git a/src/BirdsiteLive.Twitter/CachedTwitterService.cs b/src/BirdsiteLive.Twitter/CachedTwitterService.cs index ac20a62..ef5b0e5 100644 --- a/src/BirdsiteLive.Twitter/CachedTwitterService.cs +++ b/src/BirdsiteLive.Twitter/CachedTwitterService.cs @@ -1,4 +1,5 @@ using System; +using BirdsiteLive.Common.Settings; using BirdsiteLive.Twitter.Models; using Microsoft.Extensions.Caching.Memory; @@ -13,11 +14,8 @@ namespace BirdsiteLive.Twitter { private readonly ITwitterUserService _twitterService; - private MemoryCache _userCache = new MemoryCache(new MemoryCacheOptions() - { - SizeLimit = 5000 - }); - private MemoryCacheEntryOptions _cacheEntryOptions = new MemoryCacheEntryOptions() + private readonly MemoryCache _userCache; + private readonly MemoryCacheEntryOptions _cacheEntryOptions = new MemoryCacheEntryOptions() .SetSize(1)//Size amount //Priority on removing when reaching size limit (memory pressure) .SetPriority(CacheItemPriority.High) @@ -27,9 +25,14 @@ namespace BirdsiteLive.Twitter .SetAbsoluteExpiration(TimeSpan.FromDays(7)); #region Ctor - public CachedTwitterUserService(ITwitterUserService twitterService) + public CachedTwitterUserService(ITwitterUserService twitterService, InstanceSettings settings) { _twitterService = twitterService; + + _userCache = new MemoryCache(new MemoryCacheOptions() + { + SizeLimit = settings.UserCacheCapacity + }); } #endregion diff --git a/src/BirdsiteLive/appsettings.json b/src/BirdsiteLive/appsettings.json index b49d25c..4cee8ba 100644 --- a/src/BirdsiteLive/appsettings.json +++ b/src/BirdsiteLive/appsettings.json @@ -24,7 +24,8 @@ "UnlistedTwitterAccounts": null, "SensitiveTwitterAccounts": null, "FailingTwitterUserCleanUpThreshold": 700, - "FailingFollowerCleanUpThreshold": 30000 + "FailingFollowerCleanUpThreshold": 30000, + "UserCacheCapacity": 10000 }, "Db": { "Type": "postgres",