@@ -12,30 +12,47 @@ 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" ;
17- private static readonly string Development = "Development" ;
1838
19- private readonly IPlayerRepository _playerRepository = playerRepository ;
20- private readonly ILogger < PlayerService > _logger = logger ;
21- private readonly IMemoryCache _memoryCache = memoryCache ;
22- private readonly IMapper _mapper = mapper ;
39+ /// <summary>
40+ /// The value used to check if the environment is Development.
41+ /// </summary>
42+ private static readonly string Development = "Development" ;
2343
2444 /* -------------------------------------------------------------------------
2545 * Create
2646 * ---------------------------------------------------------------------- */
2747
2848 public async Task < PlayerResponseModel > CreateAsync ( PlayerRequestModel playerRequestModel )
2949 {
30- var player = _mapper . Map < Player > ( playerRequestModel ) ;
31- await _playerRepository . AddAsync ( player ) ;
32- _logger . LogInformation ( "Player added to Repository: {Player}" , player ) ;
33- _memoryCache . Remove ( CacheKey_RetrieveAsync ) ;
34- _logger . LogInformation (
35- "Removed objects from Cache with Key: {Key}" ,
36- CacheKey_RetrieveAsync
37- ) ;
38- return _mapper . Map < PlayerResponseModel > ( player ) ;
50+ var player = mapper . Map < Player > ( playerRequestModel ) ;
51+ await playerRepository . AddAsync ( player ) ;
52+ logger . LogInformation ( "Player added to Repository: {Player}" , player ) ;
53+ memoryCache . Remove ( CacheKey_RetrieveAsync ) ;
54+ logger . LogInformation ( "Removed objects from Cache with Key: {Key}" , CacheKey_RetrieveAsync ) ;
55+ return mapper . Map < PlayerResponseModel > ( player ) ;
3956 }
4057
4158 /* -------------------------------------------------------------------------
@@ -44,48 +61,45 @@ public async Task<PlayerResponseModel> CreateAsync(PlayerRequestModel playerRequ
4461
4562 public async Task < List < PlayerResponseModel > > RetrieveAsync ( )
4663 {
47- if ( _memoryCache . TryGetValue ( CacheKey_RetrieveAsync , out List < PlayerResponseModel > ? cached ) )
64+ if ( memoryCache . TryGetValue ( CacheKey_RetrieveAsync , out List < PlayerResponseModel > ? cached ) )
4865 {
49- _logger . LogInformation ( "Players retrieved from Cache" ) ;
66+ logger . LogInformation ( "Players retrieved from Cache" ) ;
5067 return cached ! ;
5168 }
5269 else
5370 {
54- // Use multiple environments in ASP.NET Core
55- // https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-8.0
5671 if ( Environment . GetEnvironmentVariable ( AspNetCore_Environment ) == Development )
5772 {
5873 await SimulateRepositoryDelayAsync ( ) ;
5974 }
60-
61- var players = await _playerRepository . GetAllAsync ( ) ;
62- _logger . LogInformation ( "Players retrieved from Repository" ) ;
63- var playerResponseModels = _mapper . Map < List < PlayerResponseModel > > ( players ) ;
64- using ( var cacheEntry = _memoryCache . CreateEntry ( CacheKey_RetrieveAsync ) )
75+ var players = await playerRepository . GetAllAsync ( ) ;
76+ logger . LogInformation ( "Players retrieved from Repository" ) ;
77+ var playerResponseModels = mapper . Map < List < PlayerResponseModel > > ( players ) ;
78+ using ( var cacheEntry = memoryCache . CreateEntry ( CacheKey_RetrieveAsync ) )
6579 {
66- _logger . LogInformation (
80+ logger . LogInformation (
6781 "{Count} entries created in Cache with key: {Key}" ,
6882 playerResponseModels . Count ,
6983 CacheKey_RetrieveAsync
7084 ) ;
7185 cacheEntry . SetSize ( playerResponseModels . Count ) ;
7286 cacheEntry . Value = playerResponseModels ;
73- cacheEntry . SetOptions ( GetMemoryCacheEntryOptions ( ) ) ;
87+ cacheEntry . SetOptions ( CacheEntryOptions ) ;
7488 }
7589 return playerResponseModels ;
7690 }
7791 }
7892
7993 public async Task < PlayerResponseModel ? > RetrieveByIdAsync ( long id )
8094 {
81- var player = await _playerRepository . FindByIdAsync ( id ) ;
82- return player is not null ? _mapper . Map < PlayerResponseModel > ( player ) : null ;
95+ var player = await playerRepository . FindByIdAsync ( id ) ;
96+ return player is not null ? mapper . Map < PlayerResponseModel > ( player ) : null ;
8397 }
8498
8599 public async Task < PlayerResponseModel ? > RetrieveBySquadNumberAsync ( int squadNumber )
86100 {
87- var player = await _playerRepository . FindBySquadNumberAsync ( squadNumber ) ;
88- return player is not null ? _mapper . Map < PlayerResponseModel > ( player ) : null ;
101+ var player = await playerRepository . FindBySquadNumberAsync ( squadNumber ) ;
102+ return player is not null ? mapper . Map < PlayerResponseModel > ( player ) : null ;
89103 }
90104
91105 /* -------------------------------------------------------------------------
@@ -94,13 +108,13 @@ public async Task<List<PlayerResponseModel>> RetrieveAsync()
94108
95109 public async Task UpdateAsync ( PlayerRequestModel playerRequestModel )
96110 {
97- if ( await _playerRepository . FindByIdAsync ( playerRequestModel . Id ) is Player player )
111+ if ( await playerRepository . FindByIdAsync ( playerRequestModel . Id ) is Player player )
98112 {
99- _mapper . Map ( playerRequestModel , player ) ;
100- await _playerRepository . UpdateAsync ( player ) ;
101- _logger . LogInformation ( "Player updated in Repository: {Player}" , player ) ;
102- _memoryCache . Remove ( CacheKey_RetrieveAsync ) ;
103- _logger . LogInformation (
113+ mapper . Map ( playerRequestModel , player ) ;
114+ await playerRepository . UpdateAsync ( player ) ;
115+ logger . LogInformation ( "Player updated in Repository: {Player}" , player ) ;
116+ memoryCache . Remove ( CacheKey_RetrieveAsync ) ;
117+ logger . LogInformation (
104118 "Removed objects from Cache with Key: {Key}" ,
105119 CacheKey_RetrieveAsync
106120 ) ;
@@ -113,31 +127,18 @@ public async Task UpdateAsync(PlayerRequestModel playerRequestModel)
113127
114128 public async Task DeleteAsync ( long id )
115129 {
116- if ( await _playerRepository . FindByIdAsync ( id ) is not null )
130+ if ( await playerRepository . FindByIdAsync ( id ) is not null )
117131 {
118- await _playerRepository . RemoveAsync ( id ) ;
119- _logger . LogInformation ( "Player with Id {Id} removed from Repository" , id ) ;
120- _memoryCache . Remove ( CacheKey_RetrieveAsync ) ;
121- _logger . LogInformation (
132+ await playerRepository . RemoveAsync ( id ) ;
133+ logger . LogInformation ( "Player with Id {Id} removed from Repository" , id ) ;
134+ memoryCache . Remove ( CacheKey_RetrieveAsync ) ;
135+ logger . LogInformation (
122136 "Removed objects from Cache with Key: {Key}" ,
123137 CacheKey_RetrieveAsync
124138 ) ;
125139 }
126140 }
127141
128- /// <summary>
129- /// Creates a MemoryCacheEntryOptions instance with Normal priority,
130- /// SlidingExpiration of 10 minutes and AbsoluteExpiration of 1 hour.
131- /// </summary>
132- /// <returns>A MemoryCacheEntryOptions instance with the specified options.</returns>
133- private static MemoryCacheEntryOptions GetMemoryCacheEntryOptions ( )
134- {
135- return new MemoryCacheEntryOptions ( )
136- . SetPriority ( CacheItemPriority . Normal )
137- . SetSlidingExpiration ( TimeSpan . FromMinutes ( 10 ) )
138- . SetAbsoluteExpiration ( TimeSpan . FromHours ( 1 ) ) ;
139- }
140-
141142 /// <summary>
142143 /// Simulates a delay in the repository call to mimic a long-running operation.
143144 /// This is only used in the Development environment to simulate a delay
@@ -147,7 +148,7 @@ private static MemoryCacheEntryOptions GetMemoryCacheEntryOptions()
147148 private async Task SimulateRepositoryDelayAsync ( )
148149 {
149150 var milliseconds = new Random ( ) . Next ( 2600 , 4200 ) ;
150- _logger . LogInformation (
151+ logger . LogInformation (
151152 "Simulating a random delay of {Milliseconds} milliseconds..." ,
152153 milliseconds
153154 ) ;
0 commit comments