kiat has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I need to make use of the total number of a certain item in the database. This value has the following characteristics:

1) it does change from time to time

2) it is used every time the webpage (the main page) is requested

This total value can be queried from the database. I'm wondering, however, if it should be hardcoded into the script given the above characteristics.

What are your views?

Have a blessed Christmas ahead :)

Replies are listed 'Best First'.
Re: Best way of storing semi-static data
by freddo411 (Chaplain) on Dec 23, 2004 at 02:07 UTC
    Since you mention this is on your main page, it sounds like you are worried a bit about performance. A static page is much faster than a page that requires a DB lookup, however, some web server setups already incur much of the expense of establishing a DB connection so the "cost" might not be so high; it depends on your particular circustances. Let's assume that the choice is between a static page and the full cost of establishing a DB connection and quering the DB for the info.

    You can build a semi-static site quite easily. Use HTML::Template (or other templating module) to merge semi-static db data into static .html files. Psuedo code below: (note this does one file, but you could easily do many with this same script )

    use HTML::Template my %vars = getStuffFromDB(); # open the html template my $t = HTML::Template->new(filename => 'ffff.tmpl'); $t->param( foo => $var{'foo'}; # open $filehandle to .html file .. snip .. # output print $fh $t->output;

    -------------------------------------
    Nothing is too wonderful to be true
    -- Michael Faraday

      Thanks, freddo411!

      That's the kind of solution I'm having. At this point in time, performance isn't a problem because the traffic to the site is low. But I'm not certain whether performance will take a dip when more people are simultaneously on the site.

        As suggested above by yacoubean, to complete the solution you can have the "makeHTML.pl" script run nightly to make sure that your static page is up to date. Obviously you can set this update interval to any time period you like. Even if you do it as often as hourly your users will enjoy the benefit of a very fast static page.

        On Unix the scheduler is called "cron" and the schedule to run things is called a crontab. The command crontab -e will allow you to edit your personal crontab entries. You'll want to set up an ENV var for your visual editor before you run crontab -e (read the man page for details).

        Merrry Xmas

        -------------------------------------
        Nothing is too wonderful to be true
        -- Michael Faraday

Re: Best way of storing semi-static data
by perrin (Chancellor) on Dec 23, 2004 at 01:47 UTC
Re: Best way of storing semi-static data
by yacoubean (Scribe) on Dec 23, 2004 at 01:20 UTC
    I'd think a query to get one value wouldn't slow your page down enough to worry about it, but I guess it depends on the circumstances. If you are concerned about it, I guess you could hard code the total in your file, and have a perl script in your scheduler update it once a day (or whenever you deem best). I did that once for a value that took a few minutes to calculate every time.

    My home on the web: http://www.techfeed.net/