On Wed, 2008-03-26 at 10:28 -0700, Brian Aker wrote:
> We need code examples for using memcached in Ruby, Python, and PHP.
> 

Here is the PHP class that I use in the Chisimba PHP5 Framework:

class chisimbacache extends Memcache
{
        static private $objMem = NULL;
        static private $objServers = array();
        static private $objAPC = NULL;
        
        /**
         * Singleton method for memcache servers
         * 
         * The Servers array should contain arrays of servers (IP and Port)
         *
         * @param array $servers
         * @return memcahed instance
         */
        static function getMem()
        {
                $servers = self::getServers();
                if(!empty($servers))
                {
                        if(self::$objMem == NULL)
                        {
                                self::$objMem = new Memcache;
                                // connect to the memcache server(s)
                                foreach($servers as $cache)
                                {
                                        self::$objMem->addServer($cache['ip'], 
(int)$cache['port']);
                                }               
                        }
                }
                
                return self::$objMem;
        }
        
        public function getServers()
        {
                $filename = 'cache.config';
                if(!file_exists($filename))
                {
                        touch($filename);
                        chmod($filename, 0777);
                }
                $handle = fopen($filename, "r");
                while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $num = count($data);
                for ($c=0; $c < $num; $c++) {
                                        $cache[] = array('ip' => $data[0], 
'port' => $data[1]); 
                }
                }
                fclose($handle);
                if(empty($cache))
                {
                        $cache = array('ip' => 'localhost', 'port' => 11211);
                        $cacherec = array($cache);
                        $handle = fopen($filename, 'wb');
                        foreach($cacherec as $rec)
                        {
                                fputcsv($handle, $rec);
                        }
                        fclose($handle);
                }
                return $cache;
        }
}

Hope it helps a bit...

--Paul
-- 
------------------------------------------------------------.
| Chisimba PHP5 Framework - http://avoir.uwc.ac.za           |
:------------------------------------------------------------:

All Email originating from UWC is covered by disclaimer 
http://www.uwc.ac.za/portal/public/portal_services/disclaimer.htm 

Reply via email to