You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by "Vladimir S. Tikhonjuk" <vs...@vst.donetsk.ua> on 2006/08/17 18:53:07 UTC

mod_perl comforts

    Hi all!

    I need some advice on how to make some comfort into mod_perl
Response Handler. 

    I used cookies and sessions to authenticate and authorizate user
into my app ( Apache2::AuthCookie ). So, every handler ( or most of them
) have to get cookie, take a session id from it, makes session, and only
then
do its own work.

    So, the question is: Is there a possibility to get tied session hash
more quickly ?

    The same thing happens with Tamplate object. Most part of my scripts
have to generate output over a Template Toolkit. So, may I declare the
Template object in the only place, and then use it into response handlers ?

    Well, I'm new to the mod_perl, but I have already read enough info
about numerous frameworks. I'm not shure yet: was it good idea to start
project with "clear" mod_perl and not using Catalyst for example. May be
some one has  some serious arguments whether to use frameworks or not ?
As I understand from documentation: frameworks give me everything: and
what I need, and what I don't need. But I want to use Template if I want
it, and don't use it else :)

Re: mod_perl comforts

Posted by Michael Peters <mp...@plusthree.com>.

Arshavir Grigorian wrote:
>     The same thing happens with Tamplate object. Most part of my scripts
> 
>     have to generate output over a Template Toolkit. So, may I declare the
>     Template object in the only place, and then use it into response
>     handlers ?
> 
> 
> You can create a Template object in your top handler, store it in  the
> request's pnotes ($r->pnotes), then retrieve it later in a different
> module when you are ready to use it.
> Hope that helps.

The same advice goes with any object you may or may not need during a request.
If you need it, check pnotes. If it's there use it, else create it and put it
back into pnotes.

But for the Template object, I would strongly suggest creating a package level
Template object in some base controller package or some other package. Create it
at compile time and then it will already exist for every request. This will also
improve caching since it can cache all of the templates in memory.

-- 
Michael Peters
Developer
Plus Three, LP


Re: mod_perl comforts

Posted by Arshavir Grigorian <gr...@gmail.com>.
    The same thing happens with Tamplate object. Most part of my scripts

> have to generate output over a Template Toolkit. So, may I declare the
> Template object in the only place, and then use it into response handlers
> ?


You can create a Template object in your top handler, store it in  the
request's pnotes ($r->pnotes), then retrieve it later in a different module
when you are ready to use it.
Hope that helps.

Arshavir

Re: mod_perl comforts

Posted by Jonathan Vanasco <mo...@2xlp.com>.
On Aug 17, 2006, at 5:50 PM, Frank Wiles wrote:
>   TT handles all of that for you. It can even cache them on disk  
> for you
>   so you don't have to bother recompiling them on startup if they
>   haven't changed.

Petal does that as well.

On Aug 17, 2006, at 5:54 PM, Joel Bernstein wrote:
> AFAIK TT doesn't precompile templates, it caches them on first use.
> Which means you'll take the hit in each forked child unless you
> explicitly precompile them. But perhaps I'm wrong about that?

That is probably right.  Petal behaves the same way.  You usually  
have to call them, either as a request or some other way.

I opted for 'some other way'-- i just find and loop through all my  
templates on startup as dummy documents.

Here's what i do in startup.pl:

	a- i patched petal to not die and still compile the template if not  
given the right hash info (its was put in the last version )
	b- i do a recursive loop on my template dir to find template files,  
then make blank dummy templates

originally i did this with File::Find.  Then i found out that  
File::Find is gigantic-- 2+ MB.  ModPerl::Utils's unload couldn't get  
rid of enough of it for me.  so i just did a dumb recursive loop.   
not as fancy, but its a fraction of the resources.

i included the code below, as i think its of marginal use to others.

FindMeOn::Templater is just my webapp specific template base class,  
that wraps Petal or whatever else engine, hold some defaults, and  
gives a standard interface.

===============================

my 	$restart_count= Apache2::ServerUtil::restart_count();
if ( $restart_count == 2 ) {
	my 	@template_files;
	sub _list_templates {
		my 	( $dir )= @_;
		my 	@sub_dirs;
		opendir DIR , $dir ;
		foreach my $file ( readdir ( DIR ) ) {
			next if substr ( $file , 0 , 1) eq '.';
			my 	$fullpath= $dir . $file;
			if ( -f $fullpath ) {
				push @template_files , $fullpath;
				next;
			}
			if ( -d $fullpath ) {
				&_list_templates( $fullpath . '/' );
				next;
			}
		}
		closedir DIR ;
	}

	print STDERR "\nPreCompiling Templates";
		# recurse all of the petal templates and pre-cache
		&_list_templates( $FindMeOn::Config::ServerLayout::TemplateDir );
		foreach my $file ( @template_files ) {
			my	$template= FindMeOn::Templater->new(
				engine=> 'Petal',
				file=> $file,
				base_dir=> '/',
				cache_only=> 1,
			);
			$template->process();
		}
	print STDERR "\n\tPreCompiling Templates: DONE";
}


Re: mod_perl comforts

Posted by Perrin Harkins <pe...@elem.com>.
On Fri, 2006-08-18 at 13:27 -0500, Frank Wiles wrote:
>   It doesn't precompile them, so you do take the first hit per template,
>   but not per apache child.  Because you can store a compiled version
>   of the template on disk where the other children can see it. 

To explain a bit further, there are two compilation steps.  The first
turns the template into a Perl sub.  This is the slowest part and it's
the part that is cached on disk.  The second part is when perl actually
compiles that generated code into memory.  That part has to happen once
in each child process, or in a startup.pl.  There is sample code for
doing it from startup.pl in the TT mail archive.

- Perrin


Re: mod_perl comforts

Posted by Frank Wiles <fr...@wiles.org>.
On Thu, 17 Aug 2006 22:54:56 +0100
Joel Bernstein <jo...@fysh.org> wrote:

> On Thu, Aug 17, 2006 at 04:50:46PM -0500, Frank Wiles wrote:
> > On Thu, 17 Aug 2006 16:54:08 -0400
> > Jonathan Vanasco <jo...@2xlp.com> wrote:
> > 
> > > 
> > > On Aug 17, 2006, at 1:09 PM, Perrin Harkins wrote:
> > > > You should create a single Template object, put it in a global,
> > > > and reuse it.
> > > 
> > > I'm not sure how template toolkit works, but in Petal I loop
> > > through all my templates and compile them into memory on
> > > startup.  Adds 3mb to my parent process, but saves 3mb from every
> > > child.    if TT works the same way, i'd suggest looking into
> > > making that happen.
> > 
> >   TT handles all of that for you. It can even cache them on disk
> > for you so you don't have to bother recompiling them on startup if
> > they haven't changed. 
> 
> AFAIK TT doesn't precompile templates, it caches them on first use.
> Which means you'll take the hit in each forked child unless you
> explicitly precompile them. But perhaps I'm wrong about that?

  It doesn't precompile them, so you do take the first hit per template,
  but not per apache child.  Because you can store a compiled version
  of the template on disk where the other children can see it. 

 ---------------------------------
   Frank Wiles <fr...@wiles.org>
   http://www.wiles.org
 ---------------------------------


Re: mod_perl comforts

Posted by Joel Bernstein <jo...@fysh.org>.
On Thu, Aug 17, 2006 at 04:50:46PM -0500, Frank Wiles wrote:
> On Thu, 17 Aug 2006 16:54:08 -0400
> Jonathan Vanasco <jo...@2xlp.com> wrote:
> 
> > 
> > On Aug 17, 2006, at 1:09 PM, Perrin Harkins wrote:
> > > You should create a single Template object, put it in a global, and
> > > reuse it.
> > 
> > I'm not sure how template toolkit works, but in Petal I loop through  
> > all my templates and compile them into memory on startup.  Adds 3mb  
> > to my parent process, but saves 3mb from every child.    if TT works  
> > the same way, i'd suggest looking into making that happen.
> 
>   TT handles all of that for you. It can even cache them on disk for you
>   so you don't have to bother recompiling them on startup if they
>   haven't changed. 

AFAIK TT doesn't precompile templates, it caches them on first use.
Which means you'll take the hit in each forked child unless you
explicitly precompile them. But perhaps I'm wrong about that?

/joel

Re: mod_perl comforts

Posted by Frank Wiles <fr...@wiles.org>.
On Thu, 17 Aug 2006 16:54:08 -0400
Jonathan Vanasco <jo...@2xlp.com> wrote:

> 
> On Aug 17, 2006, at 1:09 PM, Perrin Harkins wrote:
> > You should create a single Template object, put it in a global, and
> > reuse it.
> 
> I'm not sure how template toolkit works, but in Petal I loop through  
> all my templates and compile them into memory on startup.  Adds 3mb  
> to my parent process, but saves 3mb from every child.    if TT works  
> the same way, i'd suggest looking into making that happen.

  TT handles all of that for you. It can even cache them on disk for you
  so you don't have to bother recompiling them on startup if they
  haven't changed. 

 ---------------------------------
   Frank Wiles <fr...@wiles.org>
   http://www.wiles.org
 ---------------------------------


Re: mod_perl comforts

Posted by Jonathan Vanasco <jo...@2xlp.com>.
On Aug 17, 2006, at 1:09 PM, Perrin Harkins wrote:
> You should create a single Template object, put it in a global, and
> reuse it.

I'm not sure how template toolkit works, but in Petal I loop through  
all my templates and compile them into memory on startup.  Adds 3mb  
to my parent process, but saves 3mb from every child.    if TT works  
the same way, i'd suggest looking into making that happen.

Re: mod_perl comforts

Posted by Perrin Harkins <pe...@elem.com>.
On Thu, 2006-08-17 at 19:53 +0300, Vladimir S. Tikhonjuk wrote:
>     I used cookies and sessions to authenticate and authorizate user
> into my app ( Apache2::AuthCookie ). So, every handler ( or most of them
> ) have to get cookie, take a session id from it, makes session, and only
> then
> do its own work.
> 
>     So, the question is: Is there a possibility to get tied session hash
> more quickly ?

Is it slow?

If all you're doing is authentication, you don't need sessions at all.
You can just use a cookie with a token that you can check in it.  Look
at something like Apache::AuthCookie.

>     The same thing happens with Tamplate object. Most part of my scripts
> have to generate output over a Template Toolkit. So, may I declare the
> Template object in the only place, and then use it into response handlers ?

You should create a single Template object, put it in a global, and
reuse it.

> I'm not shure yet: was it good idea to start
> project with "clear" mod_perl and not using Catalyst for example. May be
> some one has  some serious arguments whether to use frameworks or not ?

You will learn more if you use mod_perl directly, and will have an
easier time using mod_perl features like subrequests and filters.  You
may get something working faster by using a framework.

- Perrin