regarding what to cache???

John Kramlich john.kramlich at gigoit.org
Tue Sep 4 23:22:12 UTC 2007


You cannot directly store a MySQL result set.  I believe this is because 
it's actually a reference and when it's converted to a string you will 
simply be storing "MySQL Result Set" instead of the result of your query.

My solution to this problem has been to turn my MySQL results into a PHP 
array where each row in the resultset is an item in the array.  These 
items are arrays themselves with their keys being the column name.  So 
if your result set returned names and phone numbers the array would be 
something like this:

$MyResultSetArray = array();
$MyResultSetArray[] = array('first_name' => 'Bob', 'last_name' => 
'Johnson', 'phone' = '555-555-5555');

You then store the array in memcache.  If you write a class you can do 
all this transparently.  Something like:

MyClass->query(theSQL, addToCache)

and have it return the array mentioned above, regardless of whether it 
was pulled from the cache or generated on the fly.

This gives you the greatest flexibility with what you can do with your 
resultset.  However, you will have to devise a method to ensure your 
data is not stale.  My website, Gigoit, uses very consistent SQL queries 
that can easily be cached.  When new items are posted to our site we run 
code to remove any keys that could be stale.  If you describe your 
situation in more detail, I might be able to give you more information.

- John

http://www.Gigoit.org - Give and Get Free Items in your Area.  (Saving 
the planet is just a bonus)


Ajinkya Nahar wrote:
>
> Hi,
>
>  
>
> I am working on a PHP -- Mysql application.
>
> I wanted to ask that can we cache the result set returned by a 
> particular query into memcached?
>
> If yes, can we fetch the same result set from memcached and use it 
> without database connection?
>
>  
>
> E.g.
>
> $query = "SELECT * from ......";
>
>      $result = mysql_query($query);
>
>      $memcache_obj->set($key, $result);
>
>  
>
> Will this work??
>
>  
>
> Please let me know your comments.
>
>  
>
> Regards,
>
> Ajinkya
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.danga.com/pipermail/memcached/attachments/20070904/5cd5ee69/attachment.html


More information about the memcached mailing list