[Catalyst] DB values that change very rarely

Jesse Sheidlower jester at panix.com
Thu Jan 10 15:15:57 GMT 2013


In one of my apps, I have a number of tables that contain values that
change very rarely. Think of something like a "category" table in an
e-commerce app--they're mostly static, but every now and then you need
to change or add or delete a category. These occasional changes do,
however, need to be made by a (privileged) user, rather than a
developer, so I can't just put them in the config file and edit this and
restart the app when necessary. Since I don't know when they might
change, I don't know how I could cache them, because when a change is
made it needs to take effect immediately.

These tables are used on almost every request. The result is that most
of my controllers end up looking something like this, or having
something like this at the end of them:

sub add : Local {
   my ( $self, $c ) = @_;

   $c->stash->{title} = 'Add a new lemma';

   $c->stash->{batches} = $c->model('WordsDB::Batch')->search();
   $c->stash->{statuses} = $c->model('WordsDB::Status')->search();
   $c->stash->{regions} = $c->model('WordsDB::Region')->search();
   $c->stash->{subjects} = $c->model('WordsDB::Subject')->search();

   $c->stash->{template} = 'add.tt';
}

This means that almost every page generation hits the database a whole bunch of
unnecessary times, and that all of my controllers are cluttered. 

This must be a fairly common problem. What's the best way to deal with
it--both the desire to persist what is mostly static data, and to keep
my controllers clean?

Thanks.

Jesse Sheidlower



More information about the Catalyst mailing list