On Sat, Dec 20, 2003 at 11:16:53AM -0500, Perrin Harkins wrote:
> Eric Sammer wrote:
> >I've had fantastic luck with MLDBM::Sync thus far, not that it's the all 
> >in one wonder tool.
> 
> I think it's a great tool, especially when you need easy access to 
> complex data structures, but I wanted to point the non-intuitive fact 
> that a local MySQL can be a great cache for a remote database, and is 
> actually faster than all of the other shared memory or file-based 
> solutions except IPC::MM and BerkeleyDB.  I will have a full article 
> about this soon.

Another approach to the caching problem is memcached:
http://www.danga.com/memcached/

it is a C daemon which you access over a socket. It is key=>value based
storage and if you use it to store strings it is _really_ fast. (You
can put arbitrary perl objects in there, but it uses Storable.pm which makes
it a little slower).  This is great to store pieces of HTML. 
Since it is a seperate process it stores every value
once and there is no copy for every mod_perl process. If the parserd deamon dies the 
perl
client will just ignore the caching and go on uncached. It will try every now and then 
to
see if the daemon got restarted.


Little bechmark (source attached):

Benchmark: timing 40000 iterations of cache_filecache, cache_memcached, 
cache_memorycache, notcached...
cache_filecache: 20 wallclock secs (15.25 usr +  2.65 sys = 17.90 CPU) @ 2234.64/s 
(n=40000)
cache_memcached:  5 wallclock secs ( 2.62 usr +  1.08 sys =  3.70 CPU) @ 10810.81/s 
(n=40000)
cache_memorycache:  7 wallclock secs ( 5.70 usr +  0.04 sys =  5.74 CPU) @ 6968.64/s 
(n=40000)
 notcached:  6 wallclock secs ( 5.56 usr +  0.01 sys =  5.57 CPU) @ 7181.33/s (n=40000)


The 'wallclock secs' are the ones to watch because usr/sys are incomplete
since mamcached uses a seperate process.
The cached routine is this very interesting (yeah, right) routine:

    foreach (sort 1..100) {
         $ret .= qq'&gt; $_<br>';
    }
    return $ret;

anything more fancy then this will only make the difference between memcached
and notcached bigger :)





Harmen
(Ok, this it not suited for complex structures which this thread is about but maybe 
someone can use it nevertheless.)



-- 
                               The Moon is Waning Crescent (10% of Full)
                        tty.nl - muziek-en-artiesten.2dehands.nl: 107625
# Cache::* becnhmark
use strict;
use warnings;
use Cache::FileCache;
use Cache::MemoryCache;
use Cache::Memcached;
use Benchmark;

my $filecache = new Cache::FileCache( { namespace => 'TweedTest',
                                    default_expires_in => 60 * 15,
                                    directory_umask => '000',
                                } );
my $memorycache = new Cache::MemoryCache( { namespace => 'TweedTest',
                                    default_expires_in => 60 * 15,
                                    directory_umask => '000',
                                } );
my $memcached = new Cache::Memcached {
    'servers' => ["127.0.0.1:11211"],
    'debug' => 0,
};


### The HTML we need to generate:
sub getmenu {
    my $ret;
    foreach (sort 1..100) {
         $ret .= qq'&gt; $_<br>';
    }
    return $ret;
}

sub getit_filecache {
    my $key = shift;
    my $data = $filecache->get($key);
    if (not defined $data) {
        $data = getmenu();
        $filecache->set($key, $data);
    }
    return $data;
}
sub getit_memorycache {
    my $key = shift;
    my $data = $memorycache->get($key);
    if (not defined $data) {
        $data = getmenu();
        $memorycache->set($key, $data);
    }
    return $data;
}
sub getit_memcached {
    my $key = shift;
    my $data = $memcached->get($key);
    if (not defined $data) {
        $data = getmenu();
        $memcached->set($key, $data);
    }
    return $data;
}


timethese (40_000, { 
                     cache_filecache    => "getit_filecache('test12')",
                     cache_memorycache  => "getit_memorycache('test12')",
                     cache_memcached    => "getit_memcached('test12')",
                     notcached          => "getmenu()",
                   }
);

-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html

Reply via email to