[Catalyst] Reading Database At Startup
John Romkey
romkey at apocalypse.org
Sun Aug 16 16:00:17 GMT 2009
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/
More information about the Catalyst
mailing list