Merge pull request #57 from NicolasConstant/topic_keep-user-cache

add user cache
This commit is contained in:
Nicolas Constant 2021-01-18 05:09:07 +01:00 committed by GitHub
commit 05fa5dfb59
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 3 deletions

View file

@ -5,6 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
<PackageReference Include="TweetinviAPI" Version="4.0.3" />
</ItemGroup>

View file

@ -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);
}
}
}

View file

@ -4,7 +4,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<UserSecretsId>d21486de-a812-47eb-a419-05682bb68856</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<Version>0.8.1</Version>
<Version>0.9.0</Version>
</PropertyGroup>
<ItemGroup>

View file

@ -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<LogsSettings>();
services.For<LogsSettings>().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<PostgresSettings>().Use(x => postgresSettings);
services.For<ITwitterUserDal>().Use<TwitterUserPostgresDal>().Singleton();
services.For<IFollowersDal>().Use<FollowersPostgresDal>().Singleton();
services.For<IDbInitializerDal>().Use<DbInitializerPostgresDal>().Singleton();
@ -82,6 +83,9 @@ namespace BirdsiteLive
{
throw new NotImplementedException($"{dbSettings.Type} is not supported");
}
services.For<ITwitterService>().DecorateAllWith<CachedTwitterService>();
services.For<ITwitterService>().Use<TwitterService>().Singleton();
services.Scan(_ =>
{