I think a more important question is... what do you hope to gain by caching
sql queries?   mysql and postgresql already have query caches, which work
about as good as using memcached as a query cache.  However, people dont
like it because it clears a table cache whenever the table is modified.

How are you handling removing cache entries when data is changed?  If the
data doesn't change, then what benefit is it over mysql built in cache.  If
the data does change, how are you removing the queries that are invalidated
by that change?

memcached is not a query cache.  It is not designed for that.   There are
other issues as well with using memcached as a query cache.  It may work for
you... but... if you are just starting out with memcached, I'd rather you
not think of it in terms of a query cache, you will end up invalidating its
more useful capabilities, and quite possibly end up just painting yourself
into a corner.

Think of memcached more of an object cache, and you might come a bit closer
to using its more beneficial capabilities.




2009/4/16 Henrik Schröder <[email protected]>

> Whatever is in $sql will always be unique with no risk of collisions. But
> if you hash $sql and use that as a key, you risk getting collisions, which
> means that when you look up the cached value for one query, you will get the
> results of some other query. This is potentially fatal for your application.
>
> Memcached keys have limitations, they can't contain space, and they can't
> be longer than 250 characters, so if you want to store the results of
> queries and key by query, you have to do some transformation of the key, but
> using md5 on everything is just lazy and not well-thought out. A smarter
> transformation would be to replace all " " with "-", and for keys that are
> still longer than 250 characters, change it to the first 240 chars + md5 of
> the remaining chars. You still have a small possibility of collisions, but
> you avoid a lot of unnecessary hashing, and since you probably have very few
> queries that are that long, you've reduced the collisions possibility. Oh,
> and your keys are still readable when you debug which makes it a lot easier
> to see what exactly gets stored and fetched.
>
>
> /Henrik
>
>
> On Thu, Apr 16, 2009 at 05:28, Paras <[email protected]> wrote:
>
>>
>> I found a sample code online which showed me how I can use memcached
>> with php. Here is the code.
>>
>> //write query
>> $sql = "select * from pages where page_id=1";
>>
>> //create an index key for memcache
>> $key = md5('query'.$sql);
>>
>> Can anyone tell me what the line after the second comment means ?
>>
>>
>>
>> Thanks,
>> Paras
>
>
>


-- 
"Be excellent to each other"

Reply via email to