books
Free Books / Computers / Practical mod_perl /

previous page: 13.10. Caching and Pre-Caching
  
page up: Practical mod_perl | by Stas Bekman and Eric Cholet
  
next page: 13.12. Comparing Runtime Performance of Perl and C

13.11. Caching with Memoize




Description

This section is from the "Practical mod_perl" book, by Stas Bekman and Eric Cholet. Also available from Amazon: Practical mod_perl

If you have a subroutine with simpler logic, where a returned value is solely a function of an input, you can use the Memoize module, which does the caching automatically for you. The gist of its usage is giving the name of the function to be memoize( ) d:

use Memoize;
memoize('slow_function');
slow_function(arguments);

Remember that in our case we had two caches: one for the text versions of the calendars and the other for HTML components. The get_text_calendar( ) function is responsible for populating the text calendar's cache. It depends only on inputs, so we could rewrite it as:

use Memoize;
memoize('get_text_calendar');
sub get_text_calendar {
    my($year,$month) = @_;
    warn "$year,$month\n" if DEBUG;
    my $cal = Date::Calc::Calendar($year, $month);
    chomp $cal;
    return $cal;
}

We have added another debug warning to check that the cache is actually working. If you want to test it under mod_perl, set DEBUG to a true value, start the server in single-process mode (-X), and issue requests to the calendar registry script we just discussed.

You can also control the size of the cache and do other automatic cache manipulations with Memoize. See its manpage for more information.

The get_html_calendar( )subroutine cannot be memoize( ) d because the returned value depends on the relation between the requested date and the current date, in addition to the normal input/output relation.

 

Continue to:

  • prev: 13.10. Caching and Pre-Caching
  • Table of Contents
  • next: 13.12. Comparing Runtime Performance of Perl and C

Books by Stas Bekman:















TOP
previous page: 13.10. Caching and Pre-Caching
  
page up: Practical mod_perl | by Stas Bekman and Eric Cholet
  
next page: 13.12. Comparing Runtime Performance of Perl and C

Topics

  • Animals
  • Architecture
  • Arts
  • Business
  • Computers
  • Crafts
  • Fairy Tales
  • Finance
  • Flora and Plants
  • Cooking
  • Gardening
  • Health and Healing
  • History
  • Home Improvements
  • Languages
  • New Age
  • Novels
  • Real Estate
  • Reference
  • Religion
  • Science
  • Society
  • Sports
  • Travel
  • Outdoors


Search

My Books

Headaches Begone! A Systemic Approach To Healing Your Headaches
Don't Let Your Bike Seat Ruin Your Sex Life Book

Discover

  • Answers FAQ

[ Privacy Policy | Terms of Use | About Us | Search ]

© 2007-2021 StasoSphere.com