登录  /  注册

PHP调用MEMCACHE高速缓存技术实例

little bottle
发布: 2019-04-20 10:42:18
转载
2246人浏览过

在项目中,涉及大访问量时,合理的使用缓存能减轻数据库的压力,同时提升用户体验。即在非实时性的需求的前提下,一小段时间内(若干秒),用于显示的数据从缓存中获取的,而不用直接读取数据库,能有效的减少数据库的读取压力。这里记录一下php语言使用memcache的情形:

      首先,我们建立一个memcachepool,可以根据不同的配置读取,生成不同的memcache实例。用到$memcache->addServer($host,$port,$flag);向连接池中添加一个memcache服务器。代码示例如下:

class memcachePool{
     private static $instance;
     private $memcacheList = array();
    private function __construct(){

    }
     public static function getInstance(){
         if(self::$instance != null)
             return self::$instance;
         self::$instance = new memcachePool();
         return self::$instance;
     }
    /**
     * get memcache object from pool
     * @param  [type] $host 服务器
     * @param  [type] $port 端口
     * @param  [type] $flag 控制是否使用持久化连接。默认TRUE
     * @return [type]
     */
     public function getMemcache($host,$port,$flag){
         if(isset($this->memcacheList[$host.$port]))
             return $this->memcacheList[$host.$port];

        $memcache = new Memcache();
        // 向连接池中添加一个memcache服务器
        $memcache->addServer($host,$port,$flag);
        //开启大值自动压缩,第一个参数表示处理数据大小的临界点,第二个参数表示压缩的比例,默认为0.2
        $memcache->setCompressThreshold(2000,0.2);
        $this->memcacheList[$host.$port] = $memcache;
        return $memcache;
     }
 }
登录后复制

接着实现一个包含memcache常用方法如add,set,get,flush,delete等的方法类,这里命名为dlufmemcache

class dlufMemcache{
     private $memcache = null;
     function __construct($host,$port){

       $this->memcache = memcachepool::getInstance()->getMemcache($host,$port,true);
     }
    /**
     * memcache set value
     * @param [type]  $key 键
     * @param [type]  $value 值
     * @param integer $expire  到期的时间,如果此值设置为0表明此数据永不过期
     * @param integer $flag 标志位 使用MEMCACHE_COMPRESSED指定对值进行压缩(使用zlib)
     * @param [type]  $serializetype
     */
     public function set($key,$value,$expire=0,$flag=0,$serializetype=null){
        if($serializetype == 'json' && is_array($value)){
            $value = json_encode($value);
        }
         $this->memcache->set($key,$value,$flag,$expire);
     }
    /**
     * 从服务端查找元素
     * @param  [type] $key
     * @return [type]
     */
     public function get($key){
         return $this->memcache->get($key);
     }
    /**
     * 增加一个条目到缓存服务器
     * @param [type]  $key
     * @param [type]  $value
     * @param integer $expire
     * @param integer $flag
     * @param [type]  $serializetype
     */
    public function add($key,$value,$expire=0,$flag=0,$serializetype=null){
        if($serializetype == 'json' && is_array($value)){
            $value = json_encode($value);
        }
        $ret = $this->memcache->add($key,$value,$flag,$expire);
        return $ret;
    }
    /**
     * 清洗(删除)已经存储的所有的元素
     * @return [type]
     */
    public function flush(){
        return $this->memcache->flush();
    }
    /**
     *  从服务端删除一个元素
     * @param  [type] delete 参数:key要删除的元素的key 删除该元素的执行时间 timeout如果值为0,则该元素立即删除。
     * @return [type]
     */
    public function delete($key){
        $ret = $this->memcache->delete($key,0);
        return $ret;
    }
 }
登录后复制

然后调用dlufmemcache:

1 $memcache = new dlufMemcache('127.0.0.1',11211);
2  $memcache->set('memcache','come on dluf&baidu !!!!!!');
3  $ret = $memcache->get('memcache');
4  echo print_r($ret,true);
登录后复制

运行输出可见:

 想了解更多关于PHP的知识不?那就赶紧去关注PHP中文网的PHP视频教程吧!

以上就是PHP调用MEMCACHE高速缓存技术实例的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:博客园网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号