Heim >php教程 >php手册 >PHP5 像使用数组一样使用Memcache

PHP5 像使用数组一样使用Memcache

WBOY
WBOYOriginal
2016-06-06 19:58:041352Durchsuche

作者:张立冰 出处:http://www.libing.name/2008/03/13/php5-memcache-array.html 我们经常使用Memcache来做数据的缓存,比如大的分布式的应用的会话数据的保存等,每次使用Memcache进行数据的操作都必须依赖于Memcache提供的API,对象的创建,方法的调用等,虽然

作者:张立冰
出处:http://www.libing.name/2008/03/13/php5-memcache-array.html

我们经常使用Memcache来做数据的缓存,比如大的分布式的应用的会话数据的保存等,每次使用Memcache进行数据的操作都必须依赖于Memcache提供的API,对象的创建,方法的调用等,虽然就几个API,使用起来也挺方便的,但再没有像使用数组这么方便来使用你的Memcache了.

At PHPClasses:http://www.phpclasses.org/browse/package/4071.html

在PHP5里,可以方便的帮我们实现了(基于SPL).几小时的工作,可能有些小BUG,欢迎反馈.:)

 <span>/**<br> * mem.class.php -- Class used to access memcache server as array.<br> *<br> * Written by roast (roast_AT_php_DOT_net).<br> *<br> * Feel free to email me with questions/bugs.<br> * If you find this useful, would be great to hear from you.<br> *<br> * version 1.0 -<br> *<br> * Changes:<br> *  - First release.<br> *<br> * Example:<br> *<br> *    include("mem.class.php");<br> *<br> *    //global variable $_MEM<br> *<br> *    $_MEM['dd'] = "roast";  //memcache::set<br> *    unset($_MEM['dd']);     //memcache::delete<br> *<br> *    var_dump($_MEM['dd']);<br> *<br> * Notes:<br> *  - Protect keys of memcache: _host/_port/_compress/_expire.<br> *  - PHP version 5.0+<br> */</span>        <br> <br><span>class</span> _MEM implements ArrayAccess<br><span>{</span><br>    <span>private</span> <span>$_data</span> <span>=</span> <span>array</span><span>(</span><span>)</span><span>;</span>      <br> <br>    <span>private</span> <span>$_handler</span> <span>=</span> <span>false</span><span>;</span>     <br> <br>    <span>public</span>  <span>$_host</span><span>;</span>    <br> <br>    <span>public</span>  <span>$_port</span> <span>=</span> <span>11211</span><span>;</span>    <br> <br>    <span>public</span>  <span>$_compress</span> <span>=</span> <span>false</span><span>;</span>    <br> <br>    <span>public</span>  <span>$_expire</span> <span>=</span> <span>30</span><span>;</span>         <br> <br>    <span>public</span> <span>function</span> __construct<span>(</span><span>$host</span><span>,</span><span>$port</span> <span>=</span> <span>11211</span><span>)</span><br>    <span>{</span><br>        <span>$this</span><span>-&</span>gt<span>;</span>_host <span>=</span> <span>$host</span><span>;</span><br>        <span>$this</span><span>-&</span>gt<span>;</span>_port <span>=</span> <span>$port</span><span>;</span><br>        <span>$this</span><span>-&</span>gt<span>;</span>_handler <span>=</span> <span>new</span> Memcache<span>;</span>     <br> <br>        <span>if</span> <span>(</span><span>!</span><span>$this</span><span>-&</span>gt<span>;</span>_handler<span>-&</span>gt<span>;</span>connect<span>(</span><span>$host</span><span>,</span><span>$port</span><span>)</span><span>)</span><br>            <span>$this</span><span>-&</span>gt<span>;</span>_handler <span>=</span> <span>false</span><span>;</span><br>    <span>}</span>          <br> <br>    <span>public</span> <span>function</span> offsetExists<span>(</span><span>$offset</span><span>)</span> <span>{</span><span>}</span>       <br> <br>    <span>public</span> <span>function</span> offsetUnset<span>(</span><span>$offset</span><span>)</span><br>    <span>{</span><br>        <span>$this</span><span>-&</span>gt<span>;</span>_handler<span>-&</span>gt<span>;</span>delete<span>(</span><span>$offset</span><span>)</span><span>;</span><br>        <span>unset</span><span>(</span><span>$this</span><span>-&</span>gt<span>;</span>_data<span>[</span><span>$offset</span><span>]</span><span>)</span><span>;</span><br>    <span>}</span>      <br> <br>    <span>public</span> <span>function</span> offsetGet<span>(</span><span>$offset</span><span>)</span><br>    <span>{</span><br>        <span>if</span> <span>(</span><span>!</span><span>$this</span><span>-&</span>gt<span>;</span>_handler<span>)</span><br>            <span>return</span> <span>false</span><span>;</span><br>                    <span>echo</span> <span>'get'</span><span>;</span><br>        <span>if</span> <span>(</span><span>isset</span><span>(</span><span>$this</span><span>-&</span>gt<span>;</span>_data<span>[</span><span>$offset</span><span>]</span><span>)</span><span>)</span><br>            <span>return</span> <span>$this</span><span>-&</span>gt<span>;</span>_data<span>[</span><span>$offset</span><span>]</span><span>;</span><br>        <span>else</span><br>            <span>return</span> <span>$this</span><span>-&</span>gt<span>;</span>_handler<span>-&</span>gt<span>;</span>get<span>(</span><span>$offset</span><span>)</span><span>;</span><br>    <span>}</span>      <br> <br>    <span>public</span> <span>function</span> offsetSet<span>(</span><span>$offset</span><span>,</span> <span>$value</span><span>)</span><br>    <span>{</span><br>        <span>if</span> <span>(</span><span>!</span><span>$this</span><span>-&</span>gt<span>;</span>_handler<span>)</span><br>            <span>return</span> <span>false</span><span>;</span>      <br> <br>        <span>switch</span> <span>(</span><span>$offset</span><span>)</span><br>        <span>{</span><br>            <span>case</span> <span>'_host'</span><span>:</span>       <span>$this</span><span>-&</span>gt<span>;</span>_host <span>=</span> <span>$value</span><span>;</span>  <span>break</span><span>;</span><br>            <span>case</span> <span>'_port'</span><span>:</span>       <span>$this</span><span>-&</span>gt<span>;</span>_port <span>=</span> <span>$value</span><span>;</span>  <span>break</span><span>;</span><br>            <span>case</span> <span>'_expire'</span><span>:</span>     <span>$this</span><span>-&</span>gt<span>;</span>_expire <span>=</span> <span>$value</span><span>;</span>break<span>;</span><br>            <span>case</span> <span>'_compress'</span><span>:</span>   <span>$this</span><span>-&</span>gt<span>;</span>_host <span>=</span> <span>$value</span><span>;</span>  <span>break</span><span>;</span><br>            <span>default</span><span>:</span><br>                <span>if</span> <span>(</span>emptyempty<span>(</span><span>$value</span><span>)</span> <span>&</span>amp<span>;&</span>amp<span>;</span> <span>$value</span> <span>!=</span> <span>'0'</span><span>)</span><br>                    <span>$this</span><span>-&</span>gt<span>;</span>_handler<span>-&</span>gt<span>;</span>delete<span>(</span><span>$offset</span><span>)</span><span>;</span><br>                <span>else</span><br>                    <span>$this</span><span>-&</span>gt<span>;</span>_handler<span>-&</span>gt<span>;</span>set<span>(</span><span>$offset</span><span>,</span><span>$value</span><span>,</span><span>$this</span><span>-&</span>gt<span>;</span>_compress<span>,</span><span>$this</span><span>-&</span>gt<span>;</span>_expire<span>)</span><span>;</span><br>        <span>}</span><br>         <span>$this</span><span>-&</span>gt<span>;</span>_data<span>[</span><span>$offest</span><span>]</span> <span>=</span> <span>$value</span><span>;</span><br>    <span>}</span><br><span>}</span><br><span>$_MEM</span> <span>=</span> <span>new</span> _MEM<span>(</span><span>'10.210.71.51'</span><span>)</span><span>;</span>

