Can I use memcached as a Database

Junaid N. Sahibzada sjunaidn at yahoo.com
Tue Sep 26 01:33:59 UTC 2006


Hi Marcus,

Thank you very much for replying.

I am using your code and it seems its not working.

Here is the first script which is adding to the cache.

<?php 

    class clsMem extends Memcache
     {
        
            static private $m_objMem = NULL;
            
                static function getMem()
                 {
            
                        if (self::$m_objMem == NULL)
                
                            {
                                
                                self::$m_objMem = new Memcache;
                                self::$m_objMem->connect('localhost', 11211) or die ("The memcached server");
                                
                            }
                        return self::$m_objMem;
                  }
     }
     
     $URLarray = array( '1'=>array('yahoo.com','Yes'),
                        '2'=>array('hotmail.com','Yes'),
                        '3'=>array('cnn.com', 'No'),
                        '4'=>array('bbc.co.uk','Yes'),
                        '5'=>array('pctool.com','Yes'),);
                   
     $unit = 1;
                   
     foreach ($URLarray as $key => $value)
         {
            clsMem::getMem()->set($value[0], $value[1] == 'Yes', true, 600) or  die ("Could not write to the cache");
         }
?>


And here is the second script which is trying to access the cache

<?php

include_once("memtest.php");

$MyStringURL = clsMem::getMem()->get("URL") or die ("Couldn't find the requested URL item in the cache!");
echo $MyStringURL;
echo "\n";
$MyStringUnit = clsMem::getMem()->get("unit") or die ("Couldn't find the Unit item in the cache!");
echo $MyStringUnit;
echo "\n";

?>


And here is the output i get when i try to access the second script.

Array Couldn't find the Unit item in the cache!

Not only it doesnt update the variable $unit but it is also not returning the correct array items.

Can you please help?

Thanks


Marcus Bointon <marcus at synchromedia.co.uk> wrote: 
On 26 Sep 2006, at 01:09, Junaid N. Sahibzada wrote:

> Now if i have another item to add to the cache, how do i add it to  
> the cache?

Just call set with the new item.

> 2. I would basically want to have a database of URLS in my main  
> memory which i can quickly query, index, update, delete and modify.
>
> Is this possible with memcache?

Possible yes, sensible, probably not. Searching memcached is very  
limited - exact match on one key is all you get (AFAIK). Seems like  
you should add your items individually so that they can be found and  
retrieved individually too. Lose your integer key, make your URL the  
key, don't serialize (memcache will do it for you) and if you're  
really storing only 'yes' and 'no' for each URL, I'd suggest you  
change that to a Boolean value.

$URLarray = array( '1'=>array('yahoo.com','Yes'),
                    '2'=>array('hotmail.com','Yes'),
                    '3'=>array('cnn.com', 'No'),
                    '4'=>array('bbc.co.uk','Yes'),
                    '5'=>array('pctool.com','Yes'),);

foreach ($URLarray as $key => $value) {
clsMem::getMem()->set($value[0], $value[1] == 'Yes', true, 600) or  
die ("Could not write to the cache");
}

Now you can add more URLs very easily:

clsMem::getMem()->set($newurl, true, true, 600) or die ("Could not  
write to the cache");

Finding them is as simple as:

$value = clsMem::getMem()->get($url);

Marcus
-- 
Marcus Bointon
Synchromedia Limited: Creators of http://www.smartmessages.net/
marcus at synchromedia.co.uk | http://www.synchromedia.co.uk/





                          Junaid N. Sahibzada
  Cell # (+61) 404 998 494 
    Student MSc Internetworking, UTS, Australia
  Bachelor of Information Technology, NUST, Pakistan














 		
---------------------------------
Stay in the know. Pulse on the new Yahoo.com.  Check it out. 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.danga.com/pipermail/memcached/attachments/20060925/746bf3b4/attachment-0001.html


More information about the memcached mailing list