asp.net中缓存的使用

刚学到asp.net怎么缓存,这里推荐学习一下

www.cnblogs.com/wang726zq/archive/2012/09/06/cache.html

http://blog.csdn.net/sundacheng1989/article/details/10001315

以下是键值缓存依赖 的实例,可以慢慢研究其中的原理

 public ActionResult SetKey1(){
            CacheDependency dep=new CacheDependency(null,new string[]{"key2"});
            HttpRuntime.Cache.Insert("key1",DateTime.Now.ToString(),dep,Cache.NoAbsoluteExpiration,Cache.NoSlidingExpiration);
            return Content("设置完毕");
        }
        public ActionResult GetKey1(){
            if (HttpRuntime.Cache["key1"] != null)
                return Content(HttpRuntime.Cache["key1"].ToString());
            else
                return Content("空");
        }
        public ActionResult SetKey2(){
            HttpRuntime.Cache.Insert("key2",DateTime.Now.ToString());
            return Content("设置完毕");
        }
        public ActionResult GetKey2(){
            if (HttpRuntime.Cache["key2"] != null)
                return Content(HttpRuntime.Cache["key2"].ToString());
            else
                return Content("空");
        }
<div id="result"></div>
<br />

@Ajax.ActionLink("设置key1", "SetKey1", new AjaxOptions() { InsertionMode = InsertionMode.Replace, UpdateTargetId = "result" })
@Ajax.ActionLink("获取key1", "GetKey1", new AjaxOptions() { InsertionMode = InsertionMode.Replace, UpdateTargetId = "result" })
@Ajax.ActionLink("设置key2", "SetKey2", new AjaxOptions() { InsertionMode = InsertionMode.Replace, UpdateTargetId = "result" })
@Ajax.ActionLink("获取key2", "GetKey2", new AjaxOptions() { InsertionMode = InsertionMode.Replace, UpdateTargetId = "result" })


 以上如果设置了key1和key2后,都能获取到cacke中的值,如果点击设置key2,这时点击获取key1就无法获得值,就是key1值是依赖key2的,如果key2值发生改变,则key1值就会自动被清空。

利用cache特性,在网站中可以存储一些经常用到而有不频繁改变值的数据,会提高网站效率,比如电商网站中的商品分类项,不会经常改变,但会每次访问都有可能显示的分类列表,这里用cache是最好的选择。

原文地址:https://www.cnblogs.com/lunawzh/p/5219430.html