Can I use memcached as a Database

Junaid N. Sahibzada sjunaidn at
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.


    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('','Yes'),
                        '3'=>array('', 'No'),
     $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



$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?


Marcus Bointon <marcus at> 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('','Yes'),
                    '3'=>array('', 'No'),

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 Bointon
Synchromedia Limited: Creators of
marcus at |

                          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  Check it out. 
-------------- next part --------------
An HTML attachment was scrubbed...

More information about the memcached mailing list