繁体   English   中英

在ASP.net中缓存

[英]Caching in ASP.net

我有一个webService。 因此,我正在使用缓存。

我编写了以下代码以将数据表存储在缓存中。

using System.Web.Caching;

Cache.Insert("dt", dt, null, DateTime.Now.AddHours(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);

它给我这样的错误,例如“非静态字段需要对象引用。

我如何清除此错误

您正在尝试将Cache类用作静态类。

如果要在请求期间将当前Cache类实例用于HTTP上下文,则应执行以下操作:

HttpContext.Current.Cache.Insert("dt", dt, null, DateTime.Now.AddHours(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);

使用System.Web.HttpRuntime.Cache .Insert(...)。 此“缓存”是一个属性,返回System.Web.Caching.Cache的实例 它与HttpContext.Current.Cache相同,但是不需要HttpContext。

您的代码试图在不使用实例的情况下访问该类的方法-因此出现错误消息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM