首页 > 后端开发 > php教程 > PHP-Mmecache操作类详细介绍

PHP-Mmecache操作类详细介绍

WBOY
发布: 2016-07-28 08:26:15
原创
1035 人浏览过

下面类对memcache进行封装,包括了对memcache的添加,读取,清空,删除,获取服务器的信息,缓存服务池等。

<code><span><span><span><?php </span>/*******************************************
 * 文件名: /includes/memcache.class.php
 * 功能:    memcache 缓存类
 * 版本:      1.0
 * 日期:      2016-07-16
 * 程序名: memcache缓存操作类 -----(PHP中需加载memcache扩展)
 * 作者:      JoeXiong
 *  版权:      Copyright@2016-2016 github.com/JoeXiong All Rights Reserved
 *********************************************/</span><span><span>class</span><span>joememcache</span>
{</span><span>private</span><span>static</span><span>$_instance</span>;
    <span>private</span><span>$_memcache</span>;
    <span>private</span><span>$_which</span> = <span>0</span>;
    <span>private</span><span>$_memservers</span>;

    <span>public</span><span>static</span><span><span>function</span><span>getInstance</span><span>()</span>
    {</span><span>if</span> (! (<span>self</span>::<span>$_instance</span><span>instanceof</span> joememcache)) {
            <span>self</span>::<span>$_instance</span> = <span>new</span><span>self</span>();
        }
        <span>return</span> is_object(<span>self</span>::<span>$_instance</span>->_memcache) ? <span>self</span>::<span>$_instance</span> : <span>false</span>;
    }

    <span>private</span><span><span>function</span><span>__construct</span><span>()</span>
    {</span><span>if</span> (extension_loaded(<span>'memcache'</span>)) {
            <span>$this</span>->_memcache = <span>new</span> Memcache();
            <span>$this</span>->_which = <span>1</span>;
        } <span>elseif</span> (extension_loaded(<span>'memcached'</span>)) {
            <span>$this</span>->_memcache = <span>new</span> Memcached();
            <span>$this</span>->_which = <span>2</span>;
        } <span>else</span><span>$this</span>->_memcache = <span>FALSE</span>;
    }

    <span>/**
     * 保存缓存
     *<span> @param</span> unknown $key
     *<span> @param</span> unknown $data
     *<span> @param</span> number $ttl
     *<span> @param</span> string $isCompress
     *<span> @return</span> boolean
     */</span><span>public</span><span><span>function</span><span>Save</span><span>(<span>$key</span>, <span>$data</span>, <span>$ttl</span> = <span>60</span>,<span>$isCompress</span> = FALSE)</span>{</span><span>if</span>(<span>$this</span>->_which == <span>1</span>)
            <span>return</span><span>$this</span>->_memcache->set(<span>$key</span>, <span>array</span>(<span>$data</span>, time(), <span>$ttl</span>), !<span>$isCompress</span> ? <span>0</span> : MEMCACHE_COMPRESSED, <span>$ttl</span>);<span>//使用time() 函数最新</span><span>else</span><span>if</span>(<span>$this</span>->_which == <span>2</span>)
            <span>return</span><span>$this</span>->_memcache->set(<span>$key</span>, <span>array</span>(<span>$data</span>, time(), <span>$ttl</span>), <span>$ttl</span>);
        <span>else</span><span>return</span><span>FALSE</span>;
    }

    <span>/**
     * 读取缓存信息
     *<span> @param</span> unknown $key
     *<span> @return</span> Ambigous <multitype: string>|boolean
     */</multitype:></span><span>public</span><span><span>function</span><span>readMetaData</span><span>(<span>$key</span>)</span>{</span><span>$value</span> = <span>$this</span>->_memcache->get(<span>$key</span>);
        <span>if</span>(is_array(<span>$value</span>) && count(<span>$value</span>) == <span>3</span>){
            <span>list</span>(<span>$data</span>, <span>$time</span>, <span>$ttl</span>) = <span>$value</span>;
            <span>return</span> (time() $time</span> + <span>$ttl</span>) ? <span>$data</span> : <span>array</span>();
        }
        <span>else</span> {
            <span>return</span><span>false</span>;
        }
    }

    <span>/**
     *<span> @deprecated</span> 读取多个缓存信息
     *<span> @param</span> array $array
     *<span> @return</span> 成功$value(array), 失败FALSE(bool)
     */</span><span>public</span><span><span>function</span><span>readMultiData</span><span>(<span>$keys</span>)</span>
    {</span><span>if</span>(!is_array(<span>$keys</span>)) {
            <span>return</span><span>FALSE</span>;
        }

        <span>$rtn</span> = <span>array</span>();
        <span>if</span>(<span>$this</span>->_which == <span>1</span>)
            <span>$rtn</span> = <span>$this</span>->_memcache->get(<span>$keys</span>);
        <span>else</span><span>if</span>(<span>$this</span>->_which == <span>2</span>)
            <span>$rtn</span> = <span>$this</span>->_memcache->getMulti(<span>$keys</span>);

        <span>$now</span> = time();
        <span>foreach</span>(<span>$rtn</span><span>as</span><span>$key</span>=>&<span>$v</span>) {
            <span>if</span>(!<span>empty</span>(<span>$v</span>)) {
                <span>list</span>(<span>$data</span>, <span>$time</span>, <span>$ttl</span>) = <span>$v</span>;
                <span>$v</span> = (<span>$now</span> $time</span> + <span>$ttl</span>) ? <span>$data</span> : <span>array</span>();
            }
        }
        <span>return</span><span>$rtn</span>;
    }

    <span>/**
     *<span> @description</span> 读取缓存
     *<span> @param</span>  $key    查询索引key
     *<span> @return</span>  成功 array 失败 FALSE
     */</span><span>public</span><span><span>function</span><span>Read</span><span>(<span>$key</span>)</span>
    {</span><span>$data</span> = <span>$this</span>->_memcache->get(<span>$key</span>);
        <span>return</span> is_array(<span>$data</span>) ? <span>$data</span>[<span>0</span>] : <span>FALSE</span>;
    }

    <span>/**
     *<span> @description</span> 删除缓存
     *<span> @param</span> $key 将要删除的key
     * return bool 成功 TRUE 失败 FALSE
     */</span><span>public</span><span><span>function</span><span>Delete</span><span>(<span>$key</span>)</span>
    {</span><span>return</span><span>$this</span>->_memcache->delete(<span>$key</span>);
    }

    <span>/**
     *<span> @description</span> 清空所有缓存
     *<span> @return</span>  bool true or false
     */</span><span>public</span><span><span>function</span><span>Clear</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_memcache->flush();
    }

    <span>/**
     * 获取缓存服务器池中所有服务器统计信息
     *<span> @return</span> array
     */</span><span>public</span><span><span>function</span><span>getExtendedStats</span><span>()</span>
    {</span><span>//return $this->_memcache->getExtendedStats();</span><span>if</span>(<span>$this</span>->_which == <span>1</span>) {
            <span>return</span><span>$this</span>->_memcache->getExtendedStats();
        }
        <span>else</span><span>if</span>(<span>$this</span>->_which == <span>2</span>)
            <span>return</span><span>$this</span>->_memcache->getServerList();
        <span>else</span><span>return</span><span>FALSE</span>;
    }

    <span>/**
     * 缓存服务器池
     */</span><span>public</span><span><span>function</span><span>addServer</span><span>()</span>
    {</span><span>foreach</span> (<span>$this</span>->_memservers <span>as</span><span>$h</span>) {
            <span>$this</span>->_memcache->addServer(<span>$h</span>[<span>'host'</span>], <span>isset</span>(<span>$h</span>[<span>'port'</span>]) ? <span>$h</span>[<span>'port'</span>] : <span>11211</span>); <span>// 默认端口为11211</span>
        }
    }

    <span>/**
     * 获取memecache服务器地址
     */</span><span>public</span><span><span>function</span><span>getHost</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_memservers;
    }

    <span>/**
     * 设置memcache服务器地址
     */</span><span>public</span><span><span>function</span><span>setHost</span><span>(array <span>$servers</span>)</span>
    {</span><span>if</span> (is_array(<span>$servers</span>) && ! <span>empty</span>(<span>$servers</span>)) {
            <span>$this</span>->_memservers = <span>$servers</span>;
            <span>$this</span>->addServer();
        }
    }

    <span>private</span><span><span>function</span><span>__clone</span><span>()</span>
    {</span>}
}</code>
登录后复制

以下是对封装类的使用方法

<code><span>//初始化memcache</span><span>$memcache</span> = joememcache::getInstance();
        <span>//是否开启memcache扩展</span><span>if</span>(<span>$memcache</span>){
           <span>//memcache的配置参数,放在配置文件中</span><span>global</span><span>$memServer</span>,<span>$memKey</span>;
           <span>//设置memecache服务器地址</span><span>$memcache</span>->setHost(<span>$memServer</span>); 
           <span>//通过key读取数据</span><span>$news</span> = <span>$memcache</span>->readMetaData(<span>$memKey</span>[<span>'sharetypekey'</span>][<span>'10001'</span>][<span>'Key'</span>].<span>$id</span>);
           <span>//memcache没有当前key的数据,则从数据库查询并保存到memecache服务器中</span><span>if</span>(!<span>$news</span>){
               <span>$news</span> = <span>$this</span>->getCurSharenewsInfo(<span>$id</span>);
               <span>$memcache</span>->Save(<span>$memKey</span>[<span>'sharetypekey'</span>][<span>'10001'</span>][<span>'Key'</span>].<span>$id</span>,<span>$news</span>,<span>$memKey</span>[<span>'sharetypekey'</span>][<span>'10001'</span>][<span>'Time'</span>]);
           }
        }<span>else</span>{
            <span>//直接数据库查询数据</span><span>$news</span> = <span>$this</span>->getCurSharenewsInfo(<span>$id</span>);
        }</code>
登录后复制
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了 PHP-Mmecache操作类详细介绍,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板