improved twitter caching

This commit is contained in:
Vincent Cloutier 2023-03-12 10:50:45 -04:00
parent f3307f4047
commit 6dc006bc66
2 changed files with 11 additions and 11 deletions

View file

@ -19,11 +19,11 @@ namespace BirdsiteLive.Twitter
private readonly MemoryCache _userCache; private readonly MemoryCache _userCache;
private readonly MemoryCacheEntryOptions _cacheEntryOptions = new MemoryCacheEntryOptions() private readonly MemoryCacheEntryOptions _cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSize(1000)//Size amount .SetSize(10000)//Size amount
//Priority on removing when reaching size limit (memory pressure) //Priority on removing when reaching size limit (memory pressure)
.SetPriority(CacheItemPriority.Low) .SetPriority(CacheItemPriority.Low)
// Keep in cache for this time, reset time if accessed. // 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 // Remove from cache after this time, regardless of sliding expiration
.SetAbsoluteExpiration(TimeSpan.FromDays(1)); .SetAbsoluteExpiration(TimeSpan.FromDays(1));
@ -41,13 +41,13 @@ namespace BirdsiteLive.Twitter
public async Task<TwitterUser> GetUserAsync(string username) public async Task<TwitterUser> GetUserAsync(string username)
{ {
if (!_userCache.TryGetValue(username, out TwitterUser user)) if (!_userCache.TryGetValue(username, out Task<TwitterUser> user))
{ {
user = await _twitterService.GetUserAsync(username); user = _twitterService.GetUserAsync(username);
if(user != null) _userCache.Set(username, user, _cacheEntryOptions); await _userCache.Set(username, user, _cacheEntryOptions);
} }
return user; return await user;
} }
public bool IsUserApiRateLimited() public bool IsUserApiRateLimited()

View file

@ -18,7 +18,7 @@ namespace BirdsiteLive.Twitter
private readonly MemoryCache _tweetCache; private readonly MemoryCache _tweetCache;
private readonly MemoryCacheEntryOptions _cacheEntryOptions = new MemoryCacheEntryOptions() private readonly MemoryCacheEntryOptions _cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSize(1000)//Size amount .SetSize(10000)//Size amount
//Priority on removing when reaching size limit (memory pressure) //Priority on removing when reaching size limit (memory pressure)
.SetPriority(CacheItemPriority.Low) .SetPriority(CacheItemPriority.Low)
// Keep in cache for this time, reset time if accessed. // Keep in cache for this time, reset time if accessed.
@ -45,13 +45,13 @@ namespace BirdsiteLive.Twitter
} }
public async Task<ExtractedTweet> GetTweetAsync(long id) public async Task<ExtractedTweet> GetTweetAsync(long id)
{ {
if (!_tweetCache.TryGetValue(id, out ExtractedTweet tweet)) if (!_tweetCache.TryGetValue(id, out Task<ExtractedTweet> tweet))
{ {
tweet = await _twitterService.GetTweetAsync(id); tweet = _twitterService.GetTweetAsync(id);
if(tweet != null) _tweetCache.Set(id, tweet, _cacheEntryOptions); await _tweetCache.Set(id, tweet, _cacheEntryOptions);
} }
return tweet; return await tweet;
} }
public void SetTweet(long id, ExtractedTweet tweet) public void SetTweet(long id, ExtractedTweet tweet)