I'm writing a custom OutputCacheProvider in MVC3.
The calls get fired in this order: Get, Add, Set. I expect that in all methods, the keys generated will be the same, but they're not. The problem is that in the different calls Get and Add are called with a different key than in the Set method.
My request looks like this: http://localhost/testCall?startIndex=0&maxResults=25&testParam=4
With the VaryByParam set, I would expect the key to be unique based on my query params to look something like: testCall?startIndex=0&maxResults=25&testParam=4
Instead, on the Get/Add calls, the key just has the full path name: localhost/testCall
But, on the Set call, the key actually looks to do what I expect: local/testCallHQNmaxresultsV25NstartindexV0NtestparamV4FCDE
Here's my controller method.
[OutputCache(Duration = 15, VaryByParam = "*")]
public ActionResult TestOutputCache()
{
var obj = new List<string>() {"test", "one", "two", "three"};
return Json(obj);
}
Here's my 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)
{
}
}
If the Set call is made with the correct params, but the Get() is never called with the correct key, then the caching will only work on the root call of /testCall
The key parameter on your implementation of the Set method is encoded, you should decode it. you can use HttpUtility.UrlDecode method.
The Key parameter will always come in the Set Method with additional parameters - this is normal.