Skip to content

Commit 87de526

Browse files
committed
Init cache on webapp startup
1 parent d68ac67 commit 87de526

3 files changed

Lines changed: 35 additions & 7 deletions

File tree

RP1AnalyticsWebApp/Services/CacheService.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ public class CacheService
1818

1919
private readonly HybridCache _cache;
2020
private readonly TelemetryClient _telemetry;
21+
private readonly ICareerLogDatabaseSettings _dbSettings;
2122

22-
public CacheService(HybridCache cache, TelemetryClient telemetry)
23+
public CacheService(HybridCache cache, TelemetryClient telemetry, ICareerLogDatabaseSettings dbSettings)
2324
{
2425
_cache = cache;
2526
_telemetry = telemetry;
27+
_dbSettings = dbSettings;
2628
}
2729

2830
public async Task<List<CareerLog>> FetchAllCareersAsync(IMongoCollection<CareerLog> careerLogs)
@@ -40,6 +42,17 @@ public async Task InvalidateAsync()
4042
await _cache.RemoveAsync(CacheKey);
4143
}
4244

45+
public async Task InitCacheAsync(CancellationToken ct = default)
46+
{
47+
_telemetry.TrackEvent("InitCacheAsync");
48+
IMongoCollection<CareerLog> careerLogs = CareerLogService.CreateCareerLogsMongoClient(_dbSettings);
49+
50+
await _cache.GetOrCreateAsync(
51+
CacheKey,
52+
FetchFromDB(careerLogs),
53+
cancellationToken: ct);
54+
}
55+
4356
private Func<CancellationToken, ValueTask<CachedCareerLogs>> FetchFromDB(IMongoCollection<CareerLog> careerLogs)
4457
{
4558
return async cancel =>

RP1AnalyticsWebApp/Services/CareerLogService.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ public CareerLogService(ICareerLogDatabaseSettings dbSettings, IContractSettings
3434
_userManager = userManager;
3535
_cache = cache;
3636

37+
_careerLogs = CreateCareerLogsMongoClient(dbSettings);
38+
}
39+
40+
public static IMongoCollection<CareerLog> CreateCareerLogsMongoClient(ICareerLogDatabaseSettings dbSettings)
41+
{
3742
var client = new MongoClient(dbSettings.ConnectionString);
3843
var database = client.GetDatabase(dbSettings.DatabaseName);
39-
_careerLogs = database.GetCollection<CareerLog>(dbSettings.CareerLogsCollectionName);
44+
return database.GetCollection<CareerLog>(dbSettings.CareerLogsCollectionName);
4045
}
4146

4247
public async Task<List<CareerLog>> GetAsync(ODataQueryOptions<CareerLog> queryOptions = null)

RP1AnalyticsWebApp/Services/StartupHostedService.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace RP1AnalyticsWebApp.Services
1111
public class StartupHostedService : IHostedService
1212
{
1313
private readonly IServiceProvider _serviceProvider;
14+
1415
public StartupHostedService(IServiceProvider serviceProvider)
1516
{
1617
_serviceProvider = serviceProvider;
@@ -19,24 +20,33 @@ public StartupHostedService(IServiceProvider serviceProvider)
1920
public async Task StartAsync(CancellationToken cancellationToken)
2021
{
2122
using IServiceScope scope = _serviceProvider.CreateScope();
22-
await EnsureRoles(scope);
23+
var rolesTask = EnsureRolesAsync(scope);
24+
_ = PreloadCacheAsync(scope, cancellationToken); // Start but do not wait for completion
25+
26+
await rolesTask;
2327
}
2428

2529
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
2630

27-
private static async Task EnsureRoles(IServiceScope scope)
31+
private static async Task EnsureRolesAsync(IServiceScope scope)
2832
{
2933
var _roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<MongoRole>>();
30-
await Task.WhenAll(EnsureRole(_roleManager, Constants.Roles.Admin),
31-
EnsureRole(_roleManager, Constants.Roles.Member));
34+
await Task.WhenAll(EnsureRoleAsync(_roleManager, Constants.Roles.Admin),
35+
EnsureRoleAsync(_roleManager, Constants.Roles.Member));
3236
}
3337

34-
private static async Task EnsureRole(RoleManager<MongoRole> _roleManager, string roleName)
38+
private static async Task EnsureRoleAsync(RoleManager<MongoRole> _roleManager, string roleName)
3539
{
3640
if (!await _roleManager.RoleExistsAsync(roleName))
3741
{
3842
await _roleManager.CreateAsync(new MongoRole(roleName));
3943
}
4044
}
45+
46+
private static async Task PreloadCacheAsync(IServiceScope scope, CancellationToken cancellationToken)
47+
{
48+
var cache = scope.ServiceProvider.GetRequiredService<CacheService>();
49+
await cache.InitCacheAsync(cancellationToken);
50+
}
4151
}
4252
}

0 commit comments

Comments
 (0)