Beispiel #1
0
 public function getUsage()
 {
     if (!function_exists('memcache_get_extended_stats')) {
         return false;
     }
     if ($this->usage === null) {
         $servers = DebugBar::getConfig('memcached');
         $server = array_shift($servers);
         $memcache = memcache_connect($server['host'], $server['port']);
         if ($memcache === false) {
             return false;
         }
         foreach ($servers as $server) {
             memcache_add_server($memcache, $server['host'], $server['port']);
         }
         $stats = memcache_get_extended_stats($memcache);
         $memory_used = 0;
         $memory_total = 0;
         foreach ($stats as $host => $usage) {
             $memory_used += $usage['bytes'];
             $memory_total += $usage['limit_maxbytes'];
             $this->usage[$host] = array('Version' => $usage['version'], 'Uptime' => $this->formatSeconds($usage['uptime']), 'Total memory' => $this->formatBytes($usage['limit_maxbytes']), 'Used memory' => $this->formatBytes($usage['bytes']), 'Available memory' => $this->formatBytes($usage['limit_maxbytes'] - $usage['bytes']), 'Threads / second' => $this->formatNum($usage['total_connections'] / $usage['uptime']), 'Current threads' => $usage['curr_connections'], 'Queries / second' => $this->formatNum($usage['cmd_get'] / $usage['uptime']), 'Hits' => $usage['get_hits'] . ' (' . $this->formatNum($usage['get_hits'] * 100 / $usage['cmd_get']) . '%)', 'Misses' => $usage['get_misses'] . ' (' . $this->formatNum($usage['get_misses'] * 100 / $usage['cmd_get']) . '%)', 'Inserts' => $usage['cmd_set'], 'Deletes' => $usage['delete_hits'], 'Items' => $usage['curr_items']);
         }
         $this->percent_used = $this->formatNum($memory_used * 100 / $memory_total);
     }
     return $this->usage;
 }
 /**
  * Connect to memcache server
  *
  * @access	private
  * @param	array 		Connection information
  * @return	boolean		Initiation successful
  */
 private function _connect($server_info = array())
 {
     if (!count($server_info)) {
         $this->crashed = true;
         return false;
     }
     if (!isset($server_info['memcache_server_1']) or !isset($server_info['memcache_port_1'])) {
         $this->crashed = true;
         return false;
     }
     $this->link = memcache_connect($server_info['memcache_server_1'], $server_info['memcache_port_1']);
     if (!$this->link) {
         $this->crashed = true;
         return false;
     }
     if (isset($server_info['memcache_server_2']) and isset($server_info['memcache_port_2'])) {
         memcache_add_server($this->link, $server_info['memcache_server_2'], $server_info['memcache_port_2']);
     }
     if (isset($server_info['memcache_server_3']) and isset($server_info['memcache_port_3'])) {
         memcache_add_server($this->link, $server_info['memcache_server_3'], $server_info['memcache_port_3']);
     }
     if (function_exists('memcache_set_compress_threshold')) {
         memcache_set_compress_threshold($this->link, 20000, 0.2);
     }
     return true;
 }
Beispiel #3
0
 public function connect($settings = array())
 {
     $config = Config::get('Cache', 'driverSettings');
     $config = !empty($settings) ? $settings : $config['memcache'];
     $connect = @memcache_add_server($config['host'], $config['port'], $config['weight']);
     if (empty($connect)) {
         die(lang('Cache', 'unsupported', 'Memcache'));
     }
     return true;
 }
Beispiel #4
0
 public function connect(array $settings = NULL)
 {
     $config = \Config::get('IndividualStructures', 'cache')['driverSettings'];
     $config = !empty($settings) ? $settings : $config['memcache'];
     $connect = @memcache_add_server($config['host'], $config['port'], $config['weight']);
     if (empty($connect)) {
         die(getErrorMessage('IndividualStructures', 'cache:unsupported', 'Memcache'));
     }
     return true;
 }
Beispiel #5
0
/**
 * @return mixed соединение с пулом кешей хранения сессий пользователя
 */
function session_getconnection()
{
    $connection = null;
    $config = session_config();
    foreach ($config as $v) {
        if (is_null($connection)) {
            $connection = memcache_connect($v['host']);
        } else {
            memcache_add_server($connection, $v['host']);
        }
    }
    return $connection;
}
Beispiel #6
0
/**
 * @param array $pool конфигурация пула
 *
 * @return &array объект мемкеша с установленными серверами
 */
