Essentially, yes.  You need to do something like:

$res = mysql_query($sql);
$data = array();
while ($res && $row = mysql_fetch_assoc($res)) {
  $data[] = $row;
}
$memcache->set('whatever_your_key_is', $data);

Getting it is then a simple:

$data = $memcache->get('whatever_your_key_is');

If you happen to be requiring searchable results on your data, then this
is not a good approach - it is fine for block queries that you are always
going to use, but if you then fetch by a key, you really get no benefit.

One approach that does work is to store by a key so that you can either
query for a batch or a particular key.

E.g. if you have data that is keyed by email and you want to pre-fetch
a heap of emails, you can do something like:

$res = mysql_query("SELECT * from mailing_list");
while ($res && $row = mysql_fetch_assoc($res)) {
   $memcache->set('email::' . $row['email'], $row);
}

Then if you are later on querying by email it then makes it trivial:

$user =  $memcache->get('email::'.$email);

I probably shouldn't self-promote, but if you happen to be in Santa Clara
this week I will be running a session on how to get started with Memcached
and how we did this sort of thing for MySQL.com at the MySQL Users Conference
and Expo.

Adam (now severely jet lagged, so forgive any typos).


Paras wrote:
> Thats intresting...., Are you saying that I have to cache (1, Jonny
> Bravo, Paris, 1) ?
> 
> If so, how do I get the result from the cache ?
> 
> Thanks,
> Paras.
> 
> On Apr 17, 12:39 am, Adam Donnison <[email protected]> wrote:
>> Yeah, sorry, too late in the afternoon for me.  What I was thinking
>> (instead of what I was writing) was that you would need to cache the
>> data, not the resource.  In this sort of situation we tend to fetch
>> the results into an array and store that in memcached.
>>
>> Adam - who obviously is in dire need of caffeine.
>>
>>
>>
>> Brian Moon wrote:
>>
>>> Actually, pecl/memcache does the serialization for you.  Another nice
>>> thing it does.
>>> The problem here is that you can't cache a mysql resource from
>>> mysql_query.  Its not a result set of data.  mysql_query returns a
>>> resource to some in memory data that is just not there on subsequent pages.
>>> Brian.
>>> --------
>>> http://brian.moonspot.net/
>>> On 4/16/09 11:22 PM, Adam Donnison wrote:
>>>> If you are storing a PHP object you should serialize it,
>>>> and unserialize it on the way out.  Otherwise PHP cannot
>>>> understand what the object is.
>>>> $memcache->set($key, serialize($result));
>>>> $result = unserialize($memcache->get($key));
>>>> Adam
>>>> Paras wrote:
>>>>> Sorry I forgot to mention, result is of type mysql_result.
>>>>> Thanks
>>>>> Paras
>>>>> On Apr 16, 11:35 pm, Paras<[email protected]>  wrote:
>>>>>> Hello Guys,
>>>>>> I was trying to print a $result which was retrieved from the cache.
>>>>>> Can anyone tell me how I can print the results ?
>>>>>> eg:
>>>>>> $result = $memcache->get($key);
>>>>>> var_dump($result);
>>>>>> does not work. Any ideas ?
>>>>>> Thanks,
>>>>>> Paras
>> --
>> Adam Donnison, MySQL Web Developer
>> Sun Microsystems Australia Pty Ltd
>> Phone: x47031
>> Mobile: +61 4 1930 5414
>> Skype: ajdonnison
>> Email: [email protected]


-- 
Adam Donnison, MySQL Web Developer
Sun Microsystems Australia Pty Ltd
Phone: x47031
Mobile: +61 4 1930 5414
Skype: ajdonnison
Email: [email protected]

Reply via email to