1. 程式人生 > >asp.net core 快取

asp.net core 快取

快取在記憶體中 ASP.NET Core

public class HomeController : Controller
    {
        private IMemoryCache cache;

        public HomeController(IMemoryCache cache)
        {
             this.cache = cache;
        }

        public IActionResult Index()
        {
            cache.Set("name", $"shijia{DateTime.Now}", TimeSpan.FromSeconds(10));
            return View();
        }

        public IActionResult About()
        {
            object result;
            string value = cache.TryGetValue("name", out result) ? $"獲取快取name{result}" : "獲取快取失敗";
            return Content(value);
        }