[Catalyst] How to create accessors to preload static data once per server instance

bits bits_catalyst at itools.com
Wed Aug 22 21:58:19 GMT 2007


I struggled with this for way too long, and finally asked for some help on
#catalyst.  mst dished out some wisdom there that I've captured into a stub
that could be a starting point for a cookbook recipe:




When you have data that you want to load just once from the model at server
load instead of for each request, use mk_group_accessors to create accessors
and tie them to resultsets in your package that inherits from
DBIx::Class::Schema

    package My::Schema;
    use base qw/DBIx::Class::Schema/;
    __PACKAGE__->register_class('RESULTSOURCEMONIKER',
'My::Schema::RESULTSOURCE');
    
    __PACKAGE__->mk_group_accessors('simple' => qw(ACCESSORNAME1
ACCESSORNAME2 ACCESSORNAMEn));

    sub connection {
        my ($self, @rest) = @_;
        $self->next::method(@rest);
        
        # $self is now a live My::Schema object, complete with DB connection


        $self->ACCESSORNAME1([ $self->resultset('RESULTSOURCEMONIKER')->all
]);
        $self->ACCESSORNAME2([
$self->resultset('RESULTSOURCEMONIKER')->search({ COLUMN => { '<' => '30' }
})->all ]);
        $self->ACCESSORNAMEn([
$self->resultset('RESULTSOURCEMONIKER')->find(1) ]);
    }

    1;

and now in the controller, you can now access any of these without a
per-request fetch:

    $c->stash->{something} = $c->model('My::Schema')->schema->ACCESSORNAMEn;




Comments?




More information about the Catalyst mailing list