Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

Reading Database At Startup

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


publiustemp-catalyst at yahoo

Aug 16, 2009, 8:09 AM

Post #1 of 14 (2200 views)
Permalink
Reading Database At Startup

Hi all,

This should be painfully obvious, but I don't see it :)

For a personal project, I want users to be able to click on a letter and get a list of countries starting with that letter. I can do this:

my $letters = $c->model('DB')->storage->dbh->selectcol_arrayref(
'select distinct(substr(name,1,1)) as letter from country order by letter'
);
$c->stash->{letters} = $letters;


But the country list is static and I want this available at startup. 'sub begin' fails because that's called once per request, not once per app.

What's the recommend way of handling this? (I'm using DBIx::Class for the model)

Cheers,
Ovid
--
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog - http://use.perl.org/~Ovid/journal/
Twitter - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


mdietrich at cpan

Aug 16, 2009, 8:52 AM

Post #2 of 14 (2127 views)
Permalink
Re: Reading Database At Startup [In reply to]

Ovid,

Am 16.08.2009 um 17:09 schrieb Ovid:

> Hi all,
>
> This should be painfully obvious, but I don't see it :)
>
> For a personal project, I want users to be able to click on a letter
> and get a list of countries starting with that letter. I can do this:
>
> my $letters = $c->model('DB')->storage->dbh->selectcol_arrayref(
> 'select distinct(substr(name,1,1)) as letter from country
> order by letter'
> );
> $c->stash->{letters} = $letters;
>
>
> But the country list is static and I want this available at
> startup. 'sub begin' fails because that's called once per request,
> not once per app.
>
> What's the recommend way of handling this? (I'm using DBIx::Class
> for the model)

I've done something similar with C::P::I18N::DBI (see: http://search.cpan.org/perldoc?Catalyst::Plugin::I18N::DBI
) but I think it wouldn't be recommended to build a plugin in your
case.

matt

--
rainboxx Matthias Dietrich
Freier Software Engineer

rainboxx | Tel.: +49 (0) 151 / 50 60 78 64
Tölzer Str. 19 | Mail: matt [at] rainboxx
70372 Stuttgart | WWW : http://www.rainboxx.de

XING: https://www.xing.com/profile/Matthias_Dietrich18
GULP: http://www.gulp.de/profil/rainboxx.html
Attachments: PGP.sig (0.19 KB)


romkey at apocalypse

Aug 16, 2009, 9:00 AM

Post #3 of 14 (2122 views)
Permalink
Re: Reading Database At Startup [In reply to]

On Aug 16, 2009, at 11:09 AM, Ovid wrote:
> Hi all,
>
> This should be painfully obvious, but I don't see it :)
>
> For a personal project, I want users to be able to click on a letter
> and get a list of countries starting with that letter. I can do this:
>
> my $letters = $c->model('DB')->storage->dbh->selectcol_arrayref(
> 'select distinct(substr(name,1,1)) as letter from country
> order by letter'
> );
> $c->stash->{letters} = $letters;
>
>
> But the country list is static and I want this available at
> startup. 'sub begin' fails because that's called once per request,
> not once per app.
>
> What's the recommend way of handling this? (I'm using DBIx::Class
> for the model)

I do something like this; on my app I have a list of towns and states
that I need to build from the database and is fairly static.

What I do is build it on demand and cache the results using
Catalyst::Plugin::Cache and Catalyst::Plugin::Cache::Store::Memory

Because every page in my app uses this list to provide a list for
searching these locales, in the auto() handler in Root.pm I check to
see if the cache entry for the data structure is there and if it's
not, I create it from the database and cache it. I also cache counts
of entries in each town. Since these counts can change without me
restarting the app, I just rebuild the cache when I add or delete a
listing from a town. I never expire the cache, there's no need.

This means that the first page hit on any instance of the server takes
a little while longer than usual, but it's really not a significant
performance hit.
- john romkey
http://www.romkey.com/


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


publiustemp-catalyst at yahoo

Aug 16, 2009, 9:58 AM

Post #4 of 14 (2123 views)
Permalink
Re: Reading Database At Startup [In reply to]

----- Original Message ----

> From: John Romkey <romkey [at] apocalypse>

> I do something like this; on my app I have a list of towns and states that I
> need to build from the database and is fairly static.
>
> What I do is build it on demand and cache the results using
> Catalyst::Plugin::Cache and Catalyst::Plugin::Cache::Store::Memory


Works like a charm. Thanks :)

Cheers,
Ovid
--
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog - http://use.perl.org/~Ovid/journal/
Twitter - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


darren at darrenduncan

Aug 16, 2009, 4:08 PM

Post #5 of 14 (2115 views)
Permalink
Re: Reading Database At Startup [In reply to]

Ovid wrote:
> For a personal project, I want users to be able to click on a letter and get a list of countries starting with that letter. I can do this:

Considering that there are only about 250 countries in total, you should just
list them all on one screen and be done with it.

I think that would be a lot more user friendly than requiring them to click a
letter first, especially if they're not sure exactly how their country is
spelled or what letter it might be under (eg, is "Republic of Ireland" under "I"
or "R").

Save clicking a letter when you're doing a list of cities or something.

> my $letters = $c->model('DB')->storage->dbh->selectcol_arrayref(
> 'select distinct(substr(name,1,1)) as letter from country order by letter'
> );
> $c->stash->{letters} = $letters;

Also, to use 'distinct' I don't think you ever use parens; you just say "select
distinct <foo-expr> as bar from baz ...".

-- Darren Duncan


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


edencardim at gmail

Aug 19, 2009, 4:44 AM

Post #6 of 14 (2069 views)
Permalink
Re: Reading Database At Startup [In reply to]

On Sun, Aug 16, 2009 at 12:09 PM, Ovid<publiustemp-catalyst [at] yahoo> wrote:
> Hi all,
>
> This should be painfully obvious, but I don't see it :)
>
> For a personal project, I want users to be able to click on a letter and get a list of countries starting with that letter.  I can do this:
>
>    my $letters = $c->model('DB')->storage->dbh->selectcol_arrayref(
>        'select distinct(substr(name,1,1)) as letter from country order by letter'
>    );
>    $c->stash->{letters} = $letters;
>
>
> But the country list is static and I want this available at startup.  'sub begin' fails because that's called once per request, not once per app.
>
> What's the recommend way of handling this?  (I'm using DBIx::Class for the model)

package MyApp::Model::DB;

extends 'Catalyst::Model::DBIC::Schema';

has country_letters => (isa => 'ArrayRef', is => 'ro', lazy_build => 1);

sub _build_country_letters {
my($self) = @_;
return $self->storage->dbh->selectcol_arrayref(
'select distinct(substr(name,1,1)) as letter from country order by letter'
);
};

sub BUILD { shift->country_letters } # optionally, force load-time construction

# in a distant controller

$c->stash->{letters} = $c->model('DB')->country_letters;

--
Eden Cardim Need help with your Catalyst or DBIx::Class project?
Code Monkey http://www.shadowcat.co.uk/catalyst/
Shadowcat Systems Ltd. Want a managed development or deployment platform?
http://edenc.vox.com/ http://www.shadowcat.co.uk/servers/

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


m at qx

Aug 19, 2009, 7:35 AM

Post #7 of 14 (2061 views)
Permalink
Re: Reading Database At Startup [In reply to]

On Wed, Aug 19, 2009 at 9:44 PM, Eden Cardim <edencardim [at] gmail> wrote:

