diff --git a/src/BirdsiteLive.Twitter/CachedTwitterTweetsService.cs b/src/BirdsiteLive.Twitter/CachedTwitterTweetsService.cs new file mode 100644 index 0000000..89e3f8e --- /dev/null +++ b/src/BirdsiteLive.Twitter/CachedTwitterTweetsService.cs @@ -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 GetTimelineAsync(string username, long id) + { + var res = await _twitterService.GetTimelineAsync(username, id); + return res; + } + public async Task 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); + } + } +} \ No newline at end of file diff --git a/src/BirdsiteLive/Controllers/UsersController.cs b/src/BirdsiteLive/Controllers/UsersController.cs index c4d35cd..b6f9f19 100644 --- a/src/BirdsiteLive/Controllers/UsersController.cs +++ b/src/BirdsiteLive/Controllers/UsersController.cs @@ -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 _logger; #region Ctor - public UsersController(ITwitterUserService twitterUserService, IUserService userService, IStatusService statusService, InstanceSettings instanceSettings, ITwitterTweetsService twitterTweetService, ILogger logger) + public UsersController(ITwitterUserService twitterUserService, IUserService userService, IStatusService statusService, InstanceSettings instanceSettings, ICachedTwitterTweetsService twitterTweetService, ILogger logger) { _twitterUserService = twitterUserService; _userService = userService;