array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'ja', ), 'this' => array ( 0 => 'memcache.examples-overview.php', 1 => '基本的な使用法', ), 'up' => array ( 0 => 'memcache.examples.php', 1 => '例', ), 'prev' => array ( 0 => 'memcache.examples.php', 1 => '例', ), 'next' => array ( 0 => 'class.memcache.php', 1 => 'Memcache', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'ja', 'path' => 'reference/memcache/examples.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
例1 memcache 拡張モジュールの概要
この例では、オブジェクトをキャッシュに保存した後に、改めて取得しなおします。 オブジェクトやその他の非スカラー型のデータは、保存される前に シリアライズされます。そのため (接続 ID などの) リソース型を 保存することはできません。
<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("接続できませんでした");
$version = $memcache->getVersion();
echo "サーバーのバージョン: ".$version."<br/>\n";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 10) or die ("データをサーバーに保存できませんでした");
echo "データをキャッシュに保存します (データの有効期限は 10 秒です)<br/>\n";
$get_result = $memcache->get('key');
echo "キャッシュから取得したデータ:<br/>\n";
var_dump($get_result);
?>
例2 memcache セッションハンドラの使用例
<?php
$session_save_path = "tcp://$host:$port?persistent=1&weight=2&timeout=2&retry_interval=10, ,tcp://$host:$port ";
ini_set('session.save_handler', 'memcache');
ini_set('session.save_path', $session_save_path);
?>