function &construct(array $pool)
{
    $init = reset($pool);
    $memcache = memcache_connect($init[MemcachedFactory\CONFIGURATION_HOST], $init[MemcachedFactory\CONFIGURATION_PORT]);
    if ($memcache === false) {
        trigger_error('Ошибка memcache_connect ' . $init[MemcachedFactory\CONFIGURATION_HOST] . ':' . $init[MemcachedFactory\CONFIGURATION_PORT], E_USER_ERROR);
    }
    foreach (array_slice($pool, 1) as $server) {
        memcache_add_server($memcache, $server[MemcachedFactory\CONFIGURATION_HOST], $server[MemcachedFactory\CONFIGURATION_PORT]);
    }
    $Memcached = [FIELD_POOL => $pool, FIELD_MEMCACHE_OBJECT => $memcache];
    return $Memcached;
}
Beispiel #7
0
 static function connect()
 {
     if (!CGlobal::$memcache_connect_id && !eb_memcache::$crashed) {
         if (!function_exists('memcache_connect')) {
             //dl("php_memcache.dll");
             //dl("php_xdebug.dll");
             eb_memcache::$crashed = 1;
             return FALSE;
         }
         eb_memcache::$identifier = MEMCACHE_ID;
         //eb_memcache::$identifier = 'chonmon.vn';
         if (!CGlobal::$memcache_server || !count(CGlobal::$memcache_server)) {
             eb_memcache::$crashed = 1;
             return FALSE;
         }
         if (DEBUG) {
             $rtime = microtime();
             $rtime = explode(" ", $rtime);
             $rtime = $rtime[1] + $rtime[0];
             $start_rb = $rtime;
         }
         for ($i = 0, $n = count(CGlobal::$memcache_server); $i < $n; $i++) {
             $server = CGlobal::$memcache_server[$i];
             if ($i < 1) {
                 CGlobal::$memcache_connect_id = memcache_connect($server['host'], $server['port']);
             } else {
                 memcache_add_server(CGlobal::$memcache_connect_id, $server['host'], $server['port']);
             }
             if (DEBUG) {
                 $mtime = microtime();
                 $mtime = explode(" ", $mtime);
                 $mtime = $mtime[1] + $mtime[0];
                 $end_rb = $mtime;
                 $load_time = round($end_rb - $start_rb, 5) . "s";
                 CGlobal::$conn_debug .= " <b>Connect to Memcache server : {$server['host']} : {$server['port']} </b> [in {$load_time}]<br>\n";
             }
         }
         if (!CGlobal::$memcache_connect_id) {
             eb_memcache::$crashed = 1;
             return FALSE;
         }
         if (function_exists('memcache_set_compress_threshold')) {
             memcache_set_compress_threshold(CGlobal::$memcache_connect_id, 20000, 0.2);
         }
         //memcache_debug( eb_memcache::$debug );
     }
     return CGlobal::$memcache_connect_id;
 }
Beispiel #8
0
 /**
  *	Load/auto-detect cache backend
  *	@return string
  *	@param $dsn bool|string
  **/
 function load($dsn)
 {
     $fw = Base::instance();
     if ($dsn = trim($dsn)) {
         if (preg_match('/^memcache=(.+)/', $dsn, $parts) && extension_loaded('memcache')) {
             foreach ($fw->split($parts[1]) as $server) {
                 $port = 11211;
                 $parts = explode(':', $server, 2);
                 if (count($parts) > 1) {
                     list($host, $port) = $parts;
                 } else {
                     $host = $parts[0];
                 }
                 if (empty($this->ref)) {
                     $this->ref = @memcache_connect($host, $port) ?: NULL;
                 } else {
                     memcache_add_server($this->ref, $host, $port);
                 }
             }
         }
         if (empty($this->ref) && !preg_match('/^folder\\h*=/', $dsn)) {
             $dsn = ($grep = preg_grep('/^(apc|wincache|xcache)/', array_map('strtolower', get_loaded_extensions()))) ? current($grep) : 'folder=' . $fw->get('TEMP') . 'cache/';
         }
         if (preg_match('/^folder\\h*=\\h*(.+)/', $dsn, $parts) && !is_dir($parts[1])) {
             mkdir($parts[1], Base::MODE, TRUE);
         }
     }
     $this->prefix = $fw->hash($fw->get('ROOT') . $fw->get('BASE'));
     return $this->dsn = $dsn;
 }
Beispiel #9
0
 /**
  *	Load/auto-detect cache backend
  *	@return string
  *	@param $dsn bool|string
  **/
 function load($dsn)
 {
     $fw = Base::instance();
     if ($dsn = trim($dsn)) {
         if (preg_match('/^redis=(.+)/', $dsn, $parts) && extension_loaded('redis')) {
             list($host, $port, $db) = explode(':', $parts[1]) + [1 => 6379, 2 => NULL];
             $this->ref = new Redis();
             if (!$this->ref->connect($host, $port, 2)) {
                 $this->ref = NULL;
             }
             if (isset($db)) {
                 $this->ref->select($db);
             }
         } elseif (preg_match('/^memcache=(.+)/', $dsn, $parts) && extension_loaded('memcache')) {
             foreach ($fw->split($parts[1]) as $server) {
                 list($host, $port) = explode(':', $server) + [1 => 11211];
                 if (empty($this->ref)) {
                     $this->ref = @memcache_connect($host, $port) ?: NULL;
                 } else {
                     memcache_add_server($this->ref, $host, $port);
                 }
             }
         }
         if (empty($this->ref) && !preg_match('/^folder\\h*=/', $dsn)) {
             $dsn = ($grep = preg_grep('/^(apc|wincache|xcache)/', array_map('strtolower', get_loaded_extensions()))) ? current($grep) : 'folder=' . $fw->get('TEMP') . 'cache/';
         }
         if (preg_match('/^folder\\h*=\\h*(.+)/', $dsn, $parts) && !is_dir($parts[1])) {
             mkdir($parts[1], Base::MODE, TRUE);
         }
     }
     $this->prefix = $fw->get('SEED');
     return $this->dsn = $dsn;
 }
Beispiel #10
0
 /**
 		Initialize cache backend
 			@return bool
 			@public
 	**/
 static function prep()
 {
     if (!self::$vars['CACHE']) {
         return TRUE;
     }
     if (is_bool(self::$vars['CACHE'])) {
         // Auto-detect backend
         self::detect();
     }
     if (preg_match('/^(apc)|(memcache)=(.+)|(xcache)|(folder)=(.+\\/)/i', self::$vars['CACHE'], $match)) {
         if (isset($match[5]) && $match[5]) {
             if (!is_dir($match[6])) {
                 self::mkdir($match[6]);
             }
             // File system
             self::$backend = array('type' => 'folder', 'id' => $match[6]);
         } else {
             $ext = strtolower($match[1] ?: ($match[2] ?: $match[4]));
             if (!extension_loaded($ext)) {
                 trigger_error(sprintf(self::TEXT_PHPExt, $ext));
                 return FALSE;
             }
             if (isset($match[2]) && $match[2]) {
                 // Open persistent MemCache connection(s)
                 $mcache = NULL;
                 foreach (self::split($match[3]) as $server) {
                     // Hostname:port
                     list($host, $port) = explode(':', $server);
                     if (is_null($port)) {
                         // Use default port
                         $port = 11211;
                     }
                     // Connect to each server
                     if (is_null($mcache)) {
                         $mcache = memcache_pconnect($host, $port);
                     } else {
                         memcache_add_server($mcache, $host, $port);
                     }
                 }
                 // MemCache
                 self::$backend = array('type' => $ext, 'id' => $mcache);
             } else {
                 // APC and XCache
                 self::$backend = array('type' => $ext);
             }
         }
         self::$buffer = NULL;
         return TRUE;
     }
     // Unknown back-end
     trigger_error(self::TEXT_Backend);
     return FALSE;
 }
Beispiel #11
0
 /**
 		Load and configure backend; Auto-detect if argument is FALSE
 			@return string|void
 			@param $dsn string|FALSE
 	**/
 static function load($dsn = FALSE)
 {
     if (is_bool($dsn)) {
         // Auto-detect backend
         $ext = array_map('strtolower', get_loaded_extensions());
         $grep = preg_grep('/^(apc|xcache|shmop)/', $ext);
         $dsn = $grep ? current($grep) : 'folder=cache/';
     }
     $parts = explode('=', $dsn);
     if (!preg_match('/apc|xcache|shmop|folder|memcache/', $parts[0])) {
         return;
     }
     self::$engine = array('type' => $parts[0], 'data' => NULL);
     self::$ref = NULL;
     if ($parts[0] == 'shmop') {
         $self = __CLASS__;
         self::$ref = self::mutex(function () use($self) {
             $ref = @shmop_open($ftok = ftok(__FILE__, 'C'), 'c', 0644, $self::bytes(ini_get('memory_limit')));
             if ($ref && !unserialize(trim(shmop_read($ref, 0, 0xffff)))) {
                 shmop_write($ref, serialize(array()) . chr(0), 0);
             }
             return $ref;
         }, self::$vars['TEMP'] . $_SERVER['SERVER_NAME']);
         if (!self::$ref) {
             return self::load('folder=cache/');
         }
     } elseif (isset($parts[1])) {
         if ($parts[0] == 'memcache') {
             if (extension_loaded('memcache')) {
                 foreach (self::split($parts[1]) as $server) {
                     $parts = explode(':', $server);
                     if (count($parts) < 2) {
                         $host = $parts[0];
                         $port = 11211;
                     } else {
                         list($host, $port) = $parts;
                     }
                     if (!self::$ref) {
                         self::$ref = @memcache_connect($host, $port);
                     } else {
                         memcache_add_server(self::$ref, $host, $port);
                     }
                 }
             } else {
                 return self::$engine = NULL;
             }
         } elseif ($parts[0] == 'folder') {
             if (!is_dir($parts[1]) && !@mkdir($parts[1], 0755, TRUE)) {
                 return self::$engine = NULL;
             }
             self::$ref = $parts[1];
         }
     }
     return $dsn;
 }
Beispiel #12
0
 public function addServer($host, $port, $weight, $timeout)
 {
     memcache_add_server($host, $port, false, $weight, $timeout, function ($host, $port) {
         //todo:发送紧急邮件
     });
 }
Beispiel #13
0
 /**
 		Initialize framework level-2 cache
 			@return boolean
 			@public
 	**/
 public static function prep()
 {
     if (preg_match('/^(apc)|(memcache)=(.+)|(xcache)|(folder)\\=(.+\\/)/i', F3::$global['CACHE'], $_match)) {
         if ($_match[5]) {
             if (!file_exists($_match[6])) {
                 if (!is_writable(dirname($_match[6])) && function_exists('posix_getpwuid')) {
                     $_uid = posix_getpwuid(posix_geteuid());
                     F3::$global['CONTEXT'] = array($_uid['name'], realpath(dirname($_match[6])));
                     trigger_error(F3::TEXT_Write);
                     return;
                 }
                 // Create the framework's cache folder
                 mkdir($_match[6], 0755);
             }
             // File system
             self::$backend = array('type' => 'folder', 'id' => $_match[6]);
         } else {
             $_ext = strtolower($_match[1] ?: ($_match[2] ?: $_match[4]));
             if (!extension_loaded($_ext)) {
                 F3::$global['CONTEXT'] = $_ext;
                 trigger_error(F3::TEXT_PHPExt);
                 return;
             }
             if ($_match[2]) {
                 // Open persistent MemCache connection(s)
                 // Multiple servers separated by semi-colon
                 $_pool = explode(';', $_match[3]);
                 $_mcache = NULL;
                 foreach ($_pool as $_server) {
                     // Hostname:port
                     list($_host, $_port) = explode(':', $_server);
                     if (is_null($_port)) {
                         // Use default port
                         $_port = 11211;
                     }
                     // Connect to each server
                     if (is_null($_mcache)) {
                         $_mcache = memcache_pconnect($_host, $_port);
                     } else {
                         memcache_add_server($_mcache, $_host, $_port);
                     }
                 }
                 // MemCache
                 self::$backend = array('type' => $_ext, 'id' => $_mcache);
             } else {
                 // APC and XCache
                 self::$backend = array('type' => $_ext);
             }
         }
         self::$l1cache = NULL;
         return TRUE;
     }
     // Unknown back-end
     trigger_error(self::TEXT_Backend);
     return FALSE;
 }
Beispiel #14
0
 /**
 		Initialize framework level-2 cache
 			@return boolean
 			@private
 	**/
 private static function prep()
 {
     if (!isset(F3::$global['CACHE'])) {
         // Extensions usable as cache back-ends
         $_exts = array_intersect(explode('|', 'apc|xcache'), get_loaded_extensions());
         foreach (array_keys($_exts, '') as $_null) {
             unset($_exts[$_null]);
         }
         $_exts = array_merge($_exts, array());
         F3::$global['CACHE'] = $_exts[0] ?: 'folder=' . F3::$global['BASE'] . 'cache/';
     }
     if (preg_match('/^(?:(folder)\\=(.+\\/)|(apc)|(memcache)=(.+))|(xcache)/i', F3::$global['CACHE'], $_match)) {
         if ($_match[1]) {
             if (!file_exists($_match[2])) {
                 if (!is_writable(dirname($_match[2])) && function_exists('posix_getpwuid')) {
                     $_uid = posix_getpwuid(posix_geteuid());
                     F3::$global['CONTEXT'] = array($_uid['name'], realpath(dirname($_match[2])));
                     trigger_error(F3::TEXT_Write);
                     return;
                 }
                 // Create the framework's cache folder
                 mkdir($_match[2], 0755);
             }
             // File system
             self::$l1cache = array('type' => 'folder', 'id' => $_match[2]);
         } else {
             $_ext = strtolower($_match[3] ?: ($_match[4] ?: $_match[6]));
             if (!extension_loaded($_ext)) {
                 F3::$global['CONTEXT'] = $_ext;
                 trigger_error(F3::TEXT_PHPExt);
                 return;
             }
             if ($_match[4]) {
                 // Open persistent MemCache connection(s)
                 // Multiple servers separated by semi-colon
                 $_pool = explode(';', $_match[5]);
                 $_mcache = NULL;
                 foreach ($_pool as $_server) {
                     // Hostname:port
                     list($_host, $_port) = explode(':', $_server);
                     if (is_null($_port)) {
                         // Use default port
                         $_port = 11211;
                     }
                     // Connect to each server
                     if (is_null($_mcache)) {
                         $_mcache = memcache_pconnect($_host, $_port);
                     } else {
                         memcache_add_server($_mcache, $_host, $_port);
                     }
                 }
                 // MemCache
                 self::$l1cache = array('type' => $_ext, 'id' => $_mcache);
             } else {
                 // APC and XCache
                 self::$l1cache = array('type' => $_ext);
             }
         }
         self::$l1cache['current'] = FALSE;
         return TRUE;
     }
     // Unknown back-end
     trigger_error(self::TEXT_Backend);
     return FALSE;
 }
Beispiel #15
0
$pid = intval($_GET['pid']);
$key = $_GET['key'];
$connMaster = mysqli_connect($config['db']['m_host'], $config['db']['m_user'], $config['db']['m_pass'], $config['db']['m_db'], $config['db']['m_port']);
$connSlave = mysqli_connect($config['db']['s_host'], $config['db']['s_user'], $config['db']['s_pass'], $config['db']['s_db'], $config['db']['s_port']);
if (!$connMaster || !$connSlave) {
    exit('0!');
}
mysqli_set_charset($connSlave, 'utf8');
mysqli_set_charset($connMaster, 'utf8');
$mcServers = explode(' ', $config['mc']);
foreach ($mcServers as $server) {
    list($host, $port) = explode(':', $server);
    if ($mc === null) {
        $mc = memcache_connect($host, $port);
    } else {
        memcache_add_server($mc, $host, $port);
    }
}
$projectKey = implode('|', array('project', $pid));
$project = memcache_get($mc, $projectKey);
if (!$project) {
    $key = mysqli_real_escape_string($connSlave, $key);
    $projectResult = mysqli_query($connSlave, "SELECT id, name, monitors FROM `project` WHERE `id`={$pid} AND `key`='{$key}'");
    $project = mysqli_fetch_array($projectResult, MYSQLI_ASSOC);
    if (!$project['id']) {
        exit('1!');
    }
    memcache_set($mc, $projectKey, $project, 0, 60);
}
$sys_mid = intval($_GET['sys_mid']);
$code = intval($_GET['code']);