A cache key identifies the cached result for one request or data variant. In a board-game API, different GET parameters must produce different keys so one request cannot receive another request’s data. section 8.3.3
Implement a request-specific cache key by:
- Creating a string from the request DTO’s type name and its JSON-serialized value. section 8.3.3
- Checking the in-memory cache for that key before querying EF Core. section 8.3.3
- Caching the retrieved board-game array for 30 seconds when no matching entry exists. section 8.3.3
Board-game GET request
A controller can form a key such as input.GetType() plus JsonSerializer.Serialize(input), then use TryGetValue<BoardGame[]> to reuse the matching result. section 8.3.3
Listing 8.6 BoardGamesController’s Get method
[HttpGet(Name = "GetBoardGames")]
[ResponseCache(CacheProfileName = "Any-60")]
public async Task<RestDTO<BoardGame[]>> Get(
[FromQuery] RequestDTO<BoardGameDTO> input)
{
_logger.LogInformation(CustomLogEvents.BoardGamesController_Get,
"Get method started.");
var query = _context.BoardGames.AsQueryable(); #1
if (!string.IsNullOrEmpty(input.FilterQuery)) #2
query = query.Where(b => b.Name.Contains(input.FilterQuery));
var recordCount = await query.CountAsync(); #3
BoardGame[]? result = null; #4
var cacheKey = $"{input.GetType()}-{JsonSerializer.Serialize(input)}";#5
if (!_memoryCache.TryGetValue<BoardGame[]>(cacheKey, out result)) #6
{
query = query #7
.OrderBy($"{input.SortColumn} {input.SortOrder}")
.Skip(input.PageIndex * input.PageSize)
.Take(input.PageSize);
result = await query.ToArrayAsync();
_memoryCache.Set(cacheKey, result, new TimeSpan(0, 0, 30));
}
return new RestDTO<BoardGame[]>()
{
Data = result, #8
PageIndex = input.PageIndex,
PageSize = input.PageSize,
RecordCount = recordCount,
Links = new List<LinkDTO> {
new LinkDTO(
Url.Action(
null,
"BoardGames",
new { input.PageIndex, input.PageSize },
Request.Scheme)!,
"self",
"GET"),
}
};
}
Inspect the controller listing to follow key creation, cache lookup, and conditional EF Core retrieval in sequence. section 8.3.3


