Reputation: 2510
Is there any way to have multiple cache pages when i am using custom caching (VarByCustom)?
For an instance if i implement a caching custom variable browser vise , i would implement function in global as below
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom == Request.Browser.Version)
{
return Request.Browser.Version;
}
else
{
return base.GetVaryByCustomString(context, custom);
}
}
Inside controller
[PartialCaching(500000)]
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
this.CachePolicy.Duration = new TimeSpan(0,5,0);
this.CachePolicy.Cached = true;
this.CachePolicy.SetVaryByCustom(Request.Browser.Version);
lblDate.Text = DateTime.Now.ToShortDateString();
lblTime.Text = DateTime.Now.ToLongTimeString();
}
}
In this case I want to have multiple cache page on following scenario ;
Open same page on Chrome browser ~ cached page is sent as response
Open same page on Firefox again ~ Since page is cached for Chrome, this will identify as a change and it will cache again for Firefox, but in this case i want have cached page for Firefox in step one instead of caching again.
Upvotes: 2
Views: 255
Reputation: 2510
This is the solution i found
In controller i merge controller name with browser by seperating by a ;
[PartialCaching(1000)]
public partial class WebUserControl : System.Web.UI.UserControl
{
protected override void OnInit(EventArgs e)
{
this.CachePolicy.Duration = new TimeSpan(0,5,0);
this.CachePolicy.Cached = true;
this.CachePolicy.SetVaryByCustom("browser"+";"+this.ID);
}
protected void Page_Load(object sender, EventArgs e)
{
lblDate.Text = DateTime.Now.ToShortDateString();
lblTime.Text = DateTime.Now.ToLongTimeString();
}
}
In global file, user control name and browser string is split
public override string GetVaryByCustomString(HttpContext context, string custom)
{
string[] array = custom.Split(';');
string controllerName = array[1];
if (array[0] == "browser")
{
return HttpContext.Current.Request.UserLanguages[0]+controllerName;
}
else
{
return base.GetVaryByCustomString(context, custom);
}
}
Upvotes: 0
Reputation: 10565
You are incorrect here at Point 5.
As per the way VarByCustom="Browser"
works, There is a seperate cached versions of the same page for different browsers. What this means is that for the first time when a page is cached for Chrome, it doesn't destroys the cached version of Firefox OR any other browsers ( if they exists ).
So, when users make requests from Chrome, a separate cached copy is created for Chrome browser ( only if it doesn't exists already ) and the Cached version for Firefox will still be there .
At any next moment, requests come from Firefox, the cached version of Firefox browser is sent as response.
NOTE:: using VarByCustom="Browser"
by default, also considers the Major Version of the browser you are using.
Upvotes: 1