You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by RJ Herrick <rj...@sbcglobal.net> on 2006/02/05 01:03:14 UTC
Clarifying Q: Usage suggestions: caching DB metadata with mod_perl?
Thanks to the Johns for their responses, but I believe perhaps I was
unclear about my intent.
In validating my form input I look at my DB tables to see what types of
input they will accept (valid values, maxlength, NULL ok, etc). This
data is about the database, not the values coming in off the forms. It
will stay the same until I alter my tables, which I won't be doing very
often (in production at least :) ). So I'm looking to avoid gathering
this metadata via DB calls every time I need to validate a form by
instead using the cached metadata, stored somewhere by mod_perl. My
thought was to put it into it's own namespace and have it available
globally, loaded from one of my modules at startup.
Does anyone else understand what I'm trying to say? I apologize if I'm
not expressing myself in the right vernacular.
Thanks,
RJ Herrick
>
> On Feb 4, 2006, at 12:25 AM, RJ Herrick wrote:
>
>> Hi all,
>> I'm starting to do a lot of form processing, and came across the
>> idea of caching table metadata while validating to avoid unnecessary
>> DB (MySQL) calls. I just realized that, with mod_perl, I could even
>> do this across requests- and since the data shouldn't be changing,
>> this seems like a good way to trade some memory for a boost in speed.
>> I'm still very much on the learning curve with mod_perl and wanted
>> to see if anyone out there was already doing this, and if so, any
>> suggestions / comments / advise they might have.
>> Thank you all in advance,
>> RJ Herrick
>>
>> --
>> "A foolish consistency is the hobgoblin of little minds" -Ralph
>> Waldo Emerson
>>
--
"A foolish consistency is the hobgoblin of little minds" -Ralph Waldo Emerson
Re: Clarifying Q: Usage suggestions: caching DB metadata with mod_perl?
Posted by John Doe <se...@tele2.ch>.
RJ Herrick am Sonntag, 5. Februar 2006 01.03:
> Thanks to the Johns for their responses, but I believe perhaps I was
> unclear about my intent.
>
> In validating my form input I look at my DB tables to see what types of
> input they will accept (valid values, maxlength, NULL ok, etc). This
> data is about the database, not the values coming in off the forms. It
> will stay the same until I alter my tables, which I won't be doing very
> often (in production at least :) ). So I'm looking to avoid gathering
> this metadata via DB calls every time I need to validate a form by
> instead using the cached metadata, stored somewhere by mod_perl. My
> thought was to put it into it's own namespace and have it available
> globally, loaded from one of my modules at startup.
> Does anyone else understand what I'm trying to say? I apologize if I'm
> not expressing myself in the right vernacular.
> Thanks,
> RJ Herrick
You could have a Cfg.pm module with a
my %Cache
variable. The new() method of the module fills the created object once into
this hash (keyed by the form name in your case). On further invocations of
new(), it returns the cached object from %Cache.
### The module also contains a sub
sub preload_cache {
my ($class, @formnames)=@_;
my $dummy=__PACKAGE__->new($_)
for @formnames;
}
### Then, in the startup.pl:
use Cfg;
Cfg::preload_cache(qw / formname1 formname2 ... /);
### and somewhere else in the app:
my $cfg=Cfg->new($formname);
###
I have such a module in production and, since I was not sure if the config
objects stay shared (not very important in my case), I checked the addresses
on different usage. They seem to be always the same.
But I'm confused: The config objects are hold in object attributes of other
objects. This means, the config objects reference counts increase - and I
thought that this is an occasion where the object does not stay shared?!?
I can post the module if anybody wishes (good occasion for a code review).
hth, joe
Re: Clarifying Q: Usage suggestions: caching DB metadata with mod_perl?
Posted by Jonathan <jv...@2xlp.com>.
Well I couldn't use Data::FormValidator for my exact needs
There were too many if/else conditions I needed to implement into my
form class that I couldn't do with its constraint and dependency
system easily ( a bunch of multiple-field validations that would even
change the valid repsonse sets . its kinda messy in my system, but
it works very easily to chain conditionals)
So I built my own heavily using parts from Data::FormValidator and
FormBuilder
It's not nearly as robust or as good, but borrows a lot of code and
ideas from both modules, so i wouldn't say that i reinvented it, but
built from it.
On Feb 5, 2006, at 5:15 PM, Michael Peters wrote:
> Looks like you're trying to reinvent Data::FormValidator.
Re: Clarifying Q: Usage suggestions: caching DB metadata with mod_perl?
Posted by Michael Peters <mp...@plusthree.com>.
Jonathan wrote:
>
> On Feb 4, 2006, at 7:03 PM, RJ Herrick wrote:
>
>> In validating my form input I look at my DB tables to see what types
>> of input they will accept (valid values, maxlength, NULL ok, etc).
>> This data is about the database, not the values coming in off the
>> forms. It will stay the same until I alter my tables, which I won't
>> be doing very often (in production at least :) ). So I'm looking to
>> avoid gathering this metadata via DB calls every time I need to
>> validate a form by instead using the cached metadata, stored
>> somewhere by mod_perl. My thought was to put it into it's own
>> namespace and have it available globally, loaded from one of my
>> modules at startup.
>> Does anyone else understand what I'm trying to say? I apologize if
>> I'm not expressing myself in the right vernacular.
>> Thanks,
>> RJ Herrick
>
>
> Oh. In that case... i put stuff like that in its own namespace during
> the startup.pl phase.
>
> Suggestion though- (and this gets completely off topic)
>
> it might make sense to have some sort of form builder/validator class
>
> At the risk of making some people here cringe, in my projects I do
> something like
Looks like you're trying to reinvent Data::FormValidator.
--
Michael Peters
Developer
Plus Three, LP
Re: Clarifying Q: Usage suggestions: caching DB metadata with mod_perl?
Posted by Jonathan <jv...@2xlp.com>.
On Feb 4, 2006, at 7:03 PM, RJ Herrick wrote:
> In validating my form input I look at my DB tables to see what
> types of input they will accept (valid values, maxlength, NULL ok,
> etc). This data is about the database, not the values coming in off
> the forms. It will stay the same until I alter my tables, which I
> won't be doing very often (in production at least :) ). So I'm
> looking to avoid gathering this metadata via DB calls every time I
> need to validate a form by instead using the cached metadata,
> stored somewhere by mod_perl. My thought was to put it into it's
> own namespace and have it available globally, loaded from one of my
> modules at startup.
> Does anyone else understand what I'm trying to say? I apologize if
> I'm not expressing myself in the right vernacular.
> Thanks,
> RJ Herrick
Oh. In that case... i put stuff like that in its own namespace
during the startup.pl phase.
Suggestion though- (and this gets completely off topic)
it might make sense to have some sort of form builder/validator class
At the risk of making some people here cringe, in my projects I do
something like
/Form.pm
/Form/account/registration.pm
/Form/account/settings.pm
where Form.pm is a base class that has my config data (ie, use
libapreq for form parsing, db handles, etc) , and inherits from my
form parsing class
registration would be a subclass of Form that has a symantic
description like
our %Fields=(
'email' =>{
type=>'text',
'maxchars'=>'6'
...
'validators' => ['REGEX','SQL_FUNCTION'],
'validator_regex = 'email',
validator_sql = ['false','SELECT user_id FROM user WHERE user_id = ?;']
' validator_regex' => 'not an email',
'validator_email' => 'already registerd'
}
);
when I do a form validation, i just do:
my $form = Form::Account::Registration->new();
$form->validate();
if ( $form->has_errors() )
{
$self->printform;return;
}
my $email = $form->get_validated('email')
i do it in classes so that I can re-use the forms. i had one project
where I did a formfield class that had versions of every field used,
and then i'd just specify while field description in the form class.
but that saved me 2% memory while it gave me 30% more confusion. so
i said screw that.
there are a bunch of CPAN packages that do form validation like
that , but most are focused on building , which can be overkill.