[Dbix-class] DBIx and persistence or object caching?

Steve Francia steve at takkle.com
Wed May 23 20:17:38 GMT 2007


I am new to catalyst and DBIx. I am writing a small app that currently
has two tables and one relationship.

In running :
$c->model('vidDB::video')->all

Certain SQL queries are run multiple times:

    SELECT me.video_id, me.title, me.rating, me.owner_id, me.length,
    me.cr_date, me.location FROM video me:
    SELECT me.user_id, me.username, me.password, me.first_name,
    me.last_name FROM user me WHERE ( ( ( me.user_id =3D ? ) ) ): '1'
    SELECT me.user_id, me.username, me.password, me.first_name,
    me.last_name FROM user me WHERE ( ( ( me.user_id =3D ? ) ) ): '3'
    SELECT me.user_id, me.username, me.password, me.first_name,
    me.last_name FROM user me WHERE ( ( ( me.user_id =3D ? ) ) ): '2'
    SELECT me.user_id, me.username, me.password, me.first_name,
    me.last_name FROM user me WHERE ( ( ( me.user_id =3D ? ) ) ): '1'
    SELECT me.user_id, me.username, me.password, me.first_name,
    me.last_name FROM user me WHERE ( ( ( me.user_id =3D ? ) ) ): '2'
    SELECT me.user_id, me.username, me.password, me.first_name,
    me.last_name FROM user me WHERE ( ( ( me.user_id =3D ? ) ) ): '1'

Obviously this is not ideal. I have been searching the web and reading
the manuals, but so far I haven't found a way to turn on persistence.
The multiple queries are a result of one user "owning" multiple videos,
but I would think it could keep that information in the object so it
doesn't need to query the database again, at least during the same page
request. Can anyone direct me in how to do this? Ideally I would like to
use mem cache for this, but any cache would be better than repeated queries.

video is defined as :

    # Load required DBIC stuff
    __PACKAGE__->load_components(qw/PK::Auto Core/);
    # Set the table name
    __PACKAGE__->table('video');
    # Set columns in table
    __PACKAGE__->add_columns(qw/video_id title rating owner_id length
    cr_date location/);
    # Set the primary key for the table
    __PACKAGE__->set_primary_key(qw/video_id/);

    #
    # Set relationships:
    #

    # has_one():
    #   args:
    #     1) Name of relationship, DBIC will create accessor with this name
    #     2) Name of the model class referenced by this relationship
    #     3) Column name in *foreign* table
    __PACKAGE__->has_one(owner =3D> 'vidDB::user', { 'foreign.user_id' =3D>
    'self.owner_id' } );


and user is defined as :

    # Load required DBIC stuff
    __PACKAGE__->load_components(qw/PK::Auto Core/);
    # Set the table name
    __PACKAGE__->table('user');
    # Set columns in table
    __PACKAGE__->add_columns(qw/user_id username password first_name
    last_name/);
    # Set the primary key for the table
    __PACKAGE__->set_primary_key(qw/user_id/);

    #
    # Set relationships:
    #

    # has_many():
    #   args:
    #     1) Name of relationship, DBIC will create accessor with this name
    #     2) Name of the model class referenced by this relationship
    #     3) Column name in *foreign* table
    __PACKAGE__->belongs_to(video =3D> 'vidDB::video', 'user_id');

Thanks.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20070523/c7a=
628e7/attachment.htm


More information about the Dbix-class mailing list