Soo*_*ead 4 caching asp.net-mvc-3
我需要在我的MVC3项目中使用应用程序级缓存.
我想在控制器中使用这样的东西:
using System.Web.Caching;
protected IMyStuff GetStuff(string stuffkey)
{
var ret = Cache[stuffkey];
if (ret == null)
{
ret = LoadStuffFromDB(stuffkey);
Cache[stuffkey] = ret;
}
return (IMyStuff)ret;
}
Run Code Online (Sandbox Code Playgroud)
这会失败,因为Cache ["foo"]不能编译为"System.Web.Caching.Cache是'type',而是像'variable'一样使用".
我看到Cache是一个类,但是在控制器中像Session ["asdf"]一样使用它时,网上有很多例子,就像它是一个属性一样.
我究竟做错了什么?
Ego*_*4eg 11
有一个Session在控制器中命名的属性,但没有名为的属性Cache.您应该使用HttpRuntime.Cache静态属性来获取Cache对象.例如:
using System.Web.Caching;
protected IMyStuff GetStuff(string stuffkey)
{
var ret = HttpRuntime.Cache[stuffkey];
if (ret == null)
{
ret = LoadStuffFromDB(stuffkey);
HttpRuntime.Cache[stuffkey] = ret;
}
return (IMyStuff)ret;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3777 次 |
| 最近记录: |