Structure of the memcached KEY

Cahill, Earl ecahill at
Mon Nov 6 18:25:14 UTC 2006

Well, to start we use ?s as sql placeholders, then we get a key based on
what you pass in for the ?s.  Your key could then look something like

$key = md5($sql) . '.' . md5(join(',', @execute_args))

And yeah, I don't buy that the hashing (or extra hashing) is going to be
any sort of overhead hit.

Or, if you know that the @execute_args are all numbers, you could avoid
the extra md5 and do

$key = md5($sql) . '.' . join(',', @execute_args)


-----Original Message-----
From: memcached-bounces at
[mailto:memcached-bounces at] On Behalf Of Jed Reynolds
Sent: Friday, November 03, 2006 9:47 AM
To: memcached at
Subject: Re: Structure of the memcached KEY

Brian Moon wrote:
> Steven Grimm wrote:
>> If you use a good hash function like SHA-1, false collisions are
>> basically a nonissue. You're more likely to be killed by a falling
>> airplane while holding a winning lottery ticket than to ever see an
>> SHA-1 collision between valid SQL queries.
> I still recommend more than just one SHA-1 or md5 hash.  Maybe both
> concated.  I have had collisions with md5 and sha1.  Sure, its 3 out
> of a couple of million, but it happens.
I'm using a hash for my sql queries like this:
$k = "sql://$dbname/".md5( $sql )."/".md5( base64_encode( $sql ));


More information about the memcached mailing list