downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

Source of: /manual/en/memcache.examples-overview.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once
dirname(__FILE__) ."/toc/memcache.examples.inc";
$setup = array (
 
'home' =>
  array (
   
0 => 'index.php',
   
1 => 'PHP Manual',
  ),
 
'head' =>
  array (
   
0 => 'UTF-8',
   
1 => 'en',
  ),
 
'this' =>
  array (
   
0 => 'memcache.examples-overview.php',
   
1 => 'memcache extension overview example',
  ),
 
'up' =>
  array (
   
0 => 'memcache.examples.php',
   
1 => 'Examples',
  ),
 
'prev' =>
  array (
   
0 => 'memcache.examples.php',
   
1 => 'Examples',
  ),
 
'next' =>
  array (
   
0 => 'ref.memcache.php',
   
1 => 'Memcache Functions',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<div id="memcache.examples-overview" class="section">
  <p class="para">
   </p><div class="example">
    <p><b>Example #1 memcache extension overview example</b></p>
    <div class="example-contents para"><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&#039;s impossible to store resources (i.e. connection
     identifiers and others) in the cache.
    </p></div>
    <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$memcache&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Memcache</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$memcache</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">11211</span><span style="color: #007700">)&nbsp;or&nbsp;die&nbsp;(</span><span style="color: #DD0000">"Could&nbsp;not&nbsp;connect"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$version&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$memcache</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getVersion</span><span style="color: #007700">();<br />echo&nbsp;</span><span style="color: #DD0000">"Server's&nbsp;version:&nbsp;"</span><span style="color: #007700">.</span><span style="color: #0000BB">$version</span><span style="color: #007700">.</span><span style="color: #DD0000">"&lt;br/&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$tmp_object&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">stdClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$tmp_object</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">str_attr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'test'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$tmp_object</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">int_attr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">123</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$memcache</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">set</span><span style="color: #007700">(</span><span style="color: #DD0000">'key'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$tmp_object</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">)&nbsp;or&nbsp;die&nbsp;(</span><span style="color: #DD0000">"Failed&nbsp;to&nbsp;save&nbsp;data&nbsp;at&nbsp;the&nbsp;server"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Store&nbsp;data&nbsp;in&nbsp;the&nbsp;cache&nbsp;(data&nbsp;will&nbsp;expire&nbsp;in&nbsp;10&nbsp;seconds)&lt;br/&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$get_result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$memcache</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">get</span><span style="color: #007700">(</span><span style="color: #DD0000">'key'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Data&nbsp;from&nbsp;the&nbsp;cache:&lt;br/&gt;\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">?&gt;</span>
</span>
</code></div>
    </div>

   </div><p>
  </p>
  <p class="para">
   </p><div class="example">
    <p><b>Example #2 Using memcache session handler</b></p>
    <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$session_save_path&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"tcp://</span><span style="color: #0000BB">$host</span><span style="color: #DD0000">:</span><span style="color: #0000BB">$port</span><span style="color: #DD0000">?persistent=1&amp;weight=2&amp;timeout=2&amp;retry_interval=10,&nbsp;&nbsp;,tcp://</span><span style="color: #0000BB">$host</span><span style="color: #DD0000">:</span><span style="color: #0000BB">$port</span><span style="color: #DD0000">&nbsp;&nbsp;"</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">,&nbsp;</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">,&nbsp;</span><span style="color: #0000BB">$session_save_path</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div><p>
  </p>
 </div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites