Resource Files and Output Caching

  • Thread starter aptenodytesforsteri
  • Start date
A

aptenodytesforsteri

I have an ASP.NET 2.0 application I've localized to English, French,
German, and Italian.

I used resource (.resx) files.

Most of the site is static content, easily 90% of it, so I thought
output caching would be useful.

I implemented output caching at 60 seconds.

The problem: If page A is in English, it's get's cached as English.
The user can click on German, Italian, or French, but the page stays
in English. If click French, French, French for 60 seconds, it'll give
me French when the cache expires.

Question: How do I expire the cache when the user changes languages?

I've tried Response.Cache.SetExpires(DateTime.Now) when thr user
selects a new language, but that failed.

Ideas? Suggestions. Thanks in advance.
 
G

Guest

Hi there,

I don't think expiring cached page depending on the language is good idea.
If there are many requesting in different language the page would be
invalidated from cache very often and cache hit ration would be very low (so
what’s the point of using cache in this case?). You could cache page in all
languages separately overriding GetVaryByCustomString (Global.asax) method.
Please note, the session information is not available at this stage of page
execution, so in order to determine user’s current language (what the logic
at the moment?) you should use cookie or Query string (page.aspx?lang=en) or
URL with domain name (en.mywebsite.com – more things to consider) or any
other mechanism that don not rely on session. I prepared a simple example
based on assumption the user language is stored in a cookie:


-- begin Global.asax c# code --

public override string GetVaryByCustomString(HttpContext context, string
argument)
{

switch (argument)
{
case "UserLanguage" :

HttpCookie cookie = context.Request.Cookies["UserLanguageCookie"];

string language = cookie == null ?
DefaultLanguage : ValidateLanguageCode(cookie.Value);

return language;

default:
return String.Empty;
}
}

private const string DefaultLanguage = "en-GB";

private string ValidateLanguageCode(string code)
{
// if there is something wrong with the language return default code
// bla bla vbla
return DefaultLanguage;
}

-- end Global.asax c# code --

-- begin cached page aspx code --

<%@ Page Language="C#" EnableViewState="false" AutoEventWireup="true"
Codebeside="MyPage.aspx.cs" %>
<%@ OutputCache VaryByParam="None" Duration="60" VaryByCustom="UserLanguage"
%>

....page content...

-- end cached page aspx code--
 
G

Guest

I forgot you're caching localized pages, therefore you must use
System.Threading.Thread.CurrentThread.CurrentUICulture.Name


public override string GetVaryByCustomString(HttpContext context, string
custom)
{
if (string.Compare(custom, "uiCulture", true,
System.Globalization.CultureInfo.InvariantCulture) == 0 &&
System.Threading.Thread.CurrentThread.CurrentUICulture != null)
{
return System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
}
else
return base.GetVaryByCustomString(context, custom);
}

--
Milosz


Milosz Skalecki said:
Hi there,

I don't think expiring cached page depending on the language is good idea.
If there are many requesting in different language the page would be
invalidated from cache very often and cache hit ration would be very low (so
what’s the point of using cache in this case?). You could cache page in all
languages separately overriding GetVaryByCustomString (Global.asax) method.
Please note, the session information is not available at this stage of page
execution, so in order to determine user’s current language (what the logic
at the moment?) you should use cookie or Query string (page.aspx?lang=en) or
URL with domain name (en.mywebsite.com – more things to consider) or any
other mechanism that don not rely on session. I prepared a simple example
based on assumption the user language is stored in a cookie:


-- begin Global.asax c# code --

public override string GetVaryByCustomString(HttpContext context, string
argument)
{

switch (argument)
{
case "UserLanguage" :

HttpCookie cookie = context.Request.Cookies["UserLanguageCookie"];

string language = cookie == null ?
DefaultLanguage : ValidateLanguageCode(cookie.Value);

return language;

default:
return String.Empty;
}
}

private const string DefaultLanguage = "en-GB";

private string ValidateLanguageCode(string code)
{
// if there is something wrong with the language return default code
// bla bla vbla
return DefaultLanguage;
}

-- end Global.asax c# code --

-- begin cached page aspx code --

<%@ Page Language="C#" EnableViewState="false" AutoEventWireup="true"
Codebeside="MyPage.aspx.cs" %>
<%@ OutputCache VaryByParam="None" Duration="60" VaryByCustom="UserLanguage"
%>

...page content...

-- end cached page aspx code--


--
Milosz


I have an ASP.NET 2.0 application I've localized to English, French,
German, and Italian.

I used resource (.resx) files.

Most of the site is static content, easily 90% of it, so I thought
output caching would be useful.

I implemented output caching at 60 seconds.

The problem: If page A is in English, it's get's cached as English.
The user can click on German, Italian, or French, but the page stays
in English. If click French, French, French for 60 seconds, it'll give
me French when the cache expires.

Question: How do I expire the cache when the user changes languages?

I've tried Response.Cache.SetExpires(DateTime.Now) when thr user
selects a new language, but that failed.

Ideas? Suggestions. Thanks in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top