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 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<TwitterUser> GetUserAsync(string username)
{
if (!_userCache.TryGetValue(username, out TwitterUser user))
if (!_userCache.TryGetValue(username, out Task<TwitterUser> 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()

View File

@ -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<ExtractedTweet> GetTweetAsync(long id)
{
if (!_tweetCache.TryGetValue(id, out ExtractedTweet tweet))
if (!_tweetCache.TryGetValue(id, out Task<ExtractedTweet> 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)