Skip to content

Commit 5f2f411

Browse files
committed
Fixed targeting of LoggingBehavior (need to fix in sharedkernel as well)
Minor other updates
1 parent 9e9e9a0 commit 5f2f411

File tree

17 files changed

+105
-21
lines changed

17 files changed

+105
-21
lines changed

sample/src/NimblePros.SampleToDo.Core/ProjectAggregate/Handlers/ItemCompletedEmailNotificationHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public ItemCompletedEmailNotificationHandler(IEmailSender emailSender)
1515

1616
// configure a test email server to demo this works
1717
// https://ardalis.com/configuring-a-local-test-email-server
18-
public async ValueTask Handle(ToDoItemCompletedEvent domainEvent, CancellationToken cancellationToken)
18+
public async ValueTask Handle(ToDoItemCompletedEvent domainEvent,
19+
CancellationToken cancellationToken)
1920
{
2021
Guard.Against.Null(domainEvent, nameof(domainEvent));
2122

sample/src/NimblePros.SampleToDo.Infrastructure/Data/EfRepository.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
namespace NimblePros.SampleToDo.Infrastructure.Data;
44

55
// inherit from Ardalis.Specification type
6-
public class EfRepository<T> : RepositoryBase<T>, IReadRepository<T>, IRepository<T> where T : class, IAggregateRoot
6+
public class EfRepository<T> : RepositoryBase<T>, IReadRepository<T>, IRepository<T>
7+
where T : class, IAggregateRoot
78
{
89
public EfRepository(AppDbContext dbContext) : base(dbContext)
910
{

sample/src/NimblePros.SampleToDo.UseCases/Contributors/Queries/Get/GetContributorQuery.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace NimblePros.SampleToDo.UseCases.Contributors.Queries.Get;
44

55
public record GetContributorQuery(ContributorId ContributorId) : IQuery<Result<ContributorDto>>, ICacheable
66
{
7+
public string? CacheProfile => null;
8+
79
public string GetCacheKey()
810
{
911
return $"{nameof(GetContributorQuery)}-{ContributorId.Value}";

sample/src/NimblePros.SampleToDo.UseCases/Contributors/Queries/List/ListContributorsQuery.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
public record ListContributorsQuery(int? Page = 1, int? PerPage = Constants.DEFAULT_PAGE_SIZE)
44
: IQuery<Result<PagedResult<ContributorDto>>>, ICacheable
55
{
6+
public string? CacheProfile => "Long";
7+
68
public string GetCacheKey()
79
{
810
return $"{nameof(ListContributorsQuery)}-{Page}-{PerPage}";

sample/src/NimblePros.SampleToDo.UseCases/ICacheable.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
public interface ICacheable
33
{
44
string GetCacheKey();
5+
string? CacheProfile { get; } // TODO: Use SmartEnum for this with CacheProfile.None
56
}

sample/src/NimblePros.SampleToDo.UseCases/Projects/GetWithAllItems/GetProjectWithAllItemsQuery.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace NimblePros.SampleToDo.UseCases.Projects.GetWithAllItems;
44

5-
public record GetProjectWithAllItemsQuery(ProjectId ProjectId) : IQuery<Result<ProjectWithAllItemsDto>>, ICacheable
5+
public record GetProjectWithAllItemsQuery(ProjectId ProjectId) :
6+
IQuery<Result<ProjectWithAllItemsDto>>, ICacheable
67
{
8+
public string? CacheProfile => "Short";
9+
710
public string GetCacheKey()
811
{
912
return $"{nameof(GetProjectWithAllItemsQuery)}-{ProjectId}";

sample/src/NimblePros.SampleToDo.UseCases/Projects/ListIncompleteItems/ListIncompleteItemsByProjectQuery.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
namespace NimblePros.SampleToDo.UseCases.Projects.ListIncompleteItems;
22

3-
public record ListIncompleteItemsByProjectQuery(int ProjectId) : IQuery<Result<IEnumerable<ToDoItemDto>>>, ICacheable
3+
public record ListIncompleteItemsByProjectQuery(int ProjectId) :
4+
IQuery<Result<IEnumerable<ToDoItemDto>>>, ICacheable
45
{
6+
public string? CacheProfile => null;
7+
58
public string GetCacheKey()
69
{
710
return $"{nameof(ListIncompleteItemsByProjectQuery)}-{ProjectId}";

sample/src/NimblePros.SampleToDo.UseCases/Projects/ListShallow/ListProjectsShallowQuery.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
namespace NimblePros.SampleToDo.UseCases.Projects.ListShallow;
22

3-
public record ListProjectsShallowQuery(int? Skip, int? Take) : IQuery<Result<IEnumerable<ProjectDto>>>, ICacheable
3+
public record ListProjectsShallowQuery(int? Skip, int? Take) :
4+
IQuery<Result<IEnumerable<ProjectDto>>>, ICacheable
45
{
6+
public string? CacheProfile => null;
7+
58
public string GetCacheKey()
69
{
710
return $"{nameof(ListProjectsShallowQuery)}-{Skip}-{Take}";

sample/src/NimblePros.SampleToDo.Web/CachingBehavior.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ public class CachingBehavior<TRequest, TResponse>(IMemoryCache cache,
1616
private readonly ILogger<CachingBehavior<TRequest, TResponse>> _logger = logger;
1717
private readonly CachingOptions _cachingOptions = cachingOptions.Value;
1818

19-
private MemoryCacheEntryOptions _cacheOptions =>
20-
new MemoryCacheEntryOptions()
21-
.SetAbsoluteExpiration(TimeSpan.FromSeconds(_cachingOptions.DefaultDurationSeconds));
22-
2319
public async ValueTask<TResponse?> Handle(
2420
TRequest request,
2521
Mediator.MessageHandlerDelegate<TRequest, TResponse?> next,
@@ -29,12 +25,27 @@ public class CachingBehavior<TRequest, TResponse>(IMemoryCache cache,
2925

3026
if (request is not ICacheable cacheable) return await next(request, cancellationToken);
3127

32-
var cacheKey = cacheable.GetCacheKey();
28+
int cacheDurationSeconds = _cachingOptions.DefaultDurationSeconds;
29+
if (cacheable.CacheProfile != null)
30+
{
31+
var profile = _cachingOptions.Profiles.FirstOrDefault(p => p.Name.Equals(cacheable.CacheProfile, StringComparison.OrdinalIgnoreCase));
32+
if (profile != null)
33+
{
34+
_logger.LogInformation("Using cache profile {ProfileName} with duration {DurationSeconds} seconds.", profile.Name, profile.CacheDurationSeconds);
35+
cacheDurationSeconds = profile?.CacheDurationSeconds ?? _cachingOptions.DefaultDurationSeconds;
36+
}
37+
}
38+
var cacheOptions =
39+
new MemoryCacheEntryOptions()
40+
.SetAbsoluteExpiration(TimeSpan.FromSeconds(cacheDurationSeconds));
41+
42+
43+
var cacheKey = cacheable.GetCacheKey();
3344
_logger.LogDebug("Checking cache for {CacheKey}", cacheKey);
3445
return await _cache.GetOrCreateAsync(cacheKey, async entry =>
3546
{
3647
_logger.LogInformation($"Cache miss. Getting data from database. ({cacheKey})");
37-
entry.SetOptions(_cacheOptions);
48+
entry.SetOptions(cacheOptions);
3849
return await next(request, cancellationToken);
3950
});
4051
}

sample/src/NimblePros.SampleToDo.Web/Configurations/CachingOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ public class CachingOptions
44
{
55
public const string SectionName = "Caching";
66
public int DefaultDurationSeconds { get; set; } = 10;
7+
public List<CachingProfile> Profiles { get; set; } = [];
78
}

0 commit comments

Comments
 (0)