Пример #1
0
 public static function connect()
 {
     if (self::$connected) {
         return true;
     }
     // Connect using a persistent connection with the id "MC"
     self::$link = new Memcached('MC');
     // Set options
     if (Memcached::HAVE_IGBINARY) {
         /* Use the igbinary serializer if its available, its much faster and more efficient */
         self::$link->setOption(Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY);
     }
     self::$link->setOption(Memcached::OPT_HASH, Memcached::HASH_MD5);
     self::$link->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_MODULA);
     self::$link->setOption(Memcached::OPT_NO_BLOCK, true);
     /* non-blocking I/O */
     self::$link->setOption(Memcached::OPT_CONNECT_TIMEOUT, 50);
     /* 50ms connect timeout */
     if (isset(config::$conf['memcache_prefix'])) {
         self::$link->setOption(Memcached::OPT_PREFIX_KEY, config::$conf['memcache_prefix']);
     }
     /* set the key prefix */
     $status = count(self::$link->getServerList());
     if (!$status) {
         if (!isset(config::$conf['memcache_servers']) || !self::set_servers(config::$conf['memcache_servers'])) {
             trigger_error('Unable to set Memcache server(s) in ' . __METHOD__, E_USER_WARNING);
             return false;
         }
         foreach (self::$servers as $server) {
             $status += (int) self::$link->addServer($server['ip'], $server['port'], $server['weight']);
         }
     }
     self::$connected = (bool) $status;
     return self::$connected;
 }