Skip to content

Commit 4720a4e

Browse files
committed
refactor(services): move MemoryCacheEntryOptions init to static field
1 parent 45839ff commit 4720a4e

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

src/Dotnet.Samples.AspNetCore.WebApi/Services/PlayerService.cs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,33 @@ public class PlayerService(
1212
IMapper mapper
1313
) : IPlayerService
1414
{
15+
/// <summary>
16+
/// Creates a MemoryCacheEntryOptions instance with Normal priority,
17+
/// SlidingExpiration of 10 minutes and AbsoluteExpiration of 1 hour.
18+
/// </summary>
19+
private static readonly MemoryCacheEntryOptions CacheEntryOptions =
20+
new MemoryCacheEntryOptions()
21+
.SetPriority(CacheItemPriority.Normal)
22+
.SetSlidingExpiration(TimeSpan.FromMinutes(10))
23+
.SetAbsoluteExpiration(TimeSpan.FromHours(1));
24+
25+
/// <summary>
26+
/// The key used to store the list of Players in the cache.
27+
/// </summary>
1528
private static readonly string CacheKey_RetrieveAsync = nameof(RetrieveAsync);
29+
30+
/// <summary>
31+
/// The key used to store the environment variable for ASP.NET Core.
32+
/// <br/>
33+
/// <see href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-8.0">
34+
/// Use multiple environments in ASP.NET Core
35+
/// </see>
36+
/// </summary>
1637
private static readonly string AspNetCore_Environment = "ASPNETCORE_ENVIRONMENT";
38+
39+
/// <summary>
40+
/// The value used to check if the environment is Development.
41+
/// </summary>
1742
private static readonly string Development = "Development";
1843

1944
/* -------------------------------------------------------------------------
@@ -43,13 +68,10 @@ public async Task<List<PlayerResponseModel>> RetrieveAsync()
4368
}
4469
else
4570
{
46-
// Use multiple environments in ASP.NET Core
47-
// https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-8.0
4871
if (Environment.GetEnvironmentVariable(AspNetCore_Environment) == Development)
4972
{
5073
await SimulateRepositoryDelayAsync();
5174
}
52-
5375
var players = await playerRepository.GetAllAsync();
5476
logger.LogInformation("Players retrieved from Repository");
5577
var playerResponseModels = mapper.Map<List<PlayerResponseModel>>(players);
@@ -62,7 +84,7 @@ public async Task<List<PlayerResponseModel>> RetrieveAsync()
6284
);
6385
cacheEntry.SetSize(playerResponseModels.Count);
6486
cacheEntry.Value = playerResponseModels;
65-
cacheEntry.SetOptions(GetMemoryCacheEntryOptions());
87+
cacheEntry.SetOptions(CacheEntryOptions);
6688
}
6789
return playerResponseModels;
6890
}
@@ -117,19 +139,6 @@ public async Task DeleteAsync(long id)
117139
}
118140
}
119141

120-
/// <summary>
121-
/// Creates a MemoryCacheEntryOptions instance with Normal priority,
122-
/// SlidingExpiration of 10 minutes and AbsoluteExpiration of 1 hour.
123-
/// </summary>
124-
/// <returns>A MemoryCacheEntryOptions instance with the specified options.</returns>
125-
private static MemoryCacheEntryOptions GetMemoryCacheEntryOptions()
126-
{
127-
return new MemoryCacheEntryOptions()
128-
.SetPriority(CacheItemPriority.Normal)
129-
.SetSlidingExpiration(TimeSpan.FromMinutes(10))
130-
.SetAbsoluteExpiration(TimeSpan.FromHours(1));
131-
}
132-
133142
/// <summary>
134143
/// Simulates a delay in the repository call to mimic a long-running operation.
135144
/// This is only used in the Development environment to simulate a delay

0 commit comments

Comments
 (0)