In order to cache actions per label and language for a given number of seconds, I wrote the following helper method (where label, is the name I give to my action):
def cacheResponseFor(label: String, duration: Int)(action: EssentialAction) = {
Cached({r: RequestHeader => (label + getLanguage(r))}, duration){ action }
}
def getLanguage(request: RequestHeader): String = {
request.cookies
.get(helpers.SessionHelpers.LANGUAGE_SESSION)
.map(_.value)
.getOrElse(helpers.LanguageHelpers.FRENCH)
}
But I'm experiencing something weird, when I try to cache an Action
for 60s and switch languages in the meantime to English from French for example, I keep getting the French version for 60s then it switches to english.
After investigating, I found that method getLanguage
is not called at each call to that action as if the Key gets evaluated only after the caching period ends.
This is not right, I would want this cacheResponseFor
to be called everytime I request my page, the language gets evaluated using getLanguage
and I get the right cached version, i.e. I should end up with 2 cached actions (one per language).
Am I missing something?
Maybe the problem is in the getLanguage
method. Try this, as recommended by the docs:
def getLanguage(request: RequestHeader): String = {
request.acceptLanguages
.map(_.code)
.headOption
.getOrElse(helpers.LanguageHelpers.FRENCH)
}
Also, take a look at Controller.request2lang()
method and see if it could be helpful to you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With