{"id":1651,"date":"2013-10-22T18:08:03","date_gmt":"2013-10-22T18:08:03","guid":{"rendered":"http:\/\/galhano.com\/blog\/?p=1651"},"modified":"2013-10-22T19:38:16","modified_gmt":"2013-10-22T19:38:16","slug":"using-cache-in-asp-net","status":"publish","type":"post","link":"http:\/\/galhano.com\/blog\/?p=1651","title":{"rendered":"Using &#8220;Cache&#8221; in ASP.Net"},"content":{"rendered":"<div dir=\"ltr\">\u00a0Data can be stored on the client &#8211; using the <strong>view state<\/strong> of the page, or on the server in a variable <strong>session state<\/strong> or <strong>application<\/strong>, or using the server <strong>cache<\/strong>.<\/div>\n<div dir=\"ltr\"><\/div>\n<div dir=\"ltr\">ASP.NET implements <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.web.caching.cache.aspx\"><strong>System.Web.Caching.Cache<\/strong><\/a> class to store objects that require a large amount of server resources to be created, so that they do not have to be recreated each time it is needed.<\/div>\n<div dir=\"ltr\"><\/div>\n<div dir=\"ltr\">You can access via code information about a class instance cache through the property <strong>cache<\/strong>, the object of the<strong> HttpContext<\/strong> or <strong>Page object<\/strong>.<\/div>\n<div dir=\"ltr\"><\/div>\n<div dir=\"ltr\"><strong>Expiration<\/strong> policy for items in cache:<\/div>\n<ul>\n<li>Specific time:\u00a0 <span style=\"font-family: Trebuchet MS;\"><strong>absolute expiration<\/strong><\/span><\/li>\n<li>May also expire if not accessed for a period of time:\u00a0 <span style=\"font-family: Trebuchet MS;\"><strong>sliding expiration<\/strong><\/span><\/li>\n<\/ul>\n<p><!--more--><\/p>\n<p><strong>Insert Into cache<\/strong><\/p>\n<p>[vb]<br \/>\n Cache.Insert(&quot;myDataSet&quot;, ds, Nothing, DateTime.Now.AddSeconds(30), System.TimeSpan.Zero)<br \/>\n[\/vb]<\/p>\n<p><strong>Using Cache:<\/strong><\/p>\n<p>[vb]<br \/>\nIf Cache(&quot;myDataSet&quot;) Is Nothing Then<br \/>\n\tMyDataSet()<br \/>\n\t&#8216;DataSet from database<br \/>\nElse<br \/>\n\tds = CType(Cache(&quot;myDataSet&quot;), DataSet)<br \/>\n\t&#8216; DataSet from cache.<br \/>\nEnd If<br \/>\n[\/vb]<\/p>\n<p><strong>Clear Cache:<\/strong><\/p>\n<p>[vb]<br \/>\n  Cache.Remove(&quot;myDataSet&quot;)<br \/>\n[\/vb]<\/p>\n<p><strong>Clear ALL Cache vars:<\/strong><\/p>\n<p>[vb]<br \/>\n        Dim enumerator As IDictionaryEnumerator = HttpContext.Current.Cache.GetEnumerator()<br \/>\n        While enumerator.MoveNext()<br \/>\n            HttpContext.Current.Cache.Remove(enumerator.Key)<br \/>\n        End While<br \/>\n[\/vb]<\/p>\n<p>Note that, this technique is not efficient for pages with OutputCache type. for clearing this type cached memory, you should use below code line.<\/p>\n<p>[vb]<br \/>\nHttpRuntime.Close()<br \/>\n[\/vb]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0Data can be stored on the client &#8211; using the view state of the page, or on the server in a variable session state or application, or using the server cache. ASP.NET implements System.Web.Caching.Cache class to store objects that require a large amount of server resources to be created, so that they do not have [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"close","ping_status":"close","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-1651","post","type-post","status-publish","format-standard","hentry","category-aspnet","author-admin"],"_links":{"self":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1651"}],"version-history":[{"count":12,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1651\/revisions"}],"predecessor-version":[{"id":1653,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1651\/revisions\/1653"}],"wp:attachment":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1651"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}