HI mọi người cho mình hỏi vấn đề về website asp.net mvc, mình đang sử udngj cache của OutputCache mà mỗi lần xử lý xóa cache tìm hiểu mệt quá, nó không quản lý được và xóa được hay sao ấy. Mình đã tham khảo nhiều cách xóa cache như ở dưới có để code, mà không thể xóa được, bạn nào cho mình biết cách Clear xóa cache với. Mã: var urlToRemove = Url.Action("AjaxHtmlOutputMethod", "Controller"); HttpResponse.RemoveOutputCacheItem(urlToRemove); Mã: [OutputCache (Duration=3600, VaryByParam="param1;param2", Location = OutputCacheLocation.Server)] public string AjaxHtmlOutputMethod(string param1, string param2) { object routeValues = new { param1 = param1, param2 = param2 }; string url = Url.Action("AjaxHtmlOutputMethod", "Controller", routeValues); Response.RemoveOutputCacheItem(url); }
OutputCache bạn xóa cache không được theo ý của bạn đây nhé, nó tự động xóa, clear, refresh nếu expire time hoặc reset pool ở IIS, upload lại dll project. Bạn tham khảo Memory Cache hoặc Distributed Cache nó sẽ cho bạn quản lý và xóa cache dễ dàng hơn và được sử dụng nhiều hơn ở các ứng dụng lớn. Còn OutputCache chỉ áp dụng cho các web nhỏ, hoặc bạn sử dụng PartialView cho menu, danh sách bài liên quan, các sản phẩm top thì áp dụng tốt, còn bạn xài cho View ActionResult sẽ khó quản lý việc xóa cache và quản lý nó.
Bạn xem ở link sau: https://www.nuget.org/packages/system.runtime.caching/ Code mình tham khảo tạo theo cũng khá ok và đơn giản để cache database: https://drive.google.com/file/d/1e0UUY1p_pmCjGf9YNuBNhzC6YfxGLOTV/view?usp=sharing