Skip to content

Instantly share code, notes, and snippets.

@k-holy
Created January 13, 2012 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k-holy/1604492 to your computer and use it in GitHub Desktop.
Save k-holy/1604492 to your computer and use it in GitHub Desktop.
PHP Memcacheのテスト
<?php
session_start();
// コネクションプールにサーバ追加。必要な時に自動接続、スクリプト終了時に自動切断される。
$memcache = new Memcache();
$memcache->addServer('localhost', 11211);
$keys = range(0, 10000);
$items = array_combine($keys,
array_map(function($i) {
return sha1($i);
}, $keys)
);
$start = microtime(true);
foreach ($items as $key => $value) {
// 圧縮して格納、有効期限は1時間
$memcache->add($key, $value, MEMCACHE_COMPRESSED, 36000);
}
$end = microtime(true) - $start;
$_SESSION['items'] = $items;
?>
<pre>
Took <?php echo sprintf('%f', $end) ?> seconds to add.
</pre>
<p><a href="memcache_get.php">memcache_get.php</a></p>
<?php
session_start();
// コネクションプールにサーバ追加。必要な時に自動接続、スクリプト終了時に自動切断される。
$memcache = new Memcache();
$memcache->addServer('localhost', 11211);
$keys = range(0, 10000);
$start = microtime(true);
$results = $memcache->get($keys);
$end = microtime(true) - $start;
$ok = (isset($_SESSION['items']) && $_SESSION['items'] === $results);
?>
<pre>
Took <?php echo sprintf('%f', $end) ?> seconds to get.
OK? <?php var_dump($ok) ?>
</pre>
<p><a href="memcache_add.php">memcache_add.php</a></p>
<?php
$stats = $memcache->getStats();
$stat_keys = array(
'pid' => 'Process id of this server process ',
'uptime' => 'Number of seconds this server has been running ',
'time' => 'Current UNIX time according to the server ',
'version' => 'Version string of this server ',
'rusage_user' => 'Accumulated user time for this process ',
'rusage_system' => 'Accumulated system time for this process ',
'curr_items' => 'Current number of items stored by the server ',
'total_items' => 'Total number of items stored by this server ever since it started ',
'bytes' => 'Current number of bytes used by this server to store items ',
'curr_connections' => 'Number of open connections ',
'total_connections' => 'Total number of connections opened since the server started running ',
'connection_structures' => 'Number of connection structures allocated by the server ',
'cmd_get' => 'Cumulative number of retrieval requests ',
'cmd_set' => 'Cumulative number of storage requests ',
'get_hits' => 'Number of keys that have been requested and found present ',
'get_misses' => 'Number of items that have been requested and not found ',
'bytes_read' => 'Total number of bytes read by this server from network ',
'bytes_written' => 'Total number of bytes sent by this server to network ',
'limit_maxbytes' => 'Number of bytes this server is allowed to use for storage.',
);
?>
<table>
<?php foreach ($stat_keys as $key => $name) : ?>
<tr>
<th><?php echo $key ?></th>
<th><?php echo $name ?></th>
<td><?php echo (isset($stats[$key])) ? $stats[$key] : '' ?></td>
</tr>
<?php endforeach ?>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment