[Catalyst] Store something in the stash at startup

Charlie Garrison garrison at zeta.org.au
Tue Aug 17 14:50:33 GMT 2010


Good morning,

On 17/08/10 at 8:59 AM -0500, will at serensoft.com wrote:

>On Tue, Aug 17, 2010 at 6:23 AM, Tomas Doran <bobtfish at bobtfish.net> wrote:
>
>> No, it wouldn't. The adaptor you showed has a lifetime of the application,
>> and so it won't be recreated each request.
>>
>
></lurk>
>Hmm, this is interesting. Are you referring to the "Moose goodness" of
>
>has "menu_data" => (
>is => "ro",
>isa => "HashRef",
>default => sub { shift->config->{menu_data} }, #pull data from config
>);
>
>Is that the part you're referring to as application-lifetime?

No, he's referring to a part of one of the OP's messages that I 
missed, specifically:

>package BRK::Model::Menu;
>use strict;
>use warnings;
>use base 'Catalyst::Model::Adaptor';

Using Catalyst::Model::Adaptor as base for a model class will 
create the model (or class referred to by model) once for each 
app run.  There are also two other Adaptor modules for creating 
models for each request or for each call to $c->model. Choose 
the one appropriate for the desired model life-cycle; once for 
each app run, once for each request, or once each time $c->model 
is called.

Octavian, has your question been answered? At this point in the 
discussion I'm not clear where the problem is and what you need 
to solve. I would have thought that by using 
Catalyst::Model::Adaptor your app would be working as desired, 
but I may be missing the point.

I was suggesting the Moose attribute as way of storing the model 
in $c to be easier to reference from templates.

This is where I got confused:

On 17/08/10 at 8:27 AM +0300, Octavian Rasnita 
<octavian.rasnita at ssifbroker.ro> wrote:

>I load and store this object in the stash on each request in the Root::auto : Private method with:
>
>$c->stash(menu => $c->model('Menu'));
>
>But this would re-create the menu object on each request, and this takes time.

Since the model is created with Catalyst::Model::Adaptor, it's 
only instantiated once. So the only thing happening with the 
code above is adding the already-existing model to the stash for 
each request.

I also agree with t0m's comment about premature optimization. 
Unless you know there is a performance problem there then I 
doubt there is anything to fix.

Charlie

-- 
    Ꮚ Charlie Garrison ♊ <garrison at zeta.org.au>

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
〠  http://www.ietf.org/rfc/rfc1855.txt



More information about the Catalyst mailing list