Can I use memcached as a Database

Marcus Bointon marcus at synchromedia.co.uk
Tue Sep 26 00:38:13 UTC 2006


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/




More information about the memcached mailing list