How to share request scope data among snippets in Lift

28 views
Skip to first unread message

ishiijp

unread,
Sep 26, 2009, 3:14:28 AM9/26/09
to Lift
Hi.

If my lift application have some data that cost to create, and I want
to share it among snippets, how to do in Lift?

if such data are shared inside one snippet, I may use "lazy val".
But I have no nice idea to share it among different snippts.

Thanks much.

Kevin Wright

unread,
Sep 26, 2009, 9:05:37 AM9/26/09
to lif...@googlegroups.com
You could always try putting your lazy val inside a singleton object

David Pollak

unread,
Sep 26, 2009, 9:33:46 AM9/26/09
to lif...@googlegroups.com
To share information between snippets during a request, use a RequestVar:

object MyInfo extends RequestVar(calculate_value)

so

object MyInfo extends RequestVar[Box[Invoice]](Empty)

in one snippet, you may calculate the Invoice and put it in the MyInfo:

MyInfo.set(Full(invoice))

In another snippet, you can extract:

for {
  invoice <- MyInfo.is
  } yield ...

Note that the "calculate_value" is a call-by-name parameter, so it will be invoked each time the RequestVar is uninitialized.  You can place lazy calculation logic in here.

--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics

Timothy Perrett

unread,
Sep 26, 2009, 9:35:01 AM9/26/09
to lif...@googlegroups.com
Take a look at RequestVar... If he were to just use a object singleton
he might end up being not thread safe.

Cheers

Tim

Sent from my iPhone

On 26 Sep 2009, at 14:05, Kevin Wright <kev.lee...@googlemail.com>
wrote:

Kevin Wright

unread,
Sep 26, 2009, 9:40:36 AM9/26/09
to lif...@googlegroups.com
If the information is created just once and then used across multiple
requests, it'll be a lazy val in a singleton

If it's request-dependent then it'll be a singleton extending RequestVar

Sadly, the OP didn't specify what was needed :(

David Pollak

unread,
Sep 26, 2009, 9:54:03 AM9/26/09
to lif...@googlegroups.com
On Sat, Sep 26, 2009 at 6:40 AM, Kevin Wright <kev.lee...@googlemail.com> wrote:

If the information is created just once and then used across multiple
requests, it'll be a lazy val in a singleton

A lazy val in a singleton will not work.

Once it's calculated, it's calculated and cannot be changed for the life of the singleton.  If the singleton is global scope, you're really hosed.

RequestVars are the Lift way of sharing information across snippets on a request-by-request basis.

If you want lazy evaluation of the RequestVar:

object Invoice extends RequestVar(S.param("invoice_id").flatMap(id => Invoice.find(id))) // type inferred to Box[Invoice]

If you want to share information across the scope of your session, use a SessionVar.

SessionVar works exactly like RequestVar, but the lifespan of the SessionVar is your session.

 

If it's request-dependent then it'll be a singleton extending RequestVar

Sadly, the OP didn't specify what was needed :(


On Sat, Sep 26, 2009 at 2:35 PM, Timothy Perrett
<tim...@getintheloop.eu> wrote:
>
> Take a look at RequestVar... If he were to just use a object singleton
> he might end up being not thread safe.
>
> Cheers
>
> Tim
>
> Sent from my iPhone
>
> On 26 Sep 2009, at 14:05, Kevin Wright <kev.lee...@googlemail.com>
> wrote:
>
>>
>> You could always try putting your lazy val inside a singleton object
>>
>> On Sat, Sep 26, 2009 at 8:14 AM, ishiijp <yoshino...@gmail.com>
>> wrote:
>>>
>>> Hi.
>>>
>>> If my lift application have some data that cost to create, and I want
>>> to share it among snippets, how to do in Lift?
>>>
>>> if such data are shared inside one snippet, I may use "lazy val".
>>> But I have no nice idea to share it among different snippts.
>>>
>>> Thanks much.
>>>
>>>>
>>>
>>
>> >
>>
>
> >
>


Kevin Wright

unread,
Sep 26, 2009, 10:21:30 AM9/26/09
to lif...@googlegroups.com
On Sat, Sep 26, 2009 at 2:54 PM, David Pollak
<feeder.of...@gmail.com> wrote:
>
>
> On Sat, Sep 26, 2009 at 6:40 AM, Kevin Wright
> <kev.lee...@googlemail.com> wrote:
>>
>> If the information is created just once and then used across multiple
>> requests, it'll be a lazy val in a singleton
>
> A lazy val in a singleton will not work.
> Once it's calculated, it's calculated and cannot be changed for the life of
> the singleton.  If the singleton is global scope, you're really hosed.
> RequestVars are the Lift way of sharing information across snippets on a
> request-by-request basis.
> If you want lazy evaluation of the RequestVar:
> object Invoice extends RequestVar(S.param("invoice_id").flatMap(id =>
> Invoice.find(id))) // type inferred to Box[Invoice]
> If you want to share information across the scope of your session, use a
> SessionVar.
> SessionVar works exactly like RequestVar, but the lifespan of the SessionVar
> is your session.
>

The OP never actually stated "share between snippets across a session"
or "share between snippets across a request". It was simply "share
between snippets"

Almost certainly SessionVar or RequestVar are what will be needed, but
what if the data is truly global in nature? Something like the 2001
average world exchange rates vs USD, which requires a costly
webservice or database lookup to find.

The original request was vague, but global caching is definitely one
possible interpretation of what was wanted.

ishiijp

unread,
Oct 4, 2009, 5:41:20 AM10/4/09
to Lift
Thank you for your example, David.
It will work in my purpose.

It seems that my poor English and less information let some people
confused.
I need just a request scope data.
It means I want to share information between snippets across a
request.
(Is the expression "request scope" not good for in this case...?)

Thank all responsed my post,
and your suggestions are informative.
I think very nice community is here.

On 9月26日, 午後10:33, David Pollak <feeder.of.the.be...@gmail.com> wrote:
> To share information between snippets during a request, use a RequestVar:
> object MyInfo extends RequestVar(calculate_value)
>
> so
>
> object MyInfo extends RequestVar[Box[Invoice]](Empty)
>
> in one snippet, you may calculate the Invoice and put it in the MyInfo:
>
> MyInfo.set(Full(invoice))
>
> In another snippet, you can extract:
>
> for {
>   invoice <- MyInfo.is
>   } yield ...
>
> Note that the "calculate_value" is a call-by-name parameter, so it will be
> invoked each time the RequestVar is uninitialized.  You can place lazy
> calculation logic in here.
>
> On Sat, Sep 26, 2009 at 12:14 AM, ishiijp <yoshinori.is...@gmail.com> wrote:
>
> > Hi.
>
> > If my lift application have some data that cost to create, and I want
> > to share it among snippets, how to do in Lift?
>
> > if such data are shared inside one snippet, I may use "lazy val".
> > But I have no nice idea to share it among different snippts.
>
> > Thanks much.
>
> --
> Lift, the simply functional web frameworkhttp://liftweb.net
> Beginning Scalahttp://www.apress.com/book/view/1430219890

David Pollak

unread,
Oct 5, 2009, 11:15:57 AM10/5/09
to lif...@googlegroups.com
On Sun, Oct 4, 2009 at 2:41 AM, ishiijp <yoshino...@gmail.com> wrote:

Thank you for your example, David.
It will work in my purpose.

Cool.
 

It seems that my poor English and less information let some people
confused.

I appreciate that you have put in the work to translate your thoughts to English.  I apologize that I only speak English, but would love to speak Japanese, German, Russian, and a few other languages.
 
I need just a request scope data.
It means I want to share information between snippets across a
request.
(Is the expression "request scope" not good for in this case...?)

"request scope" is exactly the right phrase.



--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890

marius d.

unread,
Oct 5, 2009, 11:52:17 AM10/5/09
to Lift


On Oct 5, 6:15 pm, David Pollak <feeder.of.the.be...@gmail.com> wrote:
> On Sun, Oct 4, 2009 at 2:41 AM, ishiijp <yoshinori.is...@gmail.com> wrote:
>
> > Thank you for your example, David.
> > It will work in my purpose.
>
> Cool.
>
>
>
> > It seems that my poor English and less information let some people
> > confused.
>
> I appreciate that you have put in the work to translate your thoughts to
> English.  I apologize that I only speak English, but would love to speak
> Japanese, German, Russian, and a few other languages.

oh you gotta learn Romanian :)

Jack Widman

unread,
Oct 5, 2009, 12:02:54 PM10/5/09
to lif...@googlegroups.com
Why don't we make Esperanto the official Lift language?
--
Jack

Viktor Klang

