diff --git a/src/BirdsiteLive.Twitter/BirdsiteLive.Twitter.csproj b/src/BirdsiteLive.Twitter/BirdsiteLive.Twitter.csproj index 8b73ab8..377d04d 100644 --- a/src/BirdsiteLive.Twitter/BirdsiteLive.Twitter.csproj +++ b/src/BirdsiteLive.Twitter/BirdsiteLive.Twitter.csproj @@ -5,6 +5,7 @@ + diff --git a/src/BirdsiteLive.Twitter/CachedTwitterService.cs b/src/BirdsiteLive.Twitter/CachedTwitterService.cs new file mode 100644 index 0000000..a77fb12 --- /dev/null +++ b/src/BirdsiteLive.Twitter/CachedTwitterService.cs @@ -0,0 +1,52 @@ +using System; +using BirdsiteLive.Twitter.Models; +using Microsoft.Extensions.Caching.Memory; + +namespace BirdsiteLive.Twitter +{ + public class CachedTwitterService : ITwitterService + { + private readonly ITwitterService _twitterService; + + private MemoryCache _userCache = new MemoryCache(new MemoryCacheOptions() + { + SizeLimit = 5000 + }); + private MemoryCacheEntryOptions _cacheEntryOptions = new MemoryCacheEntryOptions() + .SetSize(1)//Size amount + //Priority on removing when reaching size limit (memory pressure) + .SetPriority(CacheItemPriority.High) + // Keep in cache for this time, reset time if accessed. + .SetSlidingExpiration(TimeSpan.FromHours(24)) + // Remove from cache after this time, regardless of sliding expiration + .SetAbsoluteExpiration(TimeSpan.FromDays(30)); + + #region Ctor + public CachedTwitterService(ITwitterService twitterService) + { + _twitterService = twitterService; + } + #endregion + + public TwitterUser GetUser(string username) + { + if (!_userCache.TryGetValue(username, out TwitterUser user)) + { + user = _twitterService.GetUser(username); + _userCache.Set(username, user, _cacheEntryOptions); + } + + return user; + } + + public ExtractedTweet GetTweet(long statusId) + { + return _twitterService.GetTweet(statusId); + } + + public ExtractedTweet[] GetTimeline(string username, int nberTweets, long fromTweetId = -1) + { + return _twitterService.GetTimeline(username, nberTweets, fromTweetId); + } + } +} \ No newline at end of file diff --git a/src/BirdsiteLive/BirdsiteLive.csproj b/src/BirdsiteLive/BirdsiteLive.csproj index aef5d85..9cfe9db 100644 --- a/src/BirdsiteLive/BirdsiteLive.csproj +++ b/src/BirdsiteLive/BirdsiteLive.csproj @@ -4,7 +4,7 @@ netcoreapp3.1 d21486de-a812-47eb-a419-05682bb68856 Linux - 0.8.1 + 0.9.0 diff --git a/src/BirdsiteLive/Startup.cs b/src/BirdsiteLive/Startup.cs index ee4b079..9c37a54 100644 --- a/src/BirdsiteLive/Startup.cs +++ b/src/BirdsiteLive/Startup.cs @@ -9,6 +9,7 @@ using BirdsiteLive.DAL.Contracts; using BirdsiteLive.DAL.Postgres.DataAccessLayers; using BirdsiteLive.DAL.Postgres.Settings; using BirdsiteLive.Models; +using BirdsiteLive.Twitter; using Lamar; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -64,7 +65,7 @@ namespace BirdsiteLive var logsSettings = Configuration.GetSection("Logging").Get(); services.For().Use(x => logsSettings); - + if (string.Equals(dbSettings.Type, DbTypes.Postgres, StringComparison.OrdinalIgnoreCase)) { var connString = $"Host={dbSettings.Host};Username={dbSettings.User};Password={dbSettings.Password};Database={dbSettings.Name}"; @@ -73,7 +74,7 @@ namespace BirdsiteLive ConnString = connString }; services.For().Use(x => postgresSettings); - + services.For().Use().Singleton(); services.For().Use().Singleton(); services.For().Use().Singleton(); @@ -82,6 +83,9 @@ namespace BirdsiteLive { throw new NotImplementedException($"{dbSettings.Type} is not supported"); } + + services.For().DecorateAllWith(); + services.For().Use().Singleton(); services.Scan(_ => {