Skip to content

Commit

Permalink
Memoize the value of a resource lookup for the duration of the request.
Browse files Browse the repository at this point in the history
Closes lift#1372
  • Loading branch information
jeppenejsum committed Dec 11, 2012
1 parent 8bb1982 commit 2891714
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions web/webkit/src/main/scala/net/liftweb/http/S.scala
Expand Up @@ -933,18 +933,10 @@ trait S extends HasParams with Loggable {
* If you do not define an entry for a particular key, we fall back to using * If you do not define an entry for a particular key, we fall back to using
* Lift's core entries. * Lift's core entries.
* *
* We cache the values in modes other than DevMode
*
* @see LiftRules.resourceNames * @see LiftRules.resourceNames
* @see LiftRules.resourceBundleFactories * @see LiftRules.resourceBundleFactories
*/ */
def _resourceBundles: List[ResourceBundle] = resourceBundles(locale) ++ liftCoreResourceBundle.toList def resourceBundles: List[ResourceBundle] = resourceBundles(locale) ++ liftCoreResourceBundle.toList

private lazy val cachedResourceBundles = _resourceBundles

private def resourceBundles: List[ResourceBundle] =
if (Props.devMode) _resourceBundles else cachedResourceBundles



def resourceBundles(loc: Locale): List[ResourceBundle] = { def resourceBundles(loc: Locale): List[ResourceBundle] = {
_resBundle.box match { _resBundle.box match {
Expand Down Expand Up @@ -985,6 +977,9 @@ trait S extends HasParams with Loggable {
*/ */
def liftCoreResourceBundle: Box[ResourceBundle] = _liftCoreResBundle.is def liftCoreResourceBundle: Box[ResourceBundle] = _liftCoreResBundle.is



private object resourceValueCache extends TransientRequestMemoize[(String, Locale), String]

/** /**
* Get a localized string or return the original string. * Get a localized string or return the original string.
* We first try your own bundle resources, if that fails, we try * We first try your own bundle resources, if that fails, we try
Expand All @@ -996,7 +991,7 @@ trait S extends HasParams with Loggable {
* *
* @see # resourceBundles * @see # resourceBundles
*/ */
def ?(str: String): String = ?!(str, resourceBundles) def ?(str: String): String = resourceValueCache.get(str -> locale, ?!(str, resourceBundles))


/** /**
* Get a localized string or return the original string. * Get a localized string or return the original string.
Expand All @@ -1011,7 +1006,8 @@ trait S extends HasParams with Loggable {
* *
* @see # resourceBundles * @see # resourceBundles
*/ */
def ?(str: String, locale: Locale): String = ?!(str, resourceBundles(locale)) def ?(str: String, locale: Locale): String = resourceValueCache.get(str -> locale, ?!(str, resourceBundles(locale)))

/** /**
* Attempt to localize and then format the given string. This uses the String.format method * Attempt to localize and then format the given string. This uses the String.format method
* to format the localized string. * to format the localized string.
Expand Down

1 comment on commit 2891714

@fmpwizard
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Please sign in to comment.