unread,
Oct 5, 2009, 3:49:27 PM10/5/09
to lif...@googlegroups.com
On Mon, Oct 5, 2009 at 6:02 PM, Jack Widman <jack....@gmail.com> wrote:
Why don't we make Esperanto the official Lift language?


I'd say make Scala the official language ;)
 



--
Viktor Klang

Blog: klangism.blogspot.com
Twttr: viktorklang

Lift Committer - liftweb.com
AKKA Committer - akkasource.org
Cassidy - github.com/viktorklang/Cassidy.git
SoftPub founder: http://groups.google.com/group/softpub

Jack Widman

unread,
Oct 5, 2009, 3:51:02 PM10/5/09
to lif...@googlegroups.com
Well it is high enough level! 
--
Jack

Kevin Wright

unread,
Oct 5, 2009, 3:51:48 PM10/5/09
to lif...@googlegroups.com
I was about to offer a smattering of Greek, Swedish and Hungarian (don't ask...)
But scala has to take the prize, I wonder if we couldn't implement
lojban as a DSL? :)

Timothy Perrett

unread,
Oct 5, 2009, 12:35:04 PM10/5/09
to lif...@googlegroups.com
Call me old fashioned, but good ol' English seems to be quite
prevalent these days ;-)

David Pollak

unread,
Oct 5, 2009, 5:51:34 PM10/5/09
to lif...@googlegroups.com
On Mon, Oct 5, 2009 at 9:35 AM, Timothy Perrett <tim...@getintheloop.eu> wrote:

Call me old fashioned, but good ol' English seems to be quite
prevalent these days ;-)

Is this the Queen's English? ;-)
 

On 5 Oct 2009, at 17:02, Jack Widman wrote:

> Why don't we make Esperanto the official Lift language?






--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890

Jack Widman

unread,
Oct 5, 2009, 5:54:08 PM10/5/09
to lif...@googlegroups.com
I'm not sure English is Turing Complete. Also not sure how prominent it will be in 50 years ... :)
--
Jack

Kevin Wright

unread,
Oct 5, 2009, 6:01:37 PM10/5/09
to lif...@googlegroups.com
On Mon, Oct 5, 2009 at 10:54 PM, Jack Widman <jack....@gmail.com> wrote:
> I'm not sure English is Turing Complete. Also not sure how prominent it will
> be in 50 years ... :)

Nor I, but I'm certain that Turing was (Queen's) English complete... :)

Timothy Perrett

unread,
Oct 5, 2009, 6:06:24 PM10/5/09
to lif...@googlegroups.com
Only the Queen speaks the queens english - your average schmuck on
this isle can only just about manage to string together a sentence!
HAHA.

Cheers, Tim

Viktor Klang

unread,
Oct 6, 2009, 4:43:19 AM10/6/09
to lif...@googlegroups.com
On Tue, Oct 6, 2009 at 12:01 AM, Kevin Wright <kev.lee...@googlemail.com> wrote:

On Mon, Oct 5, 2009 at 10:54 PM, Jack Widman <jack....@gmail.com> wrote:
> I'm not sure English is Turing Complete. Also not sure how prominent it will
> be in 50 years ... :)

Nor I, but I'm certain that Turing was (Queen's) English complete... :)

Didn't the Queen's England like have him castrated for being homosexual as well?

http://en.wikipedia.org/wiki/Alan_Turing#Conviction_for_gross_indecency
 

> On Mon, Oct 5, 2009 at 5:51 PM, David Pollak <feeder.of...@gmail.com>
> wrote:
>>
>>
>> On Mon, Oct 5, 2009 at 9:35 AM, Timothy Perrett <tim...@getintheloop.eu>
>> wrote:
>>>
>>> Call me old fashioned, but good ol' English seems to be quite
>>> prevalent these days ;-)
>>
>> Is this the Queen's English? ;-)
>>
>>>
>>> On 5 Oct 2009, at 17:02, Jack Widman wrote:
>>>
>>> > Why don't we make Esperanto the official Lift language?
>>>
>>>
>>>
>>
>>
>>
>> --
>> Lift, the simply functional web framework http://liftweb.net
>> Beginning Scala http://www.apress.com/book/view/1430219890
>> Follow me: http://twitter.com/dpp
>> Surf the harmonics
>>
>>
>
>
>
> --
> Jack
>
> >
>


Reply all
Reply to author
Forward
0 new messages