ASP.NET缓存数据添加方法一览
来源:互联网 发布:等腰杨辉三角c语言程序 编辑:程序博客网 时间:2024/05/29 12:59
ASP.NET缓存数据添加需求概述
ASP.NET使用缓存机制,将需要大量服务器资源来创建的对象存储在内存中。缓存这些类型的资源会大大改进应用程序的性能。缓存是有Cache类实现的,可以通过对缓存设置优先级CacheItemPriority枚举值控制内存不够时的“清理”优先顺序。还可以为缓存设置过期策略,以及为缓存设置依赖项。
ASP.NET缓存数据添加(将数据项添加到缓存中)
1、通过键值对添加
- Cache["CacheItem"] = "Cached Item";
2、通过Insert 方法添加
Insert 方法向缓存添加项,并且已经存在与现有项同名的项,则缓存中的现有项将被替换。
- Cache.Insert("CacheItem", "Cached Item");
3、指定依赖项并添加(对添加到缓冲中的数据项指定依赖项)
数据项依赖一个字符串数组对象的情况:
- string[] dependencies = { "Dependences" };
- Cache.Insert("CacheItem",
- "Cached Item",
- new System.Web.Caching.CacheDependency(null, dependencies));
数据项依赖一个XML文件的情况:
- Cache.Insert("CacheItem", "Cached Item",
- new System.Web.Caching.CacheDependency(
- Server.MapPath("XMLFile.xml")));
数据项依赖多个依赖项的情况:
- System.Web.Caching.CacheDependency dep1 =
- new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml"));
- string[] keyDependencies2 = { "CacheItem1" };
- System.Web.Caching.CacheDependency dep2 =
- new System.Web.Caching.CacheDependency(null, keyDependencies2);
- System.Web.Caching.AggregateCacheDependency aggDep =
- new System.Web.Caching.AggregateCacheDependency();
- aggDep.Add(dep1);
- aggDep.Add(dep2);
- Cache.Insert("CacheItem", "Cached Item", aggDep);
4、设置过期策略并添加
添加一分钟绝对过期时间到缓存中:
- Cache.Insert("CacheItem", "Cached Item",
- null, DateTime.Now.AddMinutes(1d),
- System.Web.Caching.Cache.NoSlidingExpiration);
添加10 分钟弹性过期时间到缓存中:
- Cache.Insert("CacheItem", "Cached Item",
- null, System.Web.Caching.Cache.NoAbsoluteExpiration,
- new TimeSpan(0, 10, 0));
5、设置优先级并添加
调用 Insert 方法,从 CacheItemPriority 枚举中指定一个值。
- Cache.Insert("CacheItem", "Cached Item",
- null, System.Web.Caching.Cache.NoAbsoluteExpiration,
- System.Web.Caching.Cache.NoSlidingExpiration,
- System.Web.Caching.CacheItemPriority.High, null);
6、通过Add方法添加
Add 方法将返回您添加到缓存中的对象。另外,如果使用 Add 方法,并且缓存中已经存在与现有项同名的项,则该方法不会替换该项,并且不会引发异常。
- string CachedItem = (string)Cache.Add("CacheItem",
- "Cached Item", null,
- System.Web.Caching.Cache.NoAbsoluteExpiration,
- System.Web.Caching.Cache.NoSlidingExpiration,
- System.Web.Caching.CacheItemPriority.Default,
- null);
- ASP.NET缓存数据添加方法一览
- ASP.NET缓存数据添加方法一览
- ASP.NET数据缓存
- asp.net数据缓存
- asp.net数据缓存浅谈
- ASP.NET缓存学习入门---数据缓存
- ASP.NET缓存学习入门---数据缓存
- asp.net输出缓存和数据缓存
- ASP.NET缓存学习入门---数据缓存
- ASP.NET缓存 Cache之数据缓存
- Asp.Net:数据添加、修改的便捷方法
- 【ASP.NET】ASP.NET缓存学习入门---数据缓存
- asp.net中为用户控件添加数据缓存依赖项时要注意的事项
- ASP.NET 2.0数据缓存功能简介
- ASP.NET应用中缓存Oracle数据
- ASP.NET 2.0数据缓存功能简介
- ASP.NET应用中缓存Oracle数据
- ASP.NET 2.0数据缓存功能简介
- jquery高亮关键字
- hdu 杭电acm 1002
- 实用SQL语句
- perl 判断数组里面是否含有某个元素
- Java compiler level does not match the version of the installed Java project facet
- ASP.NET缓存数据添加方法一览
- ASP.NET缓存机制基础概念
- pku acm 1068
- 从iOS3到iOS5各版本feature介绍
- 解开Android应用程序组件Activity的"singleTask"之谜
- u-boot-2009.11在MINI2440上的移植 LCD驱动
- C++ Vector 使用心得
- 动态链接库、静态链接库;动态链接、静态链接
- SQLServer中Case的用法