/** * 初始化缓存配置信息 * @param array $configs 缓存配置信息 */ public function __construct($configs) { if (!$configs) { if (APP_DEBUG) { E("必须传入缓存配置信息!"); } } $this->configs = $configs; $Mem = new \Memcache(); foreach ($this->configs['server'] as $value) { call_user_func_array(array($Mem, 'addServer'), $value); } if (!$Mem->getstats()) { if (APP_DEBUG) { E("Unable to connect the Memcache server!"); } } self::$Mem = $Mem; }
function mem_status() { global $config; $config = object_to_array($config); $mem = new Memcache(); $host = "127.0.0.1"; $port = isset($config['module']['memcached1.2.6']['port']) ? $config['module']['memcached1.2.6']['port'] : 11211; $mem->connect($host, $port); print "<table style='border: 1px solid #D9D9D9;'>"; print "<thead><th>Memcache Status</th></thead>"; foreach ($mem->getstats() as $key => $value) { print "<tr><td style='background: #E3E3F1;'>{$key}</td> <td>{$value}</td></tr>\n"; } print "</table>"; print "<p>"; $mem->set('test', 'This is a test value', 0, 60); print $mem->get('test'); print "</p>"; $mem->flush(); }
/** * 初始化缓存配置信息 * @param array $configs 缓存配置信息 */ public function __construct($configs) { if (!$configs) { if (APP_DEBUG) { E("cache configure args is needed!"); } } if (!extension_loaded("memcache")) { E("please install memcache extension."); } $this->configs = $configs; $Mem = new \Memcache(); foreach ($this->configs['server'] as $value) { call_user_func_array(array($Mem, 'addServer'), $value); } if (!$Mem->getstats()) { if (APP_DEBUG) { E("Unable to connect the Memcache server!"); } } self::$Mem = $Mem; }
<?php /** * Created by PhpStorm. * User: PSV * Date: 29.09.15 * Time: 19:29 */ $cache = new Memcache(); $cache->connect("localhost", 11211, 1); $cache->set("putin", "hamlo"); echo $cache->get("putin") . "<br>"; $cache->set("putin1", "durak"); echo "putin1" . $cache->get("putin1") . "<br>"; $a = 2; $a = $a++ + ++$a - $a--; echo $a; $cache->delete("putin1"); //file_get_contents() $res = $cache->get("putin1"); echo "putin1" . $res . "<br>"; //$cache->add("putin2","hamlokhflsdfsds", MEMCACHE_COMPRESSED, 10); echo $cache->get("putin2"); $cache->add("views", 1); $cache->increment("views"); echo $cache->get("views"); //$cache->delete("views"); echo "<pre>"; //$cache->flush(); var_dump($cache->getstats()); $cache->close();
function memCache2html() { $html = ''; if (!class_exists('Memcache')) { return ''; } $cache = new Memcache(); $cache->connect('127.0.0.1', 11211); if ($res = $cache->getstats('items') and !empty($res['items'])) { if (MEMCACHE_CRYPT) { return '<b>Memcache</b>: Для отображения таблицы, отключите шифрование кэша в sys/config.php <b>MEMCACHE_CRYPT</b>'; } $html .= '<b>Memcache</b>. В целях безопасности включите шифрование кэша в sys/config.php <b>MEMCACHE_CRYPT</b>. <style type="text/css"> table.sys th{background-color: #9999ff; padding:7px; text-align: center;} table.sys td{background-color: #ccccff; padding:7px; text-align: center;white-space:pre} table.sys td.left{text-align: left;} .red{color:red;} </style><table class="sys" style="width:100%;"> <tr><th style="width:50px;">№</th><th>Ключ</th><th>Значение</th><th>Время жизни</th><th><a href="' . H . '/panel/settings/memcache_table_off" title="Закрыть таблицу" class="red">[X]</a></th></tr>'; $i = 0; foreach ($res['items'] as $key => $val) { $items = $cache->getStats('cachedump', $key); foreach ($items as $key => $val) { $i++; $live_time = $val[1] - TIME; if ($live_time < 0) { $live_time = 'Вечная'; } else { $live_time .= ' сек.'; } $html .= '<tr><td>' . $i . '</td><td>' . $key . '</td> <td class="left">' . htmlspecialchars(print_r($cache->get($key), true)) . '</td> <td>' . $live_time . '</td><td><a class="red" title="Удалить" href="' . H . '/panel/settings/del_memcached/' . $key . '">[X]</a></td></tr>'; } } $html .= '</table>'; } return $html; }
/** * @see Temporary_cache_driver::get_info() */ public function get_info() { $info = $this->memcache->getstats(); return array('available' => $info['limit_maxbytes'] - $info['bytes'], 'max' => $info['limit_maxbytes'], 'occupied' => $info['bytes']); }
<?php /* * 统计各节点的平均命中率 */ require './13-01-config.php'; $mem = new Memcache(); $gets = 0; $hits = 0; foreach ($_memserv as $k => $v) { $mem->connect($v['host'], $v['port'], 2); $res = $mem->getstats(); $gets += $res['cmd_get']; $hits += $res['get_hits']; } $rate = 1; if ($gets > 0) { $rate = $hits / $gets; } echo $rate * 100, '%';