【发布时间】:2015-10-20 20:18:29
【问题描述】:
我有一个带有 OutputCache 的 MVC 操作,因为我需要缓存数据以尽量减少对 myService 的调用。
[HttpGet]
[OutputCache(Duration = 86400, Location = OutputCacheLocation.ServerAndClient, VaryByParam = "myVariable")]
public JsonResult GetStuffData(string myVariable)
{
if (Request.IsLocal)
{
return myService.CalculateStuff(myVariable)
}
else
{
return null;
}
}
我希望它只能从运行它的服务器访问,因此是 Request.IsLocal。
这很好用,但是如果有人远程访问 GetStuffData,那么它将返回 null,并且 null 将被缓存一天...使特定的 GetStuffData(myVariable) 一天无用。
同理,如果先在本地调用,那么外部请求会收到本地缓存的数据。
有没有办法将整个函数限制为 Request.IsLocal 而不仅仅是返回值?
例如,如果它被外部访问,您只会得到 404,或未找到方法等。但如果是 Request.Local,您将获得缓存的结果。
如果没有缓存,这将运行得非常好,但我正在努力寻找一种方法来结合 Request.IsLocal 和缓存。
可能相关的额外信息:
我通过 C# 调用 GetStuffData 以通过获取这样的 json 对象来获取缓存的 StuffData...(直接调用该操作从未导致它被缓存,因此我切换到模拟 webrequest)
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlToGetStuffData);
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream()) {
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
return reader.ReadToEnd();
}
【问题讨论】:
标签: c# asp.net-mvc outputcache