A couple things:
- What is the specific purpose of this Caching?
- Why are you using this aproach?
My personal opinion is that Dominus' memoize module is *perfect* for this purpose, and if you're able to install this module, there should be no problem with installing Memoize (even without cpan)... You would only have to code a time check into a module using Memoize, or Memoize itself.
However, if you can't use Memoize, please answer my questions! :) I ask them because one may propose a differant module, or a differant tact, depending on how you propose to do this. Of hand, if I didn't use a module, I would create subroutines, so that I wouldn't have to reload subroutines subrefs from a hash all the time:
sub CheckCache
{
my $self = shift;
my $sub = shift;
my $subref = shift;
my $time = shift;
if (not defined($subtimes{$sub}) or $subtimes{$sub}) < time - $time
+) { $self->newsub($subref,$sub,$time);} else {goto &$sub;}
}
sub newsub
{
my $self = shift;
ref $self or die("$self is not an object");
my $subref = shift;
my $sub = shift;
my $time = shift;
$subtimes{$sub} = $time;
*$sub = &$subref;
# no reason to play this game every time
goto &$sub;
}
And use it by doing
my $cache = new Cache::Cache;
$cache->CheckCache("key",\&sub,$time); # Object oriented
Or
Cache::CheckCache("key",\&sub,$time); # in non OO
Note: For non OO, $self should not be included, and lines with $self are probably not necessary. Personally, I would use OO for this task, because I think it would be easier to expand, however it is *your choice*.
Update To Response: This is the exact purpose of Memoize, however I have given you what is an alternative way of doing this in the above code. The module I created is much like your own, just a module: You can learn from other's modules as well as your own, this is one way to do this, although I prefer memoize.
On the subject of using 'our' in the module, I was under the impression that our went out of scope. use vars() allows for the hash to remain in the httpd process, so that it doesn't have to be reloaded, if the script is called by two differant people in the same time period. This means that Joe and Larry can use the cached results of the same routine (I only cached the routine, but you can probably figure out how to cache results...) Although the best option would be to cache results, and if the time period had passed, to just re-execute the ready-made subroutine.
In addition, the reason I was using subroutines is that retrieving a subref and calling it from a hash is *probably* more expensive than just executing a created subroutine. Just think of my post as an alternate aproach :)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.