473,326 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Resource Files and Output Caching

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.

Feb 15 '07 #1
2 2155
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
"ap*****************@gmail.com" wrote:
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.

Feb 15 '07 #2
I forgot you're caching localized pages, therefore you must use
System.Threading.Thread.CurrentThread.CurrentUICul ture.Name
public override string GetVaryByCustomString(HttpContext context, string
custom)
{
if (string.Compare(custom, "uiCulture", true,
System.Globalization.CultureInfo.InvariantCulture) == 0 &&
System.Threading.Thread.CurrentThread.CurrentUICul ture != null)
{
return System.Threading.Thread.CurrentThread.CurrentUICul ture.Name;
}
else
return base.GetVaryByCustomString(context, custom);
}

--
Milosz
"Milosz Skalecki [MCAD]" wrote:
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
"ap*****************@gmail.com" wrote:
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.
Feb 15 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: martin | last post by:
Hi, I have noticed that every aspx page that I created (and ascx file) has an assosiated resource file aspx.resx. However what I would like to do is have a single global resource file for the...
0
by: Kleanthis | last post by:
I have a problem, when deploying multilingual applications using cab files on Compact Framework 2.0. It seems that something is going wrong with compact framework 2.0 Below I have a description...
1
by: urban.john | last post by:
Here are my steps: create resource files from resx files: <echo message="CREATING RESOURCE FILES FROM RESGEN EN" /> <resgen todir="product\resources_en" verbose="true"> <resources> <include...
12
by: TS | last post by:
i have a need to possibly enable mutli language support. What benefit do i get by using a resource file instead of a custom xml solution? thanks!
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.