php memcache单例

WBOY
Lepaskan: 2016-07-30 13:30:05
asal
1181 orang telah melayarinya

  1. 先将__construct()设置为 private 这样就不允许在外部 new 了
  2. 在类的内部实例化一个对象。外部可以得到就行
<code><span><span>class</span><span>Mem</span><span>extends</span><span>Memcache</span>{</span><span>private</span><span>static</span><span>$mem_instance</span> = <span>null</span>;
    <span>private</span><span><span>function</span><span>__construct</span><span>()</span>{</span><span>parent</span>::__construct();
    }
    <span>private</span><span><span>function</span><span>__clone</span><span>()</span>{</span>}
    <span>public</span><span>static</span><span><span>function</span><span>get_mem_ins</span><span>()</span>{</span><span>if</span>(<span>null</span> === <span>self</span>::<span>$mem_instance</span>){
            <span>self</span>::<span>$mem_instance</span> = <span>new</span> Memcache;
        }
        <span>return</span><span>self</span>::<span>$mem_instance</span>;
    }
}</code>
Salin selepas log masuk
  1. memcache 操作
<code><span></span>php
    <span>//连接</span><span>$mem</span><span>=</span><span>new</span> Memcache;
    <span>$mem</span><span>-></span>connect(<span>"db.nowamagic.net"</span>, <span>12000</span>);
    <span>//保存数据</span><span>$mem</span><span>-></span><span>set</span>(<span>'key1'</span>, <span>'This is first value'</span>, <span>0</span>, <span>60</span>);
    <span>$val</span><span>=</span><span>$mem</span><span>-></span>get(<span>'key1'</span>);
    echo <span>"Get key1 value: "</span><span>.</span><span>$val</span><span>.</span><span>"<br>"</span>;
    <span>//替换数据</span><span>$mem</span><span>-></span>replace(<span>'key1'</span>, <span>'This is replace value'</span>, <span>0</span>, <span>60</span>);
    <span>$val</span><span>=</span><span>$mem</span><span>-></span>get(<span>'key1'</span>);
    echo <span>"Get key1 value: "</span><span>.</span><span>$val</span><span>.</span><span>"<br>"</span>;
    <span>//保存数组</span><span>$arr</span><span>=</span><span>array</span>(<span>'aaa'</span>, <span>'bbb'</span>, <span>'ccc'</span>, <span>'ddd'</span>);
    <span>$mem</span><span>-></span><span>set</span>(<span>'key2'</span>, <span>$arr</span>, <span>0</span>, <span>60</span>);
    <span>$val2</span><span>=</span><span>$mem</span><span>-></span>get(<span>'key2'</span>);
    echo <span>"Get key2 value: "</span>;
    print_r(<span>$val2</span>);
    echo <span>"<br>"</span>;
    <span>//删除数据</span><span>$mem</span><span>-></span>delete(<span>'key1'</span>);
    <span>$val</span><span>=</span><span>$mem</span><span>-></span>get(<span>'key1'</span>);
    echo <span>"Get key1 value: "</span><span>.</span><span>$val</span><span>.</span><span>"<br>"</span>;
    <span>//清除所有数据</span><span>$mem</span><span>-></span>flush();
    <span>$val2</span><span>=</span><span>$mem</span><span>-></span>get(<span>'key2'</span>);
    echo <span>"Get key2 value: "</span>;
    print_r(<span>$val2</span>);
    echo <span>"<br>"</span>;
    <span>//关闭连接</span><span>$mem</span><span>-></span>close();
<span>?></span><span></span></code>
Salin selepas log masuk
  1. 输出
<code>Get key1 <span>value</span>: This <span>is</span> first <span>value</span>
Get key1 <span>value</span>: This <span>is</span> replace <span>value</span>
Get key2 <span>value</span>: Array ( [<span>0</span>] => aaa [<span>1</span>] => bbb [<span>2</span>] => ccc [<span>3</span>] => ddd )
Get key1 <span>value</span>:
Get key2 <span>value</span>:</code>
Salin selepas log masuk

版权声明:知识取之于民,用之于民!欢迎转载,转载请开头附上本文链接,不定期更新文章!

以上就介绍了php memcache单例,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan