can memcached store Perl hashes? (I get an error)

484 views
Skip to first unread message

Bennett Haselton

unread,
Jul 2, 2009, 5:51:48 AM7/2/09
to memc...@googlegroups.com
This question:
http://code.google.com/p/memcached/wiki/FAQ#Can_I_access_the_same_data_in_memcached_with_different_client_li
says:
"The Perl Cache::Memcached will use Storable to serialize complex
structures (like hash references, objects, etc)."

I take this to mean that storing hashes in Perl is supported; however
when I tried the following code to store and retrieve a hash in memcached:

>>>
#!/usr/bin/perl

use Cache::Memcached;

# Configure the memcached server

my $cache = new Cache::Memcached {
'servers' => [
'localhost:11211',
],
};

my %testhash;
$testhash{'a'} = 'b';
$testhash{'c'} = 'q';

$cache->set('abc',%testhash);

my %returnedhash = $cache->get('abc');
print $returnedhash{'a'};
>>>

I get the following error:
Argument "q" isn't numeric in int at
/usr/lib/perl5/vendor_perl/5.8.8/Cache/Memcached.pm line 477.

('q' appears in the line "$testhash{'c'} = 'q';". If I replace that
'q' with some other letter, then the error message complains about
whatever letter I put there instead.)

So does this mean that (a) storing Perl hashes in memcached is not
supported after all; (b) it is officially supported, and this is a
bug, or (c) I am doing it wrong?

-Bennett

Tokuhiro Matsuno

unread,
Jul 2, 2009, 6:12:57 AM7/2/09
to memc...@googlegroups.com
hi bennett

do not use hash, use hashref instead.

>>>
#!/usr/bin/perl

use Cache::Memcached;

# Configure the memcached server

my $cache = new Cache::Memcached {
'servers' => [
'localhost:11211',
],
};

my %testhash;
$testhash{'a'} = 'b';
$testhash{'c'} = 'q';

$cache->set('abc', \%testhash); # make hashref

my %returnedhash = %{$cache->get('abc')}; # dereference
print $returnedhash{'a'}, "\n";

Reply all
Reply to author
Forward
0 new messages