Skip to content

Commit c94d6be

Browse files
authored
Merge pull request #101 from Virtual-Finland-Development/feat/improved-cachings
Improved Cachings
2 parents a822ace + ec11986 commit c94d6be

24 files changed

Lines changed: 195 additions & 34 deletions

Docs/README.adminfunction.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@ dotnet run --project ./VirtualFinland.UsersAPI.AdminFunction.CLI migrate
4444
- cli command: `dotnet run --project ./VirtualFinland.UsersAPI.AdminFunction.CLI update-terms-of-service`
4545
- `update-analytics`:
4646
- lambda function payload: `{"Action": "UpdateAnalytics"}`
47+
- `invalidate-caches`: invalidates the api gateway caches
48+
- lambda function payload: `{"Action": "InvalidateCaches"}`
49+
- cli command: `dotnet run --project ./VirtualFinland.UsersAPI.AdminFunction.CLI invalidate-caches`

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ update-terms-of-service:
1717
@echo "> Updating terms of service in database"
1818
dotnet run --project ./VirtualFinland.UsersAPI.AdminFunction.CLI update-terms-of-service
1919

20+
invalidate-caches:
21+
@echo "> Invalidating caches"
22+
dotnet run --project ./VirtualFinland.UsersAPI.AdminFunction.CLI invalidate-caches
23+
2024
packages: test
2125
@echo "> Ensuring local dependencies are installed"
2226
dotnet tool install -g Amazon.Lambda.Tools || true

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/Data/Repositories/CacheRepository.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@ public async Task<bool> Exists(string key)
4646

4747
public async Task Clear()
4848
{
49+
// Traverse all cluster endpoint servers
4950
var endpoints = _database.Multiplexer.GetEndPoints();
5051
foreach (var endpoint in endpoints)
5152
{
5253
var server = _database.Multiplexer.GetServer(endpoint);
53-
await server.FlushDatabaseAsync();
54+
55+
// Clear all keys with the keyPrefix
56+
var keys = server.Keys(pattern: $"{_keyPrefix}*");
57+
await _database.KeyDeleteAsync(keys.ToArray());
5458
}
5559
}
5660

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/Data/Repositories/CacheRepositoryFactory.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class CacheRepositoryFactory : ICacheRepositoryFactory
66
{
77
private readonly IDatabase _database;
88
private readonly string _factoryKeyPrefix;
9+
static readonly List<string> KnownKeys = new(); // Static list of all known cache repository keys
910

1011
public CacheRepositoryFactory(IDatabase database, string factoryKeyPrefix = "")
1112
{
@@ -16,6 +17,12 @@ public CacheRepositoryFactory(IDatabase database, string factoryKeyPrefix = "")
1617

1718
public ICacheRepository Create(string keyPrefix = "")
1819
{
19-
return new CacheRepository(_database, $"{_factoryKeyPrefix}{keyPrefix}");
20+
var cacheRepositoryKeyPrefix = $"{_factoryKeyPrefix}{keyPrefix}";
21+
22+
// Safety check to avoid accidentally overwriting existing cache repositories
23+
if (KnownKeys.Contains(cacheRepositoryKeyPrefix)) throw new ArgumentException($"Key prefix {cacheRepositoryKeyPrefix} already in use");
24+
KnownKeys.Add(cacheRepositoryKeyPrefix);
25+
26+
return new CacheRepository(_database, cacheRepositoryKeyPrefix);
2027
}
2128
}
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
using VirtualFinland.UserAPI.Helpers.Configurations;
22
using VirtualFinland.UserAPI.Helpers.Services;
3+
using VirtualFinland.UserAPI.Data.Repositories;
4+
using System.Reflection.Metadata;
35

46
namespace VirtualFinland.UserAPI.Helpers;
57

68
public abstract class CodesetsResourceRepository<T>
79
{
810
private readonly CodesetsService _codesetsService;
9-
private T? _resourceCache; // @TODO: Add caching suitable for AWS Lambda
11+
private ICacheRepository _cacheRepository;
1012
protected CodesetsResource? _resource;
1113

12-
public CodesetsResourceRepository(CodesetsService codesetsService)
14+
public CodesetsResourceRepository(CodesetsService codesetsService, ICacheRepositoryFactory cacheRepositoryFactory)
1315
{
1416
_codesetsService = codesetsService;
17+
_cacheRepository = cacheRepositoryFactory.Create(Constants.Cache.CodesetsPrefix);
1518
}
1619

1720
public async Task<T> GetResource(CodesetsResource? resource = null)
1821
{
19-
if (_resourceCache is not null)
22+
var resourceToGet = resource ?? _resource ?? throw new Exception("Resource not defined");
23+
24+
var cacheKey = resourceToGet.ToString();
25+
if (await _cacheRepository.Exists(cacheKey))
2026
{
21-
return _resourceCache;
27+
return await _cacheRepository.Get<T>(cacheKey);
2228
}
2329

24-
var resourceToGet = resource ?? _resource ?? throw new Exception("Resource not defined");
25-
_resourceCache = await _codesetsService.GetResource<T>(resourceToGet);
26-
return _resourceCache;
30+
var resolvedResource = await _codesetsService.GetResource<T>(resourceToGet);
31+
await _cacheRepository.Set<T>(cacheKey, resolvedResource, TimeSpan.FromHours(24));
32+
return resolvedResource;
2733
}
2834
}

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/Data/Repositories/CountriesRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace VirtualFinland.UserAPI.Data.Repositories;
77

88
public class CountriesRepository : CodesetsResourceRepository<List<Country>>, ICountriesRepository
99
{
10-
public CountriesRepository(CodesetsService codesetsService) : base(codesetsService)
10+
public CountriesRepository(CodesetsService codesetsService, ICacheRepositoryFactory cacheRepositoryFactory) : base(codesetsService, cacheRepositoryFactory)
1111
{
1212
_resource = CodesetsResource.Countries;
1313
}

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/Data/Repositories/ISecurityCacheRepository.cs renamed to VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/Data/Repositories/ICacheRepositoryFactory.cs

File renamed without changes.

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/Data/Repositories/LanguageRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace VirtualFinland.UserAPI.Data.Repositories;
77

88
public class LanguageRepository : CodesetsResourceRepository<List<Language>>, ILanguageRepository
99
{
10-
public LanguageRepository(CodesetsService codesetsService) : base(codesetsService)
10+
public LanguageRepository(CodesetsService codesetsService, ICacheRepositoryFactory cacheRepositoryFactory) : base(codesetsService, cacheRepositoryFactory)
1111
{
1212
_resource = CodesetsResource.Languages;
1313
}

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/Data/Repositories/OccupationsFlatRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace VirtualFinland.UserAPI.Data.Repositories;
77

88
public class OccupationsFlatRepository : CodesetsResourceRepository<List<OccupationFlatRoot.Occupation>>, IOccupationsFlatRepository
99
{
10-
public OccupationsFlatRepository(CodesetsService codesetsService) : base(codesetsService)
10+
public OccupationsFlatRepository(CodesetsService codesetsService, ICacheRepositoryFactory cacheRepositoryFactory) : base(codesetsService, cacheRepositoryFactory)
1111
{
1212
_resource = CodesetsResource.OccupationsFlat;
1313
}

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/Data/Repositories/OccupationsRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace VirtualFinland.UserAPI.Data.Repositories;
77

88
public class OccupationsRepository : CodesetsResourceRepository<List<OccupationRoot.Occupation>>, IOccupationsRepository
99
{
10-
public OccupationsRepository(CodesetsService codesetsService) : base(codesetsService)
10+
public OccupationsRepository(CodesetsService codesetsService, ICacheRepositoryFactory cacheRepositoryFactory) : base(codesetsService, cacheRepositoryFactory)
1111
{
1212
_resource = CodesetsResource.Occupations;
1313
}

0 commit comments

Comments
 (0)