On 5/23/06 2:36 AM, [EMAIL PROTECTED] wrote:
> this is pretty generic 3nf 'crap' tables -- stuff like
> 
> table filetype: id | name
> table accounttype: id | name
> 
> about 10 tables, ~200 id:name pairigs total.  bs stuff.  using straight
> sql in the framework i built though, i learned pre-caching that stuff
> saved a visible bit on postgres queries and almost no performance hit in
> perl.  so i do that.

You could do that with Rose::DB::Cached.  In fact, that's exactly what it's
for: caching "read-only" data.  Example:

    # Set up your ::Cached base class for all cached objects
    package My::DB::Object::Cached;
    use base 'Rose::DB::Object::Cached';
    ...
    sub init_db { ... }

    # Make a cached class for the filetype table
    package My::FileType;
    use base 'My::DB::Object::Cached';
    ...

    # Make a manager class for My::FileType
    package My::FileType::Manager;
    use base 'Rose::DB::Object::Manager';
    ...

    # On server start-up, cached all file types
    my $fts = My::FileType::Manager->get_filetypes();
    foreach my $ft (@$fts) { $ft->remember } # Cache them all

>From that point on, finding the name of a filetype id can be done like this:

    # Does not hit the db at all
    $name = My::FileType->new(id => 123)->load->name;

Of course, you can wrap that in a function to make it look nicer.

> am i correct in assuming that the only way to accomplish what I want to do
> ( add some new arbitrary fields to a result returned by a load; or
> get_magagerclass() ) is to clone the existing fieldnames into a new ,
> normal hash?

Just add methods to the row class itself.  For example, if you have a
My::Doc class that fronts the "documents" table which has a filetype_id
column, and you want all the My::Doc objects to have a "filetype_name"
method, do this:

    package My::Doc;
    use base 'My::DB::Object';
    ...
    use My::FileType;

    sub filetype_name
    {
      my($self) = shift;
      # Will not hit the db if all the My::FileType objects were
      # cached on server start-up.
      return My::FileType->new(id => $self->filetype_id)->load->name;
    }

Then as you fetch each My::Doc object from a Manager method, you can just
call:

    $doc->filetype_name

to get the file type as a name, and it won't hit the db at all.

-John




-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to