I’m unable to store a Perl object into Memcached. I try to store a complex Perl object into a Memcached server using Cache::Memcached. Memcached runs under defaults so there is 64 MB reserved I think. This limit shouldn’t reached anyway.
How to store complex Perl objects into Memcached?
The following code fail to store the object within the Memcached server. If you change the amount of iterations from 23_171 to for example 4 there is no problem.
#!/usr/bin/perl
############################################################################
# Script
use strict;
use warnings FATAL => 'all';
use Cache::Memcached;
my $memd = Cache::Memcached->new( servers => ['127.0.0.1:11211'] );
my $obj = MyModule->new;
$obj->add( MySubModule->new($_) ) for ( 1 .. 23_171 );
$memd->set( 'obj', $obj ) or die "failed";
$memd->disconnect_all;
############################################################################
# Collector-Module
package MyModule;
sub new { bless { jar => [] }, $_[0]; }
sub add { push @{ $_[0]->{jar} }, $_[1]; }
############################################################################
# Entry-Module
package MySubModule;
sub new { bless { id => $_[1], rnd => rand( $_[1] ) }, $_[0]; }
sub id { $_[0]->{id} = $_[0]->{id} // $_[1]; }
sub rnd { $_[0]->{rnd}; }
This object shouldn’t exeed 10 MB regardless which serializer or any is used, right?
Edit: I just have figured out that 20_000 iterations results into aprox. 15 MB assumed memory within Memcached.
The maximum size of a value you can store in memcached is usually 1 megabyte. Does your serialized object exceed that size?
Version 1.4.2 of memcached made this configurable: