{"id":166,"date":"2017-09-27T16:24:03","date_gmt":"2017-09-27T16:24:03","guid":{"rendered":"http:\/\/td_uid_86_59cbd0a39939f"},"modified":"2019-03-16T12:52:35","modified_gmt":"2019-03-16T05:52:35","slug":"cache-trong-mvc","status":"publish","type":"post","link":"https:\/\/thietkewebdalat.net\/blog\/cache-trong-mvc\/","title":{"rendered":"Cache trong mvc"},"content":{"rendered":"<p>Xin ch\u00e0o c\u00e1c b\u1ea1n<\/p>\n<p>H\u00f4m nay m\u00ecnh s\u1ebd gi\u1edbi thi\u1ec7u \u0111\u1ebfn c\u00e1c b\u1ea1n v\u1ec1 cache hay d\u00f9ng nh\u1ea5t trong mvc.Trong mvc ng\u01b0\u1eddi ta hay d\u00f9ng nh\u1ea5t l\u00e0\u00a0<strong>OutputCache.<\/strong><\/p>\n<p>V\u1ec1 l\u00fd thuy\u1ebft th\u00ec m\u00ecnh c\u0169ng kh\u00f4ng n\u00f3i nhi\u1ec1u ,c\u00e1c b\u1ea1n c\u00f3 th\u1ec3 t\u00ecm \u0111\u1ecdc ,h\u00f4m nay m\u00ecnh s\u1ebd l\u1ea5y code ra l\u00e0m v\u00ed d\u1ee5 cho c\u00e1c b\u1ea1n d\u1ec5 hi\u1ec3u tr\u01b0\u1edbc khi b\u1eaft \u0111\u1ea7u m\u00ecnh s\u1ebd n\u00f3i s\u01a1 qua nh\u1eefng t\u1eeb kh\u00f3a s\u1eed d\u1ee5ng trong\u00a0<strong>OutputCache.<\/strong><\/p>\n<ul>\n<li><strong>Duration : <\/strong>\u0110\u00e2y l\u00e0 th\u1eddi gian quy \u0111\u1ecbnh cache s\u1ebd \u0111\u01b0\u1ee3c l\u00e0m m\u1edbi l\u1ea1i 1 l\u1ea7n ,\u0111\u01a1n v\u1ecb n\u00f3 s\u1ebd t\u00ednh b\u1eb1ng gi\u00e2y.<\/li>\n<li><strong>VaryByPara: <\/strong>Cache s\u1ebd l\u01b0u tr\u1eef tr\u00ean 1 tham s\u1ed1 ,n\u00f3i n\u00f4m na l\u00e0 khi b\u1ea1n vi\u1ebft 1 h\u00e0m tr\u00ean controller m\u00e0 h\u00e0m \u0111\u00f3 c\u00f3 d\u00f9ng tham s\u1ed1 th\u00ec b\u1ea1n truy\u1ec1n tham s\u1ed1 \u0111\u00f3 v\u00e0o.<\/li>\n<li><strong>Location : \u0110\u00e2y l\u00e0 v\u1ecb tr\u00ed l\u01b0u c\u1ee7a cache ,n\u00f3 c\u00f3 5 v\u1ecb tr\u00ed l\u01b0u :<\/strong>\n<ul>\n<li><strong><del>Any<\/del> <\/strong>(m\u1eb7c \u0111\u1ecbnh):N\u1ed9i dung \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef trong ba locations- Web Server, b\u1ea5t k\u1ef3 m\u00e1y ch\u1ee7 proxy v\u00e0 tr\u00ecnh duy\u1ec7t Web.<\/li>\n<li><strong>Client<\/strong>: N\u1ed9i dung \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef tr\u00ean Tr\u00ecnh duy\u1ec7t web<\/li>\n<li><strong>Server<\/strong>: N\u1ed9i dung \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef tr\u00ean M\u00e1y ch\u1ee7 Web.<\/li>\n<li><strong>ServerAndClient<\/strong>: N\u1ed9i dung \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef tr\u00ean M\u00e1y ch\u1ee7 Web v\u00e0 Tr\u00ecnh duy\u1ec7t web.<\/li>\n<li><strong>None<\/strong> :\u00a0N\u1ed9i dung kh\u00f4ng \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef \u1edf b\u1ea5t c\u1ee9 \u0111\u00e2u.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Sau \u0111\u00e2y l\u00e0 v\u00ed d\u1ee5 :<\/p>\n<ul>\n<li>Th\u1eddi gian l\u01b0u l\u00e0 60 gi\u00e2y ,tham s\u1ed1 l\u00e0 None l\u01b0u \u1edf Client<\/li>\n<\/ul>\n<p><code><br \/>\n[OutputCache(Duration = 60, VaryByParam = \"None\", , Location = OutputCacheLocation.Client)]<br \/>\npublic ActionResult Index(int Id) {<br \/>\nreturn View();<br \/>\n}<br \/>\n<\/code><\/p>\n<ul>\n<li>Th\u1eddi gian l\u01b0u l\u00e0 60 gi\u00e2y ,tham s\u1ed1 l\u00e0 Id l\u01b0u \u1edf Client<\/li>\n<\/ul>\n<p><code><br \/>\n[OutputCache(Duration = 60, VaryByParam = \"Id\", , Location = OutputCacheLocation.Client)]<br \/>\npublic ActionResult Index(int Id) {<br \/>\nreturn View();<br \/>\n}<br \/>\n<\/code><\/p>\n<ul>\n<li>Th\u1eddi gian l\u01b0u l\u00e0 60 gi\u00e2y ,tham s\u1ed1 l\u00e0 Id,SLug l\u01b0u \u1edf Client<\/li>\n<\/ul>\n<p><code><br \/>\n[OutputCache(Duration = 60, VaryByParam = \"Id;Slug\", , Location = OutputCacheLocation.Client)]<br \/>\npublic ActionResult Index(int Id) {<br \/>\nreturn View();<br \/>\n}<br \/>\n<\/code><\/p>\n<p>N\u1ebfu nh\u01b0 c\u00e1c b\u1ea1n th\u1ea5y n\u00f3 d\u00e0i qu\u00e1 th\u00ec m\u00ecnh c\u00f3 th\u1ec3 r\u00fat g\u1ecdn l\u1ea1i v\u1edbi vi\u1ec7c d\u00f9ng\u00a0<strong>CacheProfile<\/strong><\/p>\n<p>C\u00e1c b\u1ea1n th\u00eam v\u00e0o trong webconfig nh\u01b0 sau<\/p>\n<p>&lt;caching&gt;<\/p>\n<p>&lt;outputCacheSettings&gt;<\/p>\n<p>&lt;outputCacheProfiles&gt;<\/p>\n<p>&lt;add\u00a0name=<span class=\"string\">&#8220;Long&#8221;<\/span>\u00a0duration=<span class=\"string\">&#8220;60&#8221;<\/span>\u00a0varyByParam=<span class=\"string\">&#8220;Id&#8221;<\/span>\u00a0\/&gt;<\/p>\n<p>&lt;add\u00a0name=<span class=\"string\">&#8220;Medium&#8221;<\/span>\u00a0duration=<span class=\"string\">&#8220;60&#8221;<\/span>\u00a0varyByParam=<span class=\"string\">&#8220;Id;SLug&#8221;<\/span>\u00a0\/&gt;<\/p>\n<p>&lt;add\u00a0name=<span class=\"string\">&#8220;Short&#8221;<\/span>\u00a0duration=<span class=\"string\">&#8220;10&#8221;<\/span>\u00a0varyByParam=<span class=\"string\">&#8220;none&#8221;<\/span>\u00a0\/&gt;<\/p>\n<p>&lt;\/outputCacheProfiles&gt;<\/p>\n<p>&lt;\/outputCacheSettings&gt;<\/p>\n<p>&lt;\/caching&gt;<\/p>\n<p>V\u00e0 b\u1ea1n s\u1eed d\u1ee5ng :<\/p>\n<p>[OutputCache(CacheProfile\u00a0=\u00a0<span class=\"string\">&#8220;Long&#8221;<\/span>)]<\/p>\n<p><span class=\"keyword\">public<\/span>\u00a0ActionResult\u00a0Index(<span class=\"keyword\">int<\/span>\u00a0Id)\u00a0{<\/p>\n<p><span class=\"keyword\">return<\/span>\u00a0View(emps);<\/p>\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Xin ch\u00e0o c\u00e1c b\u1ea1n H\u00f4m nay m\u00ecnh s\u1ebd gi\u1edbi thi\u1ec7u \u0111\u1ebfn c\u00e1c b\u1ea1n v\u1ec1 cache hay d\u00f9ng nh\u1ea5t trong mvc.Trong mvc ng\u01b0\u1eddi ta hay d\u00f9ng nh\u1ea5t l\u00e0\u00a0OutputCache. V\u1ec1 l\u00fd thuy\u1ebft th\u00ec m\u00ecnh c\u0169ng kh\u00f4ng n\u00f3i nhi\u1ec1u ,c\u00e1c b\u1ea1n c\u00f3 th\u1ec3 t\u00ecm \u0111\u1ecdc ,h\u00f4m nay m\u00ecnh s\u1ebd l\u1ea5y code ra l\u00e0m v\u00ed d\u1ee5 cho c\u00e1c b\u1ea1n [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":268,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"amp_status":"","footnotes":""},"categories":[8,10],"tags":[97,96],"class_list":{"0":"post-166","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-net","8":"category-mvc","9":"tag-cache-trogn-mvc","10":"tag-outputcache-trong-mvc"},"_links":{"self":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts\/166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/comments?post=166"}],"version-history":[{"count":2,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts\/166\/revisions"}],"predecessor-version":[{"id":269,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts\/166\/revisions\/269"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/media\/268"}],"wp:attachment":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/media?parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/categories?post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/tags?post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}