Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reduce number of redundant requests with mod_perl properly?

In a fairly big legacy project, I've refactored several hairy modules into Moose classes. Each of these modules requires database access to (lazy) fetch its attributes. Since those objects are used pretty heavily, I want to reduce the number of redundant requests, for example for unchanged data.

Now, how do I do that properly? I've got several alternatives:

  1. Implement caching in my Moose classes via a role to store them in memcached with expiration of 5-10 minutes (probably not too difficult, but tricky with lazy attributes) update: KiokuDB could probably help here, have to read up about attributes
  2. Migrate to DBIx::Class (needs to be done anyway) and implement caching on this level (DBIC will probably take most of the pain away just by itself)
  3. Somehow make my objects persist inside the mod_perl process (no clue how to do this :()

How would you do this and what do you consider a sane way? Is caching data preferred on object or the ORM level?

like image 309
Nikolai Prokoschenko Avatar asked Mar 08 '10 14:03

Nikolai Prokoschenko


1 Answers

The short answer to #3 is: Don't use 'my'. You might do something like:

 use vars qw($object);
 # OR post perl5.6:
 # our ($object); 

 # create your object if it doesn't already exist
 $object ||= create_object;

 # Maybe reload some attributes if they have expired.
 $object->check_expires;

Objects created like this inside your handler will only be shared inside each Apache child, which is fine if you are reloading the data every 5-10 minutes. Any modules and objects that are read-only should be loaded in a PerlPostConfigRequire script so that they will be shared across all children.

like image 174
stu42j Avatar answered Nov 15 '22 08:11

stu42j