Skip to content

Commit

Permalink
Cache fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Jun 29, 2024
1 parent 7587acb commit 6d94a04
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Sample/Handlers/CachedRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Sample.Handlers;
[RegisterHandler]
public class CachedRequestHandler : IRequestHandler<CacheRequest, string>
{
[Cache]
[Cache(AbsoluteExpirationSeconds = 20)]
public Task<string> Handle(CacheRequest request, CancellationToken cancellationToken)
{
var r = DateTimeOffset.Now.ToString("h:mm:ss tt");
Expand Down
4 changes: 2 additions & 2 deletions src/Shiny.Mediator.Caching/CacheAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace Shiny.Mediator;
public class CacheAttribute : Attribute
{
public CacheItemPriority Priority { get; set; } = CacheItemPriority.Normal;
public int? AbsoluteExpirationSeconds { get; set; }
public int? SlidingExpirationSeconds { get; set; }
public int AbsoluteExpirationSeconds { get; set; }
public int SlidingExpirationSeconds { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,22 @@ CancellationToken cancellationToken
return await next().ConfigureAwait(false);

var cacheKey = this.GetCacheKey(request!, requestHandler);
var e = cache.CreateEntry(cacheKey);
e.Priority = cfg.Priority;
var result = await cache.GetOrCreateAsync<TResult>(
cacheKey,
entry =>
{
entry.Priority = cfg.Priority;

if (cfg.AbsoluteExpirationSeconds != null)
e.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(cfg.AbsoluteExpirationSeconds.Value);
if (cfg.AbsoluteExpirationSeconds > 0)
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(cfg.AbsoluteExpirationSeconds);

if (cfg.SlidingExpirationSeconds != null)
e.SlidingExpiration = TimeSpan.FromSeconds(cfg.SlidingExpirationSeconds.Value);

var result = await cache.GetOrCreateAsync<TResult>(
e,
_ => next()
if (cfg.SlidingExpirationSeconds > 0)
entry.SlidingExpiration = TimeSpan.FromSeconds(cfg.SlidingExpirationSeconds);

return next();
}
);

return result!;
}

Expand Down

0 comments on commit 6d94a04

Please sign in to comment.