made stats more efficient
This commit is contained in:
parent
000214043c
commit
f3ea6b58a7
1 changed files with 23 additions and 15 deletions
|
@ -15,7 +15,7 @@ namespace BirdsiteLive.Services
|
||||||
private readonly ITwitterUserDal _twitterUserDal;
|
private readonly ITwitterUserDal _twitterUserDal;
|
||||||
private readonly IFollowersDal _followersDal;
|
private readonly IFollowersDal _followersDal;
|
||||||
|
|
||||||
private static CachedStatistics _cachedStatistics;
|
private static Task<CachedStatistics> _cachedStatistics;
|
||||||
private readonly InstanceSettings _instanceSettings;
|
private readonly InstanceSettings _instanceSettings;
|
||||||
|
|
||||||
#region Ctor
|
#region Ctor
|
||||||
|
@ -24,28 +24,36 @@ namespace BirdsiteLive.Services
|
||||||
_twitterUserDal = twitterUserDal;
|
_twitterUserDal = twitterUserDal;
|
||||||
_instanceSettings = instanceSettings;
|
_instanceSettings = instanceSettings;
|
||||||
_followersDal = followersDal;
|
_followersDal = followersDal;
|
||||||
|
_cachedStatistics = CreateStats();
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
public async Task<CachedStatistics> GetStatisticsAsync()
|
public async Task<CachedStatistics> GetStatisticsAsync()
|
||||||
{
|
{
|
||||||
if (_cachedStatistics == null ||
|
var stats = await _cachedStatistics;
|
||||||
(DateTime.UtcNow - _cachedStatistics.RefreshedTime).TotalMinutes > 15)
|
if ((DateTime.UtcNow - stats.RefreshedTime).TotalMinutes > 5)
|
||||||
{
|
{
|
||||||
var twitterUserCount = await _twitterUserDal.GetTwitterUsersCountAsync();
|
_cachedStatistics = CreateStats();
|
||||||
var twitterSyncLag = await _twitterUserDal.GetTwitterSyncLag();
|
|
||||||
var fediverseUsers = await _followersDal.GetFollowersCountAsync();
|
|
||||||
|
|
||||||
_cachedStatistics = new CachedStatistics
|
|
||||||
{
|
|
||||||
RefreshedTime = DateTime.UtcNow,
|
|
||||||
SyncLag = twitterSyncLag,
|
|
||||||
TwitterUsers = twitterUserCount,
|
|
||||||
FediverseUsers = fediverseUsers
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return _cachedStatistics;
|
return stats;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<CachedStatistics> CreateStats()
|
||||||
|
{
|
||||||
|
var twitterUserCount = await _twitterUserDal.GetTwitterUsersCountAsync();
|
||||||
|
var twitterSyncLag = await _twitterUserDal.GetTwitterSyncLag();
|
||||||
|
var fediverseUsers = await _followersDal.GetFollowersCountAsync();
|
||||||
|
|
||||||
|
var stats = new CachedStatistics
|
||||||
|
{
|
||||||
|
RefreshedTime = DateTime.UtcNow,
|
||||||
|
SyncLag = twitterSyncLag,
|
||||||
|
TwitterUsers = twitterUserCount,
|
||||||
|
FediverseUsers = fediverseUsers
|
||||||
|
};
|
||||||
|
|
||||||
|
return stats;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue