<?xml version="1.0" encoding="windows-1252"?>
<node id="906015" title="learning memcached" created="2011-05-20 22:04:02" updated="2011-05-20 22:04:02">
<type id="115">
perlquestion</type>
<author id="231169">
punkish</author>
<data>
<field name="doctext">
I am trying to learn memcached, so I installed it on my laptop and fired it up. I also created a simple SQLite db 'mem.sqlite' like so
&lt;p&gt;
&lt;code&gt;
CREATE TABLE t (id INTEGER PRIMARY KEY, str TEXT)
&lt;/code&gt;
&lt;p&gt;
and filled the table with 20_000 random strings. Then I used the following simplistic script to benchmark.
&lt;p&gt;
&lt;code&gt;
use Cache::Memcached;
my $memd = new Cache::Memcached {'servers' =&gt; [ "localhost:11212" ]};

use DBI qw(:sql_types);
my $dbh = DBI-&gt;connect("dbi:SQLite:dbname=mem.sqlite");

use Benchmark qw(:all);
my $count = 40_000;

cmpthese($count, {
	'query_dbh' =&gt; 	sub {
		my $id = int(rand(20_000));
		my $sth = $dbh-&gt;prepare("SELECT str FROM t WHERE id = ?");
		$sth-&gt;execute($id);
		my ($str) = $sth-&gt;fetchrow_array;
		
		open F, "&gt;", "foo.txt" or die $!;
		print F "id: $id, str: $str\n";
		close F;
	},
	'query_mem' =&gt; 	sub {
		my $id = int(rand(20_000));
	
		open F, "&gt;", "foo.txt" or die $!;
		my $str = $memd-&gt;get($id);
		unless ($str) {
			my $sth = $dbh-&gt;prepare("SELECT str FROM t WHERE id = ?");
			$sth-&gt;execute($id);
			($str) = $sth-&gt;fetchrow_array;
			
			$memd-&gt;set($id, $str);
		}
		print F "id: $id, str: $str\n";
		close F;
	}
});

            Rate query_mem query_dbf
query_mem 2723/s        --      -29%
query_dbh 3846/s       41%        --
&lt;/code&gt;
&lt;p&gt;
I consistently get results such as above, while I expected the memcache to slowly fill up and speed up the queries way faster than only accessing the file based db. What am I doing wrong, or are my expectations wrong?
&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-231169"&gt;
&lt;br&gt;&lt;br&gt;&lt;i&gt;
when small people start casting long shadows, it is time to go to bed&lt;/i&gt;
&lt;/div&gt;&lt;/div&gt;</field>
</data>
</node>
