我正在MVC3中编写自定义OutputCacheProvider。
调用按以下顺序触发:Get,Add,Set。我希望在所有方法中,生成的密钥都是相同的,但它们不是。问题是在不同的调用中使用与Set方法不同的键调用Get和Add。
我的请求如下:http://localhost/testCall?startIndex=0&maxResults=25&testParam=4
使用VaryByParam设置,我希望根据我的查询参数,键是唯一的,看起来像:testCall?startIndex=0&maxResults=25&testParam=4
相反,在Get / Add调用中,键只有完整的路径名:localhost/testCall
但是,在Set调用中,键实际上看起来像我期望的那样:local/testCallHQNmaxresultsV25NstartindexV0NtestparamV4FCDE
这是我的控制器方法。
[OutputCache(Duration = 15, VaryByParam = "*")]
public ActionResult TestOutputCache()
{
var obj = new List<string>() {"test", "one", "two", "three"};
return Json(obj);
}
这是我的Custom OutputCacheProvider
public class MemcacheOutputCacheProvider : OutputCacheProvider
{
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
base.Initialize(name, config);
}
public override object Get(string key)
{
// UNEXPECTED, the key = "localhost/testCall"
return null;
}
public override object Add(string key, object entry, DateTime utcExpiry)
{
// UNEXPECTED, the key = "localhost/testCall"
return null;
}
public override void Set(string key, object entry, DateTime utcExpiry)
{
// EXPECTED, the key = "local/testCallHQNmaxresultsV25NstartindexV0NtestparamV4FCDE"
}
public override void Remove(string key)
{
}
}
如果使用正确的参数进行Set调用,但从未使用正确的密钥调用Get(),则缓存仅适用于/ testCall的根调用
您对Set方法实现的关键参数是编码的,您应该解码它。你可以使用HttpUtility.UrlDecode方法。
public override object Add(string key, object entry, DateTime utcExpiry)
{
var decodedKey = HttpUtility.UrlDecode(key)
// implement your caching logic here
return entry;
}
Key参数将始终带有附加参数的Set Method - 这是正常的。