Skip to content

Commit

Permalink
disposed Api services
Browse files Browse the repository at this point in the history
  • Loading branch information
granstel committed Dec 14, 2023
1 parent 9648e16 commit 592e514
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ internal static void AddInternalServices(this IServiceCollection services)
{
services.AddTransient<IConversationService, ConversationService>();
services.AddTransient<INotificationsService, NotificationsService>();
services.AddTransient<UnitsService>();
services.AddScoped<UnitsService>();
services.AddTransient<StatisticsService>();
services.AddTransient<ICountriesService, CountriesService>();
services.AddTransient<IUsersService, UsersService>();
services.AddTransient<IGlobalApiService, GlobalApiService>();
services.AddTransient<IPublicApiService, PublicApiService>();
services.AddScoped<IGlobalApiService, GlobalApiService>();
services.AddScoped<IPublicApiService, PublicApiService>();

services.AddScoped<IDialogflowService, DialogflowService>();
}
Expand Down
7 changes: 6 additions & 1 deletion Dodo1000Bot.Services/GlobalApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Dodo1000Bot.Services;

public class GlobalApiService : IGlobalApiService
public class GlobalApiService : IGlobalApiService, IAsyncDisposable
{
private readonly ILogger<GlobalApiService> _log;
private readonly IGlobalApiClient _globalApiClient;
Expand Down Expand Up @@ -98,4 +98,9 @@ private async Task UpdateSnapshot<TData>(string snapshotName, TData data, Cancel

_memoryCache.Remove(snapshotName);
}

public async ValueTask DisposeAsync()
{
await UpdateUnitsCountSnapshot(default);
}
}
9 changes: 7 additions & 2 deletions Dodo1000Bot.Services/PublicApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Dodo1000Bot.Services;

using AllUnitsDictionary = Dictionary<string, Dictionary<Country, IEnumerable<UnitInfo>>>;

public class PublicApiService : IPublicApiService
public class PublicApiService : IPublicApiService, IAsyncDisposable
{
private readonly ILogger<PublicApiService> _log;
private readonly IPublicApiClient _publicApiClient;
Expand Down Expand Up @@ -110,8 +110,8 @@ private async Task<AllUnitsDictionary> AllUnitsInternal(CancellationToken cancel
foreach (var country in countriesOfBrand)
{
var countryCode = country.Code;
var cacheName = GetUnitInfoOfBrandAtCountrySnapshotName(brand, countryCode);

var cacheName = GetUnitInfoOfBrandAtCountrySnapshotName(brand, countryCode);
var unitsInfoOfBrandAtCountry =
await _memoryCache.GetOrCreate(cacheName, _ => GetUnitsInfoOfBrandAtCountry(brand, countryCode, cancellationToken));

Expand Down Expand Up @@ -150,4 +150,9 @@ private async Task UpdateSnapshot<TData>(string snapshotName, TData data, Cancel

_memoryCache.Remove(snapshotName);
}

public async ValueTask DisposeAsync()
{
await UpdateAllUnitsSnapshot(default);
}
}

0 comments on commit 592e514

Please sign in to comment.