From 6dc006bc66e64bf828d30ab15362fb306c891a8d Mon Sep 17 00:00:00 2001 From: Vincent Cloutier Date: Sun, 12 Mar 2023 10:50:45 -0400 Subject: [PATCH] improved twitter caching --- src/BirdsiteLive.Twitter/CachedTwitterService.cs | 12 ++++++------ .../CachedTwitterTweetsService.cs | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/BirdsiteLive.Twitter/CachedTwitterService.cs b/src/BirdsiteLive.Twitter/CachedTwitterService.cs index 7d7ce37..a74c3de 100644 --- a/src/BirdsiteLive.Twitter/CachedTwitterService.cs +++ b/src/BirdsiteLive.Twitter/CachedTwitterService.cs @@ -19,11 +19,11 @@ namespace BirdsiteLive.Twitter private readonly MemoryCache _userCache; private readonly MemoryCacheEntryOptions _cacheEntryOptions = new MemoryCacheEntryOptions() - .SetSize(1000)//Size amount + .SetSize(10000)//Size amount //Priority on removing when reaching size limit (memory pressure) .SetPriority(CacheItemPriority.Low) // Keep in cache for this time, reset time if accessed. - .SetSlidingExpiration(TimeSpan.FromMinutes(10)) + .SetSlidingExpiration(TimeSpan.FromMinutes(60)) // Remove from cache after this time, regardless of sliding expiration .SetAbsoluteExpiration(TimeSpan.FromDays(1)); @@ -41,13 +41,13 @@ namespace BirdsiteLive.Twitter public async Task GetUserAsync(string username) { - if (!_userCache.TryGetValue(username, out TwitterUser user)) + if (!_userCache.TryGetValue(username, out Task user)) { - user = await _twitterService.GetUserAsync(username); - if(user != null) _userCache.Set(username, user, _cacheEntryOptions); + user = _twitterService.GetUserAsync(username); + await _userCache.Set(username, user, _cacheEntryOptions); } - return user; + return await user; } public bool IsUserApiRateLimited() diff --git a/src/BirdsiteLive.Twitter/CachedTwitterTweetsService.cs b/src/BirdsiteLive.Twitter/CachedTwitterTweetsService.cs index 5eb01a3..83da0ac 100644 --- a/src/BirdsiteLive.Twitter/CachedTwitterTweetsService.cs +++ b/src/BirdsiteLive.Twitter/CachedTwitterTweetsService.cs @@ -18,7 +18,7 @@ namespace BirdsiteLive.Twitter private readonly MemoryCache _tweetCache; private readonly MemoryCacheEntryOptions _cacheEntryOptions = new MemoryCacheEntryOptions() - .SetSize(1000)//Size amount + .SetSize(10000)//Size amount //Priority on removing when reaching size limit (memory pressure) .SetPriority(CacheItemPriority.Low) // Keep in cache for this time, reset time if accessed. @@ -45,13 +45,13 @@ namespace BirdsiteLive.Twitter } public async Task GetTweetAsync(long id) { - if (!_tweetCache.TryGetValue(id, out ExtractedTweet tweet)) + if (!_tweetCache.TryGetValue(id, out Task tweet)) { - tweet = await _twitterService.GetTweetAsync(id); - if(tweet != null) _tweetCache.Set(id, tweet, _cacheEntryOptions); + tweet = _twitterService.GetTweetAsync(id); + await _tweetCache.Set(id, tweet, _cacheEntryOptions); } - return tweet; + return await tweet; } public void SetTweet(long id, ExtractedTweet tweet)