This is my Blog.

It's full of opinions on a wide range of stuff.

Memoization of Lift localeCalculator

Within Lift each request has its locale calculated from scratch. Whilst this is fine with the default setting of reading from the accept headers on the incoming request, when you want to perhaps conduct something more complex you can inadvertently add quite an accidental overhead to the processing of each request. You can however ensure that the localeCalculator is only executed once by memoizing the value of the locale for the whole request cycle.

The following code memoizes the locale computation into a variable that will exist for the full lifetime of a page request ensuring that you do not take the overhead of processing the locale a bunch of times.

[gist id=803049]

This is obviously a basic implementation that carries a smalloverhead, but the principal here is the same irrespective of what it is you are actually doing :-)

Find great tips like this and more within "Lift in Action":

blog comments powered by Disqus