HttpRuntime.Cache:获取当前应用程序的Cache对象。 //System.Web.HttpRuntime.Cache属性实现 public sealed class HttpRuntime { public static Cache Cache { 既然两个指向的是同一Cache对象,两者的差别只能出现在HttpContext和HttpRuntime上了。 HttpRuntime:为当前应用程序提供一组ASP.NET运行时服务。 通过上面的定义可以看出:HttpRuntime.Cache相当于就是一个缓存具体实现类,这个类虽然被放在了System.Web命名空间下,但是非Web应用下也是可以使用。
存缓存很好用,今天写了一个windows服务程序,HttpContext.Current.Cache存缓存的时候还好,取的时候一直报错“未将对象引用到实例”很郁闷,查询了一下资料才明白引用程序缓存要用HttpRuntime.Cache HttpRuntime.Cache:获取当前应用程序的Cache。 附带的写了一个操作缓存的通用类,在应用程序中使用,如果要在asp.net中有,只需把HttpRuntime.Cache改为HttpContext.Current.Cache即可,代码如下: using try { DateTime dt = DateTime.Now.AddHours(time_HH); System.Web.HttpRuntime.Cache.Insert bool IsCacheExist(string key) { bool result = false; object temp = System.Web.HttpRuntime.Cache.Get
(类似Session机制) HttpRuntime.Cache与HttpContext.Current.Cache 为同一个对象 HttpRuntime.Cache.Add 存在相同的键会异常,返回缓存成功的对象 HttpRuntime.Cache.Insert存在相同的键会替换无返回值 HttpRuntime.Cache["key"] 使用字典的方式也可以读取和设置 HttpRuntime.Cache.Insert 读取缓存内容调用System.Web.HttpRuntime.Cache.Get(Key)方法,插入缓存数据调用Add或Insert方法。 Add与Insert的不同 HttpRuntime.Cache.Add 存在相同的键会异常,返回缓存成功的对象。 HttpRuntime.Cache.Insert存在相同的键会替换原值,无返回值。 如果您希望某个缓存项目一旦放入缓存后,就不要再被修改,那么调用Add确实可以防止后来的修改操作。
有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是应用程序级别的,而HttpContext.Current.Cache是针对当前 HttpRuntime下的除了WEB中可以使用外,非WEB程序也可以使用。 1、HttpRuntime.Cache 相当于就是一个缓存具体实现类,这个类虽然被放在了 System.Web 命名空间下了。但是非 Web 应用也是可以拿来用的。 综上所属,在可以的条件,尽量用 HttpRuntime.Cache ,而不是用 HttpContext.Cache 。 有以下几条缓存数据的规则。 下面介绍HttpRuntime.Cache常用方法: using System; using System.Web; using System.Collections; public
/// <value></value> public V this[string key] { get { return (V)HttpRuntime.Cache // /// </returns> public bool ContainsKey(string key) { return HttpRuntime.Cache <param name="key">key</param> public void Remove(string key) { HttpRuntime.Cache.Remove / public void RemoveAll() { System.Web.Caching.Cache cache = HttpRuntime.Cache RemoveAll(Func<string, bool> removeExpression) { System.Web.Caching.Cache _cache = HttpRuntime.Cache
二、第二个入口:HttpRuntime.ProcessRequest() HttpRuntime是ASP.NET请求处理的第二个入口。 当请求进来,首先进入HttpRuntime,由HttpRuntime来决定如何处理请求。 默认情况下,在machine.config和Web.config中并没有显式定义httpRuntime节点,但该节点是有默认值的,如下: <httpRuntime apartmentThreading 这里我们还可以看到_theRuntime这个字段,它是HttpRuntime类的一个静态字段,在HttpRuntime的静态构造函数中进行初始化。 ②HttpRuntime->HttpContext->HttpApplication ?
GetRecord(int maximumRows, int startRowIndex, string sortExpression) 11 { 12 DataTable dt = HttpRuntime.Cache countryDt; 53 } 54 55 public DataTable GetHobby() 56 { 57 DataTable hobbyDt = HttpRuntime.Cache private void AddCache(string key, object data) 62 { 63 System.Web.Caching.Cache dc = HttpRuntime.Cache ); 69 } 70 71 private void RemoveCache() 72 { 73 System.Web.Caching.Cache dc = HttpRuntime.Cache 2 { 3 // 在应用程序启动时运行的代码 4 OdsDataManager om = new OdsDataManager(); 5 HttpRuntime.Cache.Insert
参数名: site] System.Web.HttpRuntime.HostingInit(HostingEnvironmentFlags hostingFlags, PolicyLevel policyLevel 参数名: site] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9874568 System.Web.HttpRuntime.EnsureFirstRequestInit (HttpContext context) +101 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest
public class CacheHelper { public static object GetCache(string key) { return HttpRuntime.Cache = null; } public static void RemoveCache(string key) { HttpRuntime.Cache.Remove expireType) { if (timeout == null) HttpRuntime.Cache[key] = value; CacheHelper类:使用HttpRuntime的缓存,类里实现缓存的增删改,因为使用的是HttpRuntime,所以,如果没有设置缓存的超时时间,则缓存的超时时间等于HttpRuntime.Cache 如果网站挂载在IIS里,那么,HttpRuntime.Cache配置超时时间的地方在该网站的应用程序池中,如下图: ?
------------------------------------------------ 2.二级缓存:(System.Web;System.Web.Caching;) 2.1绝对过期缓存 HttpRuntime.Cache.Add 2.2相对过期缓存(有点Session的感觉了) HttpRuntime.Cache.Add(key, list, null, Cache.NoAbsoluteExpiration, new TimeSpan --数据库缓存依赖配置 End--> Code:HttpRuntime.Cache.Add(key, list, new SqlCacheDependency("SQLCacheDB", "SeoTKD HttpRuntime.Cache.Add(key, list, new CacheDependency(Request.MapPath("/App_Data/Config.xml")), Cache.NoAbsoluteExpiration
我们在一个控制台应用中编写了如下一段程序,这个段程序很简单:我们通过HttpRuntime的静态属性Cache得到表示当前缓存的Cache对象,并调用其Insert方法对当前的时间实施缓存。 Main(string[] args) 4: { 5: string key = Guid.NewGuid().ToString(); 6: HttpRuntime.Cache.Insert 1) ); 7: for (int i = 0; i < 5; i++) 8: { 9: Console.WriteLine(HttpRuntime.Cache.Get Main(string[] args) 4: { 5: string key = Guid.NewGuid().ToString(); 6: HttpRuntime.Cache.Insert 2) ); 7: for (int i = 0; i < 5; i++) 8: { 9: Console.WriteLine(HttpRuntime.Cache.Get
<configuration> <system.web> <httpRuntime> <httpRuntime useFullyQualifiedRedirectUrl="true|false" <configuration> <system.web> <httpRuntime maxRequestLength="4000" useFullyQualifiedRedirectUrl
2、HttpRuntime首先会确定处理该请求的类名,HttpRuntime通过公共接口IHttpHandler来调用该类获取被请求资源的类的实例。 3、调用HttpRuntime.ProcessRequest开始处理要发送到浏览器的页面,具体说就是创建一个HttpContext实例,它封装了所有与请求有关的http特有的信息,并初始化一个Write 4、HttpRuntime使用上下文信息查找或新建能处理该请求的WEB应用程序的对象。由HttpApplication Factory负责返回HttpApplication实例。 6、HttpApplication对象使用IHttpHandlerFactory类型的实例返回HttpHandler(http处理程序)给HttpRuntime对象。 7、最后由HttpRuntime对象调用IHttpHandler的页面对象的ProcessRequest方法。
System.Web.HttpRuntime.Cache["throttle"]; if (! requestCount.HasValue) requestCount = 0; requestCount++; HttpRuntime.Cache["throttle"] = requestCount helloworld")] public HttpResponseMessage HelloWorld() { ThrottleInfo throttleInfo = (ThrottleInfo)HttpRuntime.Cache ExpiresAt = DateTime.Now.AddSeconds(10), RequestCount = 0 }; throttleInfo.RequestCount++; HttpRuntime.Cache.Add public bool RequestShouldBeThrottled() { ThrottleInfo throttleInfo = (ThrottleInfo)HttpRuntime.Cache
信息服务管理器—>找到“HTTP响应标头”—>双击鼠标左键进入配置—>选中X-Powered-By单击右键后点击删除 三、去除X-AspNet-Version版本信息 在网站目录的web.config的httpRuntime 节点中添加enableVersionHeader=“false” 如web.config没有httpRuntime节点则在<system.web>创建节点 四、隐藏MVC版本信息(节点:X-AspNetMvc-Version
之后再执行HttpRuntime的静态方法:ProcessRequestNoDemand(参数为封装了浏览器请求的信息: HttpWorkerRequest) 补充:默默无闻的工作者对象HttpWorkerRequest (wr); } // _theRuntime就是HttpRuntime类型的对象,他在HttpRuntime的静态构造函数初始化。 static HttpRuntime() { ...... _theRuntime = new HttpRuntime(); _theRuntime.Init(); AddAppDomainTraceMessage("HttpRuntime::cctor _context.TraceIsEnabled) { HttpRuntime.Profile.StartRequest(this.
移除X-AspNet-Version 在web.config的<httpRuntime>中添加enableVersionHeader="false": <httpRuntime enableVersionHeader
示例代码: 1 System.Web.HttpRuntime.Cache.Insert("context", System.Web.HttpContext.Current); //异步调用,HttpContext httpContext = HttpContext.Current; 9 if (httpContext == null) 10 httpContext = HttpRuntime.Cache.Get
先去缓存中查找 DataTable tt = (DataTable)HttpRuntime.Cache["persons"]; if (tt == null SqlHelper.ExecuteQuery("select * from T_users"); //将查询的结果存入缓存中,设置30秒后过期 HttpRuntime.Cache.Insert
代码如下: <httpRuntime maxRequestLength="204800" executionTimeout="600"/> 上述代码maxRequestLength的单位是KB,204800 你可能还不明白httpRuntime的设置代码该插入到web.config哪个地方,如果放错了,可能会导致web.config配置文件失效,从而影响网站的正常运行。 请参考文章《httpRuntime代码放在web.config哪里?深度了解httpRuntime》。 这时好像httpRuntime的maxRequestLength设置已经无效了。这又是什么原因呢? 原来,IIS本身有请求长度限制!这时我们可以修改IIS配置来解决这个问题。 请注意,修改了IIS的“请求筛选”后,web.config里同样要设置httpRuntime的maxRequestLength的值大于30m。