memcache.examples.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 54 行
HTML
54 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Examples</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="memcache.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.memcache.html">Memcache Functions</a></div> <div class="up"><a href="book.memcache.html">Memcache</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div> <h1>Examples</h1> <div id="memcache.examples-overview" class="section"> <p class="para"> <div class="example"> <p><b>Example #1 memcache extension overview example</b></p> <div class="example-contents"><p> In this example, an object is being saved in the cache and then retrieved back. Object and other non-scalar types are serialized before saving, so it's impossible to store resources (i.e. connection identifiers and others) in the cache. </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br />$memcache </span><span style="color: #007700">= new </span><span style="color: #0000BB">Memcache</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$memcache</span><span style="color: #007700">-></span><span style="color: #0000BB">connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">, </span><span style="color: #0000BB">11211</span><span style="color: #007700">) or die (</span><span style="color: #DD0000">"Could not connect"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$version </span><span style="color: #007700">= </span><span style="color: #0000BB">$memcache</span><span style="color: #007700">-></span><span style="color: #0000BB">getVersion</span><span style="color: #007700">();<br />echo </span><span style="color: #DD0000">"Server's version: "</span><span style="color: #007700">.</span><span style="color: #0000BB">$version</span><span style="color: #007700">.</span><span style="color: #DD0000">"<br/>\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$tmp_object </span><span style="color: #007700">= new </span><span style="color: #0000BB">stdClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$tmp_object</span><span style="color: #007700">-></span><span style="color: #0000BB">str_attr </span><span style="color: #007700">= </span><span style="color: #DD0000">'test'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$tmp_object</span><span style="color: #007700">-></span><span style="color: #0000BB">int_attr </span><span style="color: #007700">= </span><span style="color: #0000BB">123</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$memcache</span><span style="color: #007700">-></span><span style="color: #0000BB">set</span><span style="color: #007700">(</span><span style="color: #DD0000">'key'</span><span style="color: #007700">, </span><span style="color: #0000BB">$tmp_object</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">, </span><span style="color: #0000BB">10</span><span style="color: #007700">) or die (</span><span style="color: #DD0000">"Failed to save data at the server"</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"Store data in the cache (data will expire in 10 seconds)<br/>\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$get_result </span><span style="color: #007700">= </span><span style="color: #0000BB">$memcache</span><span style="color: #007700">-></span><span style="color: #0000BB">get</span><span style="color: #007700">(</span><span style="color: #DD0000">'key'</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"Data from the cache:<br/>\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$get_result</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #2 Using memcache session handler</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br />$session_save_path </span><span style="color: #007700">= </span><span style="color: #DD0000">"tcp://$host:$port?persistent=1&weight=2&timeout=2&retry_interval=10, ,tcp://$host:$port "</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'session.save_handler'</span><span style="color: #007700">, </span><span style="color: #DD0000">'memcache'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'session.save_path'</span><span style="color: #007700">, </span><span style="color: #0000BB">$session_save_path</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="memcache.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.memcache.html">Memcache Functions</a></div> <div class="up"><a href="book.memcache.html">Memcache</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?