|
1 | | -using System.Linq; |
2 | | -using System.Threading.Tasks; |
3 | | -using Microsoft.AspNetCore.Mvc; |
4 | | -using Microsoft.Extensions.Logging; |
5 | | -using DistributedCache.Services; |
6 | | - |
7 | | -namespace DistributedCache.Controllers |
8 | | -{ |
9 | | - public class HomeController : Controller |
10 | | - { |
11 | | - private readonly ILogger<HomeController> _logger; |
12 | | - private readonly IUserService _userService; |
13 | | - private readonly ICacheUserService _cacheUserService; |
14 | | - |
15 | | - public HomeController(ILogger<HomeController> logger, IUserService userService, ICacheUserService cacheUserService) |
16 | | - { |
17 | | - _logger = logger; |
18 | | - _userService = userService; |
19 | | - _cacheUserService = cacheUserService; |
20 | | - } |
21 | | - |
22 | | - public async Task<IActionResult> Index() |
23 | | - { |
24 | | - var users = (await _cacheUserService.GetCachedUser())?.FirstOrDefault(); |
25 | | - return View(users); |
26 | | - } |
27 | | - |
28 | | - public async Task<IActionResult> CacheUserAsync() |
29 | | - { |
30 | | - var users = await _userService.GetUsersAsync(); |
31 | | - var cachedEntry = users.First(); |
32 | | - return View(nameof(Index), cachedEntry); |
33 | | - } |
34 | | - |
35 | | - public IActionResult CacheRemoveAsync() |
36 | | - { |
37 | | - _cacheUserService.ClearCache(); |
38 | | - return RedirectToAction(nameof(Index)); |
39 | | - } |
40 | | - } |
41 | | -} |
| 1 | +using Microsoft.AspNetCore.Mvc; |
| 2 | +using DistributedCache.Services; |
| 3 | + |
| 4 | +namespace DistributedCache.Controllers; |
| 5 | + |
| 6 | +public class HomeController( |
| 7 | + IUserService userService, |
| 8 | + ICacheUserService cacheUserService) : Controller |
| 9 | +{ |
| 10 | + private readonly IUserService _userService = userService; |
| 11 | + private readonly ICacheUserService _cacheUserService = cacheUserService; |
| 12 | + |
| 13 | + public async Task<IActionResult> Index() |
| 14 | + { |
| 15 | + var user = (await _cacheUserService.GetCachedUserAsync()).FirstOrDefault(); |
| 16 | + return View(user); |
| 17 | + } |
| 18 | + |
| 19 | + public async Task<IActionResult> CacheUserAsync() |
| 20 | + { |
| 21 | + var users = await _userService.GetUsersAsync(); |
| 22 | + return View(nameof(Index), users.FirstOrDefault()); |
| 23 | + } |
| 24 | + |
| 25 | + public async Task<IActionResult> CacheRemoveAsync() |
| 26 | + { |
| 27 | + await _cacheUserService.ClearCacheAsync(); |
| 28 | + return RedirectToAction(nameof(Index)); |
| 29 | + } |
| 30 | +} |
0 commit comments