asp.net 提供一个功能完整的缓存引擎,页面可使用该引擎通过 http 请求存储与检索任意对象.
【程序编程相关:正则表达式语法】
缓存的生存期与应用程序的生存期相同,也就是说,当应用程序重新启动时,将重新创建缓存. 【推荐阅读:浅谈在ASP.NET中数据有效性校验的方】1.通过指定其键与值将项添加到缓存中 【扩展信息:ZPS 4.0.1 is Availab】
将数据添加到缓存中cache["txt"] = "a";2.通过使用 insert(重载insert方法)方法将项添加到缓存中
cache.insert("txt", "a");
下列代码显示如何设置相对过期策略.它插入一个项,该项自上次访问后 10 分钟过期.注意
datetime.maxvalue 的使用,它表示此项没有绝对过期策略.
datetime absoluteexpiration=datetime.maxvalue;
timespan slidingexpiration=timespan.fromminutes(10);cache.insert("txt", "a",null,absoluteexpiration,slidingexpiration,system.web.caching.cacheitempriority.high,null);3.使用 add 方法将项添加到缓存中
add 方法与 insert 方法具有相同的签名,但它返回表示您所添加项的对象datetime absoluteexpiration=datetime.maxvalue;
timespan slidingexpiration=timespan.fromminutes(10);object ojb=(string)cache.add("txt","a",null,absoluteexpiration ,slidingexpiration ,system.web.caching.cacheitempriority.high,null);string str=(string)ojb ;response.write(str);结果显示是"a"
add 方法使用上没有insert 方法灵活,使用add 方法时必须提供7个参数,insert 方法重载4次,我们可
以根据需要选择适当重载方法
从缓存中取得数据方式1:
string str=(string)cache.get("txt");response.write(str);方式2:
string str1=(string)cache["txt1"];response.write(str1);查看cache中所有数据
... 下一页