温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ASP.NET中缓存数据添加方法是什么

发布时间:2021-08-30 23:42:06 来源:亿速云 阅读:102 作者:chen 栏目:编程语言

本篇内容主要讲解“ASP.NET中缓存数据添加方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“ASP.NET中缓存数据添加方法是什么”吧!

  缓存这些类型的资源会大大改进应用程序的性能。缓存是有Cache类实现的,可以通过对缓存设置优先级CacheItemPriority枚举值控制内存不够时的“清理”优先顺序。还可以为缓存设置过期策略,以及为缓存设置依赖项。

  ASP.NET缓存数据添加(将数据项添加到缓存中)

  1、通过键值对添加

  Cache["CacheItem"]="CachedItem";

  2、通过Insert方法添加

  Insert方法向缓存添加项,并且已经存在与现有项同名的项,则缓存中的现有项将被替换。

  Cache.Insert("CacheItem","CachedItem");

  3、指定依赖项并添加(对添加到缓冲中的数据项指定依赖项)

  数据项依赖一个字符串数组对象的情况:

  ?string[]dependencies={"Dependences"};

  ?Cache.Insert("CacheItem",

  ?"CachedItem",

  ?newSystem.Web.Caching.CacheDependency(null,dependencies));

  数据项依赖一个XML文件的情况:

  ?Cache.Insert("CacheItem","CachedItem",

  ?newSystem.Web.Caching.CacheDependency(

  ?Server.MapPath("XMLFile.xml")));

  数据项依赖多个依赖项的情况:

  ?System.Web.Caching.CacheDependencydep1=

  ?newSystem.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml"));

  ?string[]keyDependencies2={"CacheItem1"};

  ?System.Web.Caching.CacheDependencydep2=

  ?newSystem.Web.Caching.CacheDependency(null,keyDependencies2);

  ?System.Web.Caching.AggregateCacheDependencyaggDep=

  ?newSystem.Web.Caching.AggregateCacheDependency();

  ?aggDep.Add(dep1);

  ?aggDep.Add(dep2);

  ?Cache.Insert("CacheItem","CachedItem",aggDep);

  ASP.NET缓存数据添加方法是什么

  4、设置过期策略并添加

  添加一分钟绝对过期时间到缓存中:

  ?Cache.Insert("CacheItem","CachedItem",

  ?null,DateTime.Now.AddMinutes(1d),

  ?System.Web.Caching.Cache.NoSlidingExpiration);

  添加10分钟弹性过期时间到缓存中:

  ?Cache.Insert("CacheItem","CachedItem",

  ?null,System.Web.Caching.Cache.NoAbsoluteExpiration,

  ?newTimeSpan(0,10,0));

  5、设置优先级并添加

  调用Insert方法,从CacheItemPriority枚举中指定一个值。

  ?Cache.Insert("CacheItem","CachedItem",

  ?null,System.Web.Caching.Cache.NoAbsoluteExpiration,

  ?System.Web.Caching.Cache.NoSlidingExpiration,

  ?System.Web.Caching.CacheItemPriority.High,null);

  6、通过Add方法添加

  Add方法将返回您添加到缓存中的对象。另外,如果使用Add方法,并且缓存中已经存在与现有项同名的项,则该方法不会替换该项,并且不会引发异常。

  ?stringCachedItem=(string)Cache.Add("CacheItem",

  ?"CachedItem",null,

  ?System.Web.Caching.Cache.NoAbsoluteExpiration,

  ?System.Web.Caching.Cache.NoSlidingExpiration,

  ?System.Web.Caching.CacheItemPriority.Default,

  ?null);

到此,相信大家对“ASP.NET中缓存数据添加方法是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI