added tweet caching
This commit is contained in:
parent
6bc915f97d
commit
b1aafc28ab
2 changed files with 65 additions and 2 deletions
63
src/BirdsiteLive.Twitter/CachedTwitterTweetsService.cs
Normal file
63
src/BirdsiteLive.Twitter/CachedTwitterTweetsService.cs
Normal file
|
@ -0,0 +1,63 @@
|
|||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using BirdsiteLive.Common.Settings;
|
||||
using BirdsiteLive.Twitter.Models;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace BirdsiteLive.Twitter
|
||||
{
|
||||
public interface ICachedTwitterTweetsService : ITwitterTweetsService
|
||||
{
|
||||
void SetTweet(long id, ExtractedTweet tweet);
|
||||
}
|
||||
|
||||
public class CachedTwitterTweetsService : ICachedTwitterTweetsService
|
||||
{
|
||||
private readonly ITwitterTweetsService _twitterService;
|
||||
|
||||
private readonly MemoryCache _tweetCache;
|
||||
private readonly MemoryCacheEntryOptions _cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||
.SetSize(1)//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(60))
|
||||
// Remove from cache after this time, regardless of sliding expiration
|
||||
.SetAbsoluteExpiration(TimeSpan.FromDays(1));
|
||||
|
||||
#region Ctor
|
||||
public CachedTwitterTweetsService(ITwitterTweetsService twitterService, InstanceSettings settings)
|
||||
{
|
||||
_twitterService = twitterService;
|
||||
|
||||
_tweetCache = new MemoryCache(new MemoryCacheOptions()
|
||||
{
|
||||
SizeLimit = 10000 //TODO make this use number of entries in db
|
||||
});
|
||||
}
|
||||
#endregion
|
||||
|
||||
public async Task<ExtractedTweet[]> GetTimelineAsync(string username, long id)
|
||||
{
|
||||
var res = await _twitterService.GetTimelineAsync(username, id);
|
||||
return res;
|
||||
}
|
||||
public async Task<ExtractedTweet> GetTweetAsync(long id)
|
||||
{
|
||||
if (!_tweetCache.TryGetValue(id, out ExtractedTweet tweet))
|
||||
{
|
||||
tweet = await _twitterService.GetTweetAsync(id);
|
||||
if(tweet != null) _tweetCache.Set(id, tweet, _cacheEntryOptions);
|
||||
}
|
||||
|
||||
return tweet;
|
||||
}
|
||||
|
||||
public void SetTweet(long id, ExtractedTweet tweet)
|
||||
{
|
||||
|
||||
_tweetCache.Set(id, tweet, _cacheEntryOptions);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -26,14 +26,14 @@ namespace BirdsiteLive.Controllers
|
|||
public class UsersController : Controller
|
||||
{
|
||||
private readonly ITwitterUserService _twitterUserService;
|
||||
private readonly ITwitterTweetsService _twitterTweetService;
|
||||
private readonly ICachedTwitterTweetsService _twitterTweetService;
|
||||
private readonly IUserService _userService;
|
||||
private readonly IStatusService _statusService;
|
||||
private readonly InstanceSettings _instanceSettings;
|
||||
private readonly ILogger<UsersController> _logger;
|
||||
|
||||
#region Ctor
|
||||
public UsersController(ITwitterUserService twitterUserService, IUserService userService, IStatusService statusService, InstanceSettings instanceSettings, ITwitterTweetsService twitterTweetService, ILogger<UsersController> logger)
|
||||
public UsersController(ITwitterUserService twitterUserService, IUserService userService, IStatusService statusService, InstanceSettings instanceSettings, ICachedTwitterTweetsService twitterTweetService, ILogger<UsersController> logger)
|
||||
{
|
||||
_twitterUserService = twitterUserService;
|
||||
_userService = userService;
|
||||
|
|
Reference in a new issue