HttpContext.Cache和HttpRuntime.Cache

本文主要探讨Asp.Net中Cache的两种调用方式——HttpContext.Cache和HttpRuntime.Cache的区别。Msdn注释显示二者获取Cache的对象不同,但实际上它们调用的是同一对象,区别仅在调用方式。通过示例证实,二者保存的Cache均可相互读取,且一个用户对Cache的改变会影响其他用户读取的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Asp.Net中可以方便的使用缓存,对于Cache,一般有两种方式调用:HttpContext.Cache和HttpRuntime.Cache。那么这两种Cache有什么区别呢?

先来看看Msdn上的注释:
HttpRuntime.Cache:获取当前应用程序的 Cache。
HttpContext.Cache:为当前 HTTP 请求获取 Cache 对象。

那么是不是说对于HttpRuntime.Cache就是应用程序级,而HttpContext.Cache则是针对每个用户的呢?NO,而实际上,两者调用的是同一个对象。他们的区别仅仅在于调用方式不一样(就我所知)。

事实胜过雄辩,写个例子来证实一下(限于篇幅仅贴出关键代码,完整代码见附件WebDemo.rar):

 
/// <summary> /// 通过HttpRuntime.Cache的方式来保存Cache /// </summary> private void btnHttpRuntimeCacheSave_Click( object sender, System.EventArgs e) { HttpRuntime.Cache.Insert(cacheKey, cacheValue, null, DateTime.Now.AddMinutes(3), TimeSpan.Zero); } /// <summary> /// 通过HttpRuntime.Cache的方式来读取Cache /// </summary> private void btnHttpRuntimeCacheLoad_Click( object sender, System.EventArgs e) { if (HttpRuntime.Cache[cacheKey] == null) { cacheContent = "No Cache"; } else { cacheContent = (string)HttpRuntime.Cache[cacheKey]; } lblCacheContent.Text = cacheContent; } /// <summary> /// 通过HttpContext.Cache的方式来保存Cache /// </summary> private void btnHttpContextCacheSave_Click( object sender, System.EventArgs e) { HttpContext.Current.Cache.Insert(cacheKey, cacheValue, null, DateTime.Now.AddMinutes(3), TimeSpan.Zero); } /// <summary> /// 通过HttpContext.Cache的方式来读取Cache /// </summary> private void btnHttpContextCacheLoad_Click( object sender, System.EventArgs e) { if (HttpContext.Current.Cache[cacheKey] == null) { cacheContent = "No Cache"; } else { cacheContent = (string)HttpContext.Current.Cache[cacheKey]; } lblCacheContent.Text = cacheContent; }
  1. 通过这个例子可以很容易证明:
  2. HttpContext.Cache保存的Cache,HttpContext.Cache和HttpRuntime.Cache都可以读取。
  3. HttpRuntime.Cache保存的Cache,HttpContext.Cache和HttpRuntime.Cache都可以读取。
  4. 无论是哪个用户通过什么方式对Cache的改变,其他用户无论用什么方式读取的Cache内容也会随之变。
### 如何通过 `HttpContext.Request.Query` 获取查询字符串参数 在 C# 中,可以利用 `HttpContext.Request.Query` 来获取 URL 的查询字符串参数。这是一个键值对集合 (`IQueryCollection`),允许开发者轻松访问 GET 请求中的参数。 以下是具体实现方式: #### 示例代码 ```csharp using Microsoft.AspNetCore.Http; public string GetQueryStringParameter(HttpRequest request, string key) { // 判断是否存在指定的查询字符串参数 if (request.Query.ContainsKey(key)) { // 返回对应的值 return request.Query[key]; } return null; } // 调用方法并传入HttpRequest对象目标Key var value = GetQueryStringParameter(HttpContext.Request, "exampleKey"); Console.WriteLine($"The query parameter 'exampleKey' has the value: {value}"); ``` 上述代码展示了如何定义一个函数来提取特定名称的查询字符串参数,并返回其值[^4]。 如果需要处理多个参数或者更复杂的场景,则可以直接遍历整个 `Query` 集合: #### 处理多参数示例 ```csharp foreach (var kvp in HttpContext.Request.Query) { Console.WriteLine($"{kvp.Key}: {kvp.Value}"); } ``` 此循环将打印出所有的查询字符串参数及其对应值[^1]。 需要注意的是,在 ASP.NET Core 环境下推荐使用这种方式替代传统的 `Request.QueryString` 方法,因为它是强类型的并且更加安全可靠[^2]。 对于某些特殊需求比如 OAuth 授权流程中提到的情况(如引用所描述),也可以结合该技术完成相应功能开发,例如读取 auth_code 或其他回调参数[^3]。 最后提醒一点,当涉及到外部 API 访问时务必妥善保管密钥信息,避免泄露风险[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值