Skip to content

Commit

Permalink
cache and renamings
Browse files Browse the repository at this point in the history
  • Loading branch information
granstel committed Dec 14, 2023
1 parent 09fdce1 commit e47f024
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
27 changes: 19 additions & 8 deletions Dodo1000Bot.Services/GlobalApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task CreateUnitsCountSnapshotIfNotExists(CancellationToken cancella
{
try
{
var unitsCountSnapshot = await GetUnitsCountSnapshot(cancellationToken);
var unitsCountSnapshot = await UnitsCountSnapshot(cancellationToken);

if (unitsCountSnapshot is not null)
{
Expand All @@ -48,14 +48,14 @@ public async Task CreateUnitsCountSnapshotIfNotExists(CancellationToken cancella
}
}

public async Task<BrandListTotalUnitCountListModel> GetUnitsCount(CancellationToken cancellationToken)
public async Task<BrandListTotalUnitCountListModel> UnitsCount(CancellationToken cancellationToken)
{
var cacheName = nameof(_globalApiClient.UnitsCount);
var unitsCount = await _memoryCache.GetOrCreate(cacheName, _ => _globalApiClient.UnitsCount(cancellationToken));
return unitsCount;
}

public async Task<BrandListTotalUnitCountListModel> GetUnitsCountSnapshot(CancellationToken cancellationToken)
public async Task<BrandListTotalUnitCountListModel> UnitsCountSnapshot(CancellationToken cancellationToken)
{
var snapshotName = nameof(_globalApiClient.UnitsCount);
var unitsCountSnapshot =
Expand All @@ -66,16 +66,27 @@ public async Task<BrandListTotalUnitCountListModel> GetUnitsCountSnapshot(Cancel

public async Task UpdateUnitsCountSnapshot(CancellationToken cancellationToken)
{
var unitsCount = await GetUnitsCount(cancellationToken);
var unitsCount = await UnitsCount(cancellationToken);

var snapshotName = nameof(_globalApiClient.UnitsCount);
await UpdateSnapshot(snapshotName, unitsCount, cancellationToken);
}

// TODO: cache
public Task<IEnumerable<Brand>> GetBrands(CancellationToken cancellationToken) => _globalApiClient.GetBrands(cancellationToken);
// TODO: cache
public Task<IEnumerable<Country>> GetBrandCountries(string brand, CancellationToken cancellationToken) => _globalApiClient.GetBrandCountries(brand, cancellationToken);
public async Task<IEnumerable<Brand>> GetBrands(CancellationToken cancellationToken)
{
var cacheName = nameof(_globalApiClient.GetBrands);
var brands = await _memoryCache.GetOrCreate(cacheName, _ => _globalApiClient.GetBrands(cancellationToken));

return brands;
}

public async Task<IEnumerable<Country>> GetBrandCountries(string brand, CancellationToken cancellationToken)
{
var cacheName = $"{nameof(_globalApiClient.GetBrandCountries)}{brand}";
var brandCountries = await _memoryCache.GetOrCreate(cacheName, _ => _globalApiClient.GetBrandCountries(brand, cancellationToken));

return brandCountries;
}

private async Task UpdateSnapshot<TData>(string snapshotName, TData data, CancellationToken cancellationToken)
{
Expand Down
4 changes: 2 additions & 2 deletions Dodo1000Bot.Services/IGlobalApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Dodo1000Bot.Services;
public interface IGlobalApiService
{
Task CreateUnitsCountSnapshotIfNotExists(CancellationToken cancellationToken);
Task<BrandListTotalUnitCountListModel> GetUnitsCount(CancellationToken cancellationToken);
Task<BrandListTotalUnitCountListModel> GetUnitsCountSnapshot(CancellationToken cancellationToken);
Task<BrandListTotalUnitCountListModel> UnitsCount(CancellationToken cancellationToken);
Task<BrandListTotalUnitCountListModel> UnitsCountSnapshot(CancellationToken cancellationToken);
Task UpdateUnitsCountSnapshot(CancellationToken cancellationToken);
Task<IEnumerable<Brand>> GetBrands(CancellationToken cancellationToken);
Task<IEnumerable<Country>> GetBrandCountries(string brand, CancellationToken cancellationToken);
Expand Down
4 changes: 2 additions & 2 deletions Dodo1000Bot.Services/Interfaces/IPublicApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public interface IPublicApiService
Task CreateAllUnitsSnapshotIfNotExists(CancellationToken cancellationToken);
Task<IEnumerable<UnitInfo>> GetUnitInfoOfBrandAtCountrySnapshot(string brand, string countryCode,
CancellationToken cancellationToken);
Task UpdateAllUnitsSnapshot(AllUnitsDictionary allUnits, CancellationToken cancellationToken);
Task<AllUnitsDictionary> GetAllUnits(CancellationToken cancellationToken);
Task UpdateAllUnitsSnapshot(CancellationToken cancellationToken);
Task<AllUnitsDictionary> AllUnits(CancellationToken cancellationToken);
}
19 changes: 12 additions & 7 deletions Dodo1000Bot.Services/PublicApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,15 @@ public async Task CreateAllUnitsSnapshotIfNotExists(CancellationToken cancellati
{
try
{
var unitsCountSnapshot = await _globalApiService.GetUnitsCountSnapshot(cancellationToken);
var unitsCountSnapshot = await _globalApiService.UnitsCountSnapshot(cancellationToken);

if (unitsCountSnapshot is null)
{
_log.LogInformation("unitsCountSnapshot is null");
return;
}

var allUnitsInfo = await GetAllUnits(cancellationToken);
await UpdateAllUnitsSnapshot(allUnitsInfo, cancellationToken);
await UpdateAllUnitsSnapshot(cancellationToken);
}
catch (Exception e)
{
Expand All @@ -72,8 +71,9 @@ public async Task<IEnumerable<UnitInfo>> GetUnitInfoOfBrandAtCountrySnapshot(str
return unitsSnapshot?.Data;
}

public async Task UpdateAllUnitsSnapshot(AllUnitsDictionary allUnits, CancellationToken cancellationToken)
public async Task UpdateAllUnitsSnapshot(CancellationToken cancellationToken)
{
var allUnits = await AllUnits(cancellationToken);
var brands = allUnits.Keys;

foreach (var brand in brands)
Expand All @@ -89,11 +89,16 @@ public async Task UpdateAllUnitsSnapshot(AllUnitsDictionary allUnits, Cancellati
}
}
}

public async Task<AllUnitsDictionary> GetAllUnits(CancellationToken cancellationToken)

public async Task<AllUnitsDictionary> AllUnits(CancellationToken cancellationToken)
{
return await _memoryCache.GetOrCreate(nameof(AllUnits), _ => AllUnitsInternal(cancellationToken));
}

private async Task<AllUnitsDictionary> AllUnitsInternal(CancellationToken cancellationToken)
{
var allUnits = new AllUnitsDictionary();

var brands = await _globalApiService.GetBrands(cancellationToken);
var brandsNames = brands.Select(b => b.Name).ToList();

Expand Down
8 changes: 4 additions & 4 deletions Dodo1000Bot.Services/UnitsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ public override async Task CheckAndNotify(CancellationToken cancellationToken)
{
try
{
var unitsCount = await _globalApiService.GetUnitsCount(cancellationToken);
var unitsCount = await _globalApiService.UnitsCount(cancellationToken);
_log.LogInformation("unitsCount: {unitsCount}", unitsCount.Serialize());
await AboutTotalOverall(unitsCount, cancellationToken);
await AboutTotalAtBrands(unitsCount, cancellationToken);
await AboutTotalAtCountries(unitsCount, cancellationToken);
await AboutTotalCountriesAtBrands(unitsCount, cancellationToken);

var unitsCountSnapshot = await _globalApiService.GetUnitsCountSnapshot(cancellationToken);
var unitsCountSnapshot = await _globalApiService.UnitsCountSnapshot(cancellationToken);
_log.LogInformation("unitsCountSnapshot: {unitsCountSnapshot}", unitsCountSnapshot.Serialize());
await AboutNewCountries(unitsCount, unitsCountSnapshot, cancellationToken);

var allUnits = await _publicApiService.GetAllUnits(cancellationToken);
var allUnits = await _publicApiService.AllUnits(cancellationToken);

await AboutNewUnits(allUnits, cancellationToken);

await _globalApiService.UpdateUnitsCountSnapshot(cancellationToken);
await _publicApiService.UpdateAllUnitsSnapshot(allUnits, cancellationToken);
await _publicApiService.UpdateAllUnitsSnapshot(cancellationToken);
}
catch (Exception e)
{
Expand Down

0 comments on commit e47f024

Please sign in to comment.