> On Sun, Aug 16, 2009 at 12:09 PM, Ovid<publiustemp-catalyst [at] yahoo>
> wrote:
> > Hi all,
> >
> > This should be painfully obvious, but I don't see it :)
> >
> > For a personal project, I want users to be able to click on a letter and
> get a list of countries starting with that letter. I can do this:
> >
> > my $letters = $c->model('DB')->storage->dbh->selectcol_arrayref(
> > 'select distinct(substr(name,1,1)) as letter from country order by
> letter'
> > );
> > $c->stash->{letters} = $letters;
> >
> >
> > But the country list is static and I want this available at startup.
> 'sub begin' fails because that's called once per request, not once per app.
> >
> > What's the recommend way of handling this? (I'm using DBIx::Class for
> the model)
>
> package MyApp::Model::DB;
>
> extends 'Catalyst::Model::DBIC::Schema';
>
> has country_letters => (isa => 'ArrayRef', is => 'ro', lazy_build => 1);
>
> sub _build_country_letters {
> my($self) = @_;
> return $self->storage->dbh->selectcol_arrayref(
> 'select distinct(substr(name,1,1)) as letter from country order by
> letter'
> );
> };
>
> sub BUILD { shift->country_letters } # optionally, force load-time
> construction
>
> # in a distant controller
>
> $c->stash->{letters} = $c->model('DB')->country_letters;
>
> --
> Eden Cardim Need help with your Catalyst or DBIx::Class project?
> Code Monkey http://www.shadowcat.co.uk/catalyst/
> Shadowcat Systems Ltd. Want a managed development or deployment platform?
> http://edenc.vox.com/ http://www.shadowcat.co.uk/servers/


Also FYI it's pretty easy to do something on startup, inside MyApp.pm:

sub run {
my $c = shift;
$c->do_stuff();
$c->SUPER(@_);
}


bobtfish at bobtfish

Aug 19, 2009, 11:40 AM

Post #8 of 14 (2067 views)
Permalink
Re: Reading Database At Startup [In reply to]

On 19 Aug 2009, at 15:35, Matt Koscica wrote:

> On Wed, Aug 19, 2009 at 9:44 PM, Eden Cardim <edencardim [at] gmail>
> wrote:

Why did you feel that quoting Eden's entire post, including signature
was a good idea? Bottom posting and including the _ENTIRE_ message is
even worse that top posing IMO, as it forces the reader to scroll
past the message _they just read already_...

> Also FYI it's pretty easy to do something on startup, inside MyApp.pm:
>
> sub run {
> my $c = shift;
> $c->do_stuff();
> $c->SUPER(@_);
> }

Nooo! NEVER, EVER CALL ->SUPER in a Catalyst application, it will do
the wrong thing as Catalyst relies on multiple inheritance, and SUPER
is likely to call the wrong thing.

You should be using ->next::method.

Also, even that is fairly fugly - I'd highly recommend using Moose's
method modifiers, so you can write:

before run => sub {
my $c = shift;
# Do stuff
};

instead - no method redispatch to cock up the arguments on..

Cheers
t0m


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


2009 at denny

Aug 19, 2009, 12:13 PM

Post #9 of 14 (2070 views)
Permalink
Re: Netiquette (was: Reading Database At Startup) [In reply to]

On Wed, 2009-08-19 at 19:40 +0100, Tomas Doran wrote:
> On 19 Aug 2009, at 15:35, Matt Koscica wrote:
>
> > On Wed, Aug 19, 2009 at 9:44 PM, Eden Cardim <edencardim [at] gmail>
> > wrote:
>
> Why did you feel that quoting Eden's entire post, including signature
> was a good idea? Bottom posting and including the _ENTIRE_ message is
> even worse that top posing IMO, as it forces the reader to scroll
> past the message _they just read already_...

Yes, but. Didn't we have this discussion a few months ago, with zero
result?

Specifically, J. Shirley almost always does this (bottom posting after
quoting entire message _with list sigs too_), and gave some other people
a hard time for pointing out that it's actually really unhelpful, in
reply to him pointing out quite stridently that he doesn't like
top-posting. Apparently as long as you post at the bottom it doesn't
matter if it's easy to read your reply or not.

Digging through archives, that discussion seems to start around 1pm on
27 April 2009 (GMT/UTC), although the top posting discussion started
prior to that.

Regards,
Denny
Attachments: signature.asc (0.19 KB)


bobtfish at bobtfish

Aug 19, 2009, 12:23 PM

Post #10 of 14 (2068 views)
Permalink
Re: Netiquette (was: Reading Database At Startup) [In reply to]

On 19 Aug 2009, at 20:13, Denny wrote:
> Yes, but. Didn't we have this discussion a few months ago, with zero
> result?

Yes, sorry - I should shut the fuck up, and should have just replied
in the manor in which I preferred.

Please lets not have the meta discussion again?

Cheers
t0m


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


2009 at denny

Aug 19, 2009, 12:24 PM

Post #11 of 14 (2066 views)
Permalink
Re: Netiquette (was: Reading Database At Startup) [In reply to]

On Wed, 2009-08-19 at 20:13 +0100, Denny wrote:
> Specifically, J. Shirley almost always does this (bottom posting after
> quoting entire message _with list sigs too_)

I retract that. Looking at the archives it seems he finally started
trimming the list sigs off the bottom sometime around June/July.
Attachments: signature.asc (0.19 KB)


jshirley at gmail

Aug 19, 2009, 5:18 PM

Post #12 of 14 (2054 views)
Permalink
Re: Netiquette (was: Reading Database At Startup) [In reply to]

On Aug 19, 2009, at 12:24 PM, Denny wrote:

> On Wed, 2009-08-19 at 20:13 +0100, Denny wrote:
>> Specifically, J. Shirley almost always does this (bottom posting
>> after
>> quoting entire message _with list sigs too_)
>
> I retract that. Looking at the archives it seems he finally started
> trimming the list sigs off the bottom sometime around June/July.


You should retract it, and then some. Look back further and you'll see
my standard is trimming the list sigs.



_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


hoagy at ytfc

Aug 19, 2009, 5:35 PM

Post #13 of 14 (2058 views)
Permalink
Re: Netiquette (was: Reading Database At Startup) [In reply to]

Hello 1990 and silly arguments about quoting and top-posting v bottom-
posting etc. That's not what I joined this list for.

I know that 15-20 years ago it was the LAW that every email list or
Usenet group went through this, but let's move on, eh?



(BTW, top-posting is correct now. Ducks for cover).



On 20 Aug 2009, at 01:18, J. Shirley wrote:

> On Aug 19, 2009, at 12:24 PM, Denny wrote:
>
>> On Wed, 2009-08-19 at 20:13 +0100, Denny wrote:
>>> Specifically, J. Shirley almost always does this (bottom posting
>>> after
>>> quoting entire message _with list sigs too_)
>>
>> I retract that. Looking at the archives it seems he finally started
>> trimming the list sigs off the bottom sometime around June/July.
>
>
> You should retract it, and then some. Look back further and you'll see
> my standard is trimming the list sigs.
>
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


edencardim at gmail

Aug 20, 2009, 6:01 AM

Post #14 of 14 (2019 views)
Permalink
Re: Reading Database At Startup [In reply to]

On Wed, Aug 19, 2009 at 3:40 PM, Tomas Doran<bobtfish [at] bobtfish> wrote:
> before run => sub {
>    my $c = shift;
>    # Do stuff
> };
>
> instead - no method redispatch to cock up the arguments on..

Besides that, I wouldn't execute model-related logic anywhere inside
MyApp.pm, since that will take a you a step closer to creating a god
object.

--
Eden Cardim Need help with your Catalyst or DBIx::Class project?
Code Monkey http://www.shadowcat.co.uk/catalyst/
Shadowcat Systems Ltd. Want a managed development or deployment platform?
http://edenc.vox.com/ http://www.shadowcat.co.uk/servers/

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.