I often have a module that might serve as an interface to a record stored in a database. For example a 'person' object- which might hold vcard data, some info about them in relation to a company- etc.

So internally I have a load mechanism for the data from db.

I store it in the object until a save mechanism is called to commit changes if any, etc. So essentially the data is sort of cached in the object.

This could be done instead, as a direct call to the database. That is.. if you call for the 'person' object's 'name' method.. instead of something like (pseudocode):

sub name { my $self = shift; $self->{name} ||= $self->dbh->prepare('SELECT FROM ... ... $self->{name}; }
You would do something like..
sub name { my $self = shift; $self->dbh->prepare('SELECT FROM ... ... $result; }
So, in essence, we are making calls for everything, every time.

I would think the lag time would be apocalyptic- and the whole idea abusive to the db server.

Am I wrong? Does anybody have experience fetching and storing data via an api, directly? Without caching stuff in the object.. ?

Seems that it could be more convenient to code this way. Maybe a little more risky?


In reply to not caching to object in code interface to database? by leocharre

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.