gpt4 book ai didi

c# - 配置缓存类与使用 HTTP header 缓存

转载 作者:太空宇宙 更新时间:2023-11-03 22:00:35 25 4
gpt4 key购买 nike

我有 asp.net mvc web 应用程序,使用 .net 3.5

我想在 UI 逻辑层使用缓存。

我读过

1- 缓存类

http://msdn.microsoft.com/en-us/library/system.web.caching.cache(v=vs.90).aspx

2- 使用 HTTP header 进行缓存

http://www.dotnetperls.com/cache

我不确定有什么区别以及我应该使用哪一个。

此外,如何在它们中的每一个中配置缓存?

项目 1- 仅在网络配置中?

第 2 项 - 仅以编程方式?

更新:

我试过了

使用 System.Web.Caching;

    private string GetTitlePerBDataId(Guid changeRequestDataId)
{
var key = string.Format("{0}_{1}", TITLE, changeRequestDataId);

if (System.Web.Caching.Cache[key] == null)
{
Cache[key] = mBundlatorServiceHelper.GetData(changeRequestBundleDataId).Title;
}

return Convert.ToString(Cache[key]);
}

但是在这一点上得到的类名是无效的 Cache

最佳答案

Cache 类在内存缓存中,在服务器上。您可以在那里缓存对象和其他内容。

使用 http header 进行缓存,定义客户端/代理如何缓存输出。

如果您查看 System.Web.Caching.Cache 的文档,它说

Information about an instance of this class is available through the Cache property of the HttpContext object or the Cache property of the Page object.

所以你只能通过 httpcontext 使用它。

private string GetTitlePerBDataId(Guid changeRequestDataId)
{
var key = string.Format("{0}_{1}", TITLE, changeRequestDataId);

if (System.Web.HttpContext.Current.Cache[key] == null)
{
System.Web.HttpContext.Current.Cache.Insert(key, mBundlatorServiceHelper.GetData(changeRequestBundleDataId).Title);
}


return Convert.ToString(System.Web.HttpContext.Current.Cache[key]);
}

关于c# - 配置缓存类与使用 HTTP header 缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10297248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com