举个小例子:

<span>include_once</span><span>(</span><span>'mem.class.php'</span><span>)</span>  <span>;</span>       <br> <br><span>$_MEM</span><span>[</span><span>'dd'</span><span>]</span> <span>=</span> <span>"roast"</span><span>;</span><br><span>unset</span><span>(</span><span>$_MEM</span><span>[</span><span>'dd'</span><span>]</span><span>)</span><span>;</span>               <span>//delete a memcache value</span><br><span>var_dump</span><span>(</span><span>$_MEM</span><span>[</span><span>'dd'</span><span>]</span><span>)</span><span>;</span>                 <br> <br><span>echo</span> <span>$_MEM</span><span>-&</span>gt<span>;</span>_host<span>;</span><br><span>$_MEM</span><span>[</span><span>'_host'</span><span>]</span> <span>=</span> <span>'192.168.1.1'</span><span>;</span>   <span>//set the memcache server host</span><br><span>echo</span> <span>$_MEM</span><span>-&</span>gt<span>;</span>_host<span>;</span>      <br> <br><span>$_MEM</span><span>[</span><span>'roast'</span><span>]</span> <span>=</span> <span>'张立冰'</span><span>;</span><br><span>$memcache</span> <span>=</span> <span>new</span> Memcache<span>;</span><br><span>$memcache</span><span>-&</span>gt<span>;</span>connect<span>(</span><span>'10.210.71.51'</span><span>,</span> <span>11211</span><span>)</span> or <span>die</span> <span>(</span><span>"Could not connect"</span><span>)</span><span>;</span><br><span>var_dump</span><span>(</span><span>$memcache</span><span>-&</span>gt<span>;</span>get<span>(</span><span>'roast'</span><span>)</span><span>)</span><span>;</span>

您可能还喜欢

  • 将MySQL数据映射到Memcached中
  • Download File With Cache-Control Header
  • PHP中的执行命令和管道及进程间通讯
  • Random Tips
  • 将MySQL数据映射到Memcached
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn