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; }
/** * Clear contents of cache backend * @return bool * @param $suffix string * @param $lifetime int **/ function reset($suffix = NULL, $lifetime = 0) { if (!$this->dsn) { return TRUE; } $regex = '/' . preg_quote($this->prefix . '.', '/') . '.+?' . preg_quote($suffix, '/') . '/'; $parts = explode('=', $this->dsn, 2); switch ($parts[0]) { case 'apc': $info = apc_cache_info('user'); foreach ($info['cache_list'] as $item) { if (preg_match($regex, $item['info']) && $item['mtime'] + $lifetime < time()) { apc_delete($item['info']); } } return TRUE; case 'memcache': foreach (memcache_get_extended_stats($this->ref, 'slabs') as $slabs) { foreach (array_filter(array_keys($slabs), 'is_numeric') as $id) { foreach (memcache_get_extended_stats($this->ref, 'cachedump', $id) as $data) { if (is_array($data)) { foreach ($data as $key => $val) { if (preg_match($regex, $key) && $val[1] + $lifetime < time()) { memcache_delete($this->ref, $key); } } } } } } return TRUE; case 'wincache': $info = wincache_ucache_info(); foreach ($info['ucache_entries'] as $item) { if (preg_match($regex, $item['key_name']) && $item['use_time'] + $lifetime < time()) { apc_delete($item['key_name']); } } return TRUE; case 'xcache': return TRUE; /* Not supported */ /* Not supported */ case 'folder': if ($glob = @glob($parts[1] . '*')) { foreach ($glob as $file) { if (preg_match($regex, basename($file)) && filemtime($file) + $lifetime < time()) { @unlink($file); } } } return TRUE; } return FALSE; }
/** * Clear contents of cache backend * @return bool * @param $suffix string * @param $lifetime int **/ function reset($suffix = NULL, $lifetime = 0) { if (!$this->dsn) { return TRUE; } $regex = '/' . preg_quote($this->prefix . '.', '/') . '.+?' . preg_quote($suffix, '/') . '/'; $parts = explode('=', $this->dsn, 2); switch ($parts[0]) { case 'apc': case 'apcu': $info = apc_cache_info('user'); if (!empty($info['cache_list'])) { $key = array_key_exists('info', $info['cache_list'][0]) ? 'info' : 'key'; $mtkey = array_key_exists('mtime', $info['cache_list'][0]) ? 'mtime' : 'modification_time'; foreach ($info['cache_list'] as $item) { if (preg_match($regex, $item[$key]) && $item[$mtkey] + $lifetime < time()) { apc_delete($item[$key]); } } } return TRUE; case 'redis': $fw = Base::instance(); $keys = $this->ref->keys($this->prefix . '.*' . $suffix); foreach ($keys as $key) { $val = $fw->unserialize($this->ref->get($key)); if ($val[1] + $lifetime < time()) { $this->ref->del($key); } } return TRUE; case 'memcache': foreach (memcache_get_extended_stats($this->ref, 'slabs') as $slabs) { foreach (array_filter(array_keys($slabs), 'is_numeric') as $id) { foreach (memcache_get_extended_stats($this->ref, 'cachedump', $id) as $data) { if (is_array($data)) { foreach ($data as $key => $val) { if (preg_match($regex, $key) && $val[1] + $lifetime < time()) { memcache_delete($this->ref, $key); } } } } } } return TRUE; case 'wincache': $info = wincache_ucache_info(); foreach ($info['ucache_entries'] as $item) { if (preg_match($regex, $item['key_name']) && $item['use_time'] + $lifetime < time()) { wincache_ucache_delete($item['key_name']); } } return TRUE; case 'xcache': return TRUE; /* Not supported */ /* Not supported */ case 'folder': if ($glob = @glob($parts[1] . '*')) { foreach ($glob as $file) { if (preg_match($regex, basename($file)) && filemtime($file) + $lifetime < time()) { @unlink($file); } } } return TRUE; } return FALSE; }
/** * 缓存服务器池中所有服务器统计信息 * * @param mixed $type 期望抓取的统计信息类型,可以使用的值有{reset, malloc, maps, cachedump, slabs, items, sizes} * @param mixed $slabid cachedump命令会完全占用服务器通常用于 比较严格的调 * @param mixed $limit 从服务端获取的实体条数 */ public function getExtendedStats($type = '', $slabid = 0, $limit = 100) { $re = memcache_get_extended_stats($this->memcache, $type, $slabid, $limit); return $re; }