The dated "perl-benchmark-cache-with-expires-and-max-size" article no longer exists on the web. Fortunately, Celogeek's GitHub repository exists, containing the article scripts. I made two test scripts locally, at the time, when developing MCE::Shared::Cache. What I recall from the article is the author captured memory consumption. Thus, the priority for me was ensuring low memory consumption first, then performance.
test-cache-mce.pl
$ diff test-cache-lru.pl test-cache-mce.pl
1a2,3
> # based on test-cache-lru.pl
> # https://github.com/celogeek/perl-test-caching/tree/master
8c10
< use Cache::LRU;
---
> use MCE::Shared::Cache;
17c19
< my $c = Cache::LRU->new(size => 500000);
---
> my $c = MCE::Shared::Cache->new(max_keys => 500000);
test-cache-mce-with-expires.pl
$ diff test-cache-lru-with-expires.pl test-cache-mce-with-expires.pl
1a2,3
> # based on test-cache-lru-with-expires.pl
> # https://github.com/celogeek/perl-test-caching/tree/master
8c10
< use Cache::LRU::WithExpires;
---
> use MCE::Shared::Cache;
17c19
< my $c = Cache::LRU::WithExpires->new(size => 500000);
---
> my $c = MCE::Shared::Cache->new(max_keys => 500000);
Non-shared results:
$ perl test-cache-lru.pl
Mapping
Starting
Write: 629454.032475913
Read : 1121841.10542643
Found: 500000
Mem : 436666368
$ perl test-cache-mce.pl
Mapping
Starting
Write: 662199.102659472
Read : 1054666.70019362
Found: 500000
Mem : 254976000
Non-shared results with expires:
Perl's dualvar capability is the reason why MCE::Shared::Cache is able to offer max_age capability without increasing memory consumption or greatly impacting performance.
$ perl test-cache-lru-with-expires.pl
Mapping
Starting
Write: 478838.905524077
Read : 726818.987997266
Found: 500000
Mem : 525479936
$ perl test-cache-mce-with-expires.pl
Mapping
Starting
Write: 605342.765771411
Read : 994375.492182959
Found: 500000
Mem : 254980096
Shared cache with expiration:
The MCE::Shared::Cache documentation provides a parallel demonstration, using a shared cache. Here, I modified the code to match the Redis variant (i.e. set key-value pair with expiration).
$ diff demo-from-doc.pl test-cache-parallel-mce.pl
17c17
< my $c = MCE::Shared->cache( max_keys => 500_000 );
---
> my $c = MCE::Shared->cache();
29c29
< for ( @{ $chunk_ref } ) { $c->set($_, {md5 => $_}) }
---
> for ( @{ $chunk_ref } ) { $c->set($_, {md5 => $_}, 600)
+}
Finally, the diff output for the Redis example.
$ diff test-cache-parallel-mce.pl test-cache-parallel-redis.pl
10a11,12
> use Redis;
> use Sereal qw/encode_sereal decode_sereal/;
17c19
< my $c = MCE::Shared->cache();
---
> my $c = Redis->new;
29c31
< for ( @{ $chunk_ref } ) { $c->set($_, {md5 => $_}, 600)
+}
---
> for ( @{ $chunk_ref } ) { $c->setex($_, 600, encode_sere
+al({md5 => $_})) }
33c35,39
< for ( @{ $chunk_ref } ) { $f++ if ref $c->get($_) eq 'HA
+SH' }
---
> for ( @{ $chunk_ref } ) {
> my $srl = $c->get($_);
> $srl = decode_sereal($srl) if defined $srl;
> $f++ if ref $srl eq 'HASH';
> }
Results:
$ perl test-cache-parallel-mce.pl
Mapping
Starting
Write: 228731.311
Read : 143733.302
Found: 600000
$ perl test-cache-parallel-redis.pl
Mapping
Starting
Write: 159626.727
Read : 164603.402
Found: 600000
$ perl test-redis-tcp.pl
Mapping
Starting
Write: 57403.6057590594
Read : 60989.7109982115
Found: 600000
Mem : 270336