0

0

memcache通用类 包括hash分配

PHP中文网

PHP中文网

发布时间:2016-05-23 08:39:44

|

1237人浏览过

|

来源于php中文网

原创

1.  class.php

config = array(
            'host' => '127.0.0.1',
            'port' => 11211,
            'persistent' => false,
            'weight' => 1,
            //超时时间
            'timeout' => 1,
            //重试间隔时间
            'retry_interval' => 15,
            'status' => false,
            //=============================
            //是否打开压缩
            'threshold' => 1,
            //压缩率
            'min_savings' => 0.2,
            //最大重试次数
            'max_connect_retries' => 5
        );
        //不带一致性hash分配方案
        if (!isset($config['memcache_deploy']) || !is_callable($config['memcache_deploy'])) {
            $config['memcache_deploy'] = array(
                __CLASS__,
                'default_memcache_deploy'
            );
        }
        //带一致性hash分配方案
        if (!isset($config['memcache_hdeploy']) || !is_callable($config['memcache_hdeploy'])) {
            $config['memcache_hdeploy'] = array(
                __CLASS__,
                'default_memcache_hdeploy'
            );
        }
        //错误处理
        if (!isset($config['failure_callback']) || !is_callable($config['failure_callback'])) {
            $config['failure_callback'] = array(
                __CLASS__,
                'default_error_hander'
            );
        }
        $this->config       = array_merge($this->config, $config);
        $this->memcache_obj = new \Memcache;
    }
    public function host($host_list = '')
    {
        $this->host_list = $host_list;
        return $this;
    }
    //默认错误处理
    static public function default_error_hander($host = array())
    //var_dump($host);
    {
         
    }
    static public function default_memcache_hdeploy($host, $hash = '')
    {
        static $node_object;
        if (!($node_object instanceof yhash)) {
            $config      = array(
                //虚拟节点20个
                'replicas' => 20
            );
            $node_object = new yhash($config);
            //添加节点
            $node_object->addTargets(array_keys($host));
        }
        return $node_object->lookup($hash);
    }
     
     
    static public function default_memcache_deploy($host)
    {
        $keys = array_keys($host);
        $key  = array_rand($keys, 1);
        return $keys[$key];
    }
     
    public function alink()
    {
         
        $default_host     = array(
            'host' => $this->config['host'],
            'port' => $this->config['port'],
            'persistent' => $this->config['persistent'],
            'weight' => $this->config['weight'],
            'timeout' => $this->config['timeout'],
            'retry_interval' => $this->config['retry_interval'],
            'status' => $this->config['status'],
            'failure_callback' => $this->config['failure_callback'],
            'threshold' => $this->config['threshold'],
            'min_savings' => $this->config['min_savings']
        );
        $failure_callback = '';
        if (!empty($this->host_list)) {
            foreach ($this->host_list as $_host) {
                $_host = array_merge($default_host, $_host);
                $failure_callback = function($host = '', $port = 11211) use ($_host)
                {
                    return call_user_func_array($_host['failure_callback'], array(
                        $_host
                    ));
                };
                $this->memcache_obj->addServer($_host['host'], $_host['port'], $_host['persistent'], $_host['weight'], $_host['timeout'], $_host['retry_interval'], $_host['status'], $failure_callback);
                if ($_host['threshold']) {
                    $this->memcache_obj->setCompressThreshold($_host['threshold'], $_host['min_savings']);
                }
            }
        } else {
            $failure_callback = function($host = '', $port = 11211) use ($default_host)
            {
                return call_user_func_array($default_host['failure_callback'], array(
                    $default_host
                ));
            };
            $this->memcache_obj->addServer($default_host['host'], $default_host['port'], $default_host['persistent'], $default_host['weight'], $default_host['timeout'], $default_host['retry_interval'], $default_host['status'], $failure_callback);
            if ($default_host['threshold']) {
                $this->memcache_obj->setCompressThreshold($default_host['threshold'], $default_host['min_savings']);
            }
             
        }
    }
     
    public function link()
    {
        $default_host = '';
        $host_no      = -1;
        $return       = false;
        $_host        = array();
        if (!empty($this->host_list)) {
            $host_no = call_user_func_array($this->config['memcache_deploy'], array(
                $this->host_list
            ));
            $_host   = $this->host_list[$host_no];
        }
         
        $default_host = array(
            'host' => $this->config['host'],
            'port' => $this->config['port'],
            'persistent' => $this->config['persistent'],
            //'weight'=>$this->config['weight'],
            'timeout' => $this->config['timeout'],
            'retry_interval' => $this->config['retry_interval'],
            //'status'=>$this->config['status'],
            'failure_callback' => $this->config['failure_callback'],
            'threshold' => $this->config['threshold'],
            'min_savings' => $this->config['min_savings']
        );
        $_host        = array_merge($default_host, $_host);
         
        if ($_host['persistent']) {
            $return = @$this->memcache_obj->pconnect($_host['host'], $_host['port'], $_host['timeout']);
        } else {
            $return = @$this->memcache_obj->connect($_host['host'], $_host['port'], $_host['timeout']);
        }
        if (!$return) {
            $this->connectFailures++;
            if ($this->connectFailures <= $this->config['max_connect_retries']) {
                if (count($this->host_list) > 1) {
                    call_user_func_array($_host['failure_callback'], array(
                        $_host
                    ));
                    usleep($_host['interval_time']);
                    unset($this->host_list[$host_no]);
                    $this->link();
                } else {
                    $this->connectFailures = 0;
                    call_user_func_array($_host['failure_callback'], array(
                        $_host
                    ));
                }
            } else {
                $this->connectFailures = 0;
                call_user_func_array($_host['failure_callback'], array(
                    $_host
                ));
            }
        } else {
            $this->connectFailures = 0;
            if ($_host['threshold']) {
                $this->memcache_obj->setCompressThreshold($_host['threshold'], $_host['min_savings']);
            }
             
        }
    }
     
     
     
    public function hlink($hash = '')
    {
         
        $default_host = '';
        $host_no      = -1;
        $return       = false;
        $_host        = array();
        if (!empty($this->host_list)) {
            $host_no = call_user_func_array($this->config['memcache_hdeploy'], array(
                $this->host_list,
                $hash
            ));
            $_host   = $this->host_list[$host_no];
        }
         
        $default_host = array(
            'host' => $this->config['host'],
            'port' => $this->config['port'],
            'persistent' => $this->config['persistent'],
            //'weight'=>$this->config['weight'],
            'timeout' => $this->config['timeout'],
            'retry_interval' => $this->config['retry_interval'],
            //'status'=>$this->config['status'],
            'failure_callback' => $this->config['failure_callback'],
            'threshold' => $this->config['threshold'],
            'min_savings' => $this->config['min_savings'],
            'hash' => $hash
        );
        $_host        = array_merge($default_host, $_host);
        if ($_host['persistent']) {
            $return = $this->memcache_obj->pconnect($_host['host'], $_host['port'], $_host['timeout']);
        } else {
            $return = $this->memcache_obj->connect($_host['host'], $_host['port'], $_host['timeout']);
        }
        if (!$return) {
             
            $this->connectFailures = 0;
            call_user_func_array($_host['failure_callback'], array(
                $_host
            ));
             
        } else {
             
            if ($_host['threshold']) {
                $this->memcache_obj->setCompressThreshold($_host['threshold'], $_host['min_savings']);
            }
             
        }
    }
    /*
    type类型 值reset, malloc, maps, cachedump, slabs, items, sizes
    slabid slab的id
    limit 返回的最大条数
    */
    public function _server($type = '', $slabid = 1, $limit = 1000000)
    {
        //$this->memcache_obj->close();
        if ($type === '')
            return $this->memcache_obj->getStats();
        if ($type != 'cachedump')
            return $this->memcache_obj->getStats($type);
        return $this->memcache_obj->getStats($type, $slabid, $limit);
    }
    public function _allserver($type = '', $slabid = 1, $limit = 1000000)
    {
        if ($type === '')
            return $this->memcache_obj->getExtendedStats();
        if ($type != 'cachedump')
            return $this->memcache_obj->getExtendedStats($type);
        return $this->memcache_obj->getExtendedStats($type, $slabid, $limit);
    }
    public function close()
    {
        return $this->memcache_obj->close();
    }
    public function slabid()
    {
        $ids = $id = array();
        $ids = $this->memcache_obj->getStats('slabs');
        foreach ($ids as $k => $v) {
            if (is_int($k)) {
                $id[] = $k;
            }
        }
        return $id;
    }
    //!!!!!!!!!!!============================================
    public function getallkey($limit = 1000000)
    {
        $ids = $this->slabid();
        $arr = array();
        foreach ($ids as $id) {
            $arr = array_merge(array_keys($this->memcache_obj->getStats('cachedump', $id, $limit)), $arr);
        }
        return $arr;
    }
    public function set($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)
    {
        return $this->memcache_obj->set($key, $var, $flag, $expire);
    }
     
    public function add($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)
    {
        return $this->memcache_obj->add($key, $var, $flag, $expire);
    }
     
    public function get($key, &$flag = 0)
    {
        $gflag  = 0;
        $return = $this->memcache_obj->get($key, $gflag);
        $flag   = $gflag;
        return $return;
    }
     
    public function delete($key, $timeout = 0)
    {
        if ($key === 'all') {
            return $this->memcache_obj->flush();
        }
        return $this->memcache_obj->delete($key, $timeout);
    }
    public function replace($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)
    {
        return $this->memcache_obj->replace($key, $var, $flag, $expire);
    }
     
    public function increment($key, $var)
    {
        return $this->memcache_obj->increment($key, (int) $var);
    }
     
    public function decrement($key, $var)
    {
        return $this->memcache_obj->decrement($key, (int) $var);
    }
     
    public function append($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)
    {
        $return = $this->memcache_obj->get($key);
        if (empty($return))
            return false;
        return $this->memcache_obj->replace($key, $return . $var, $flag, $expire);
    }
    public function prepend($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)
    {
        $return = $this->memcache_obj->get($key);
        if (empty($return))
            return false;
        return $this->memcache_obj->replace($key, $var . $return, $flag, $expire);
    }
    public function cas($key, $key2)
    {
        $return1 = $this->memcache_obj->get($key);
        if (empty($return1))
            return false;
        $return2 = $this->memcache_obj->get($key2);
        if (empty($return2))
            return false;
        return ($this->memcache_obj->replace($key, $return2)) && ($this->memcache_obj->replace($key2, $return));
    }
 }

                                                             

2. memcache.php 

 
 '127.0.0.1',
    //端口
    'port' => 11211,
    //是否长连接
    'persistent' => false,
    //比重
    'weight' => 1,
    //超时时间
    'timeout' => 1,
    //重试间隔时间
    'retry_interval' => 15,
    //状态
    'status' => false,
    //=============================
    //是否打开压缩
    'threshold' => 1,
    //压缩率
    'min_savings' => 0.2,
    //最大重试次数
    'max_connect_retries' => 5,
    //不带一致性hash分配方案
    //回调函数
    //参数 主机数组
    'memcache_deploy' => 'default_memcache_deploy',
    //带一致性hash分配方案
    //回调函数
    //参数 主机数组 hash值
    'memcache_hdeploy' => 'default_memcache_hdeploy',
    //错误处理
    //回调函数
    //参数 主机数组
    'failure_callback' => 'default_error_hander'
);
//====================================================================================
//连接
/*memcahe 三种连接方式
1.连接池addserver
2.普通connect/pconnect
3.带hash的connect/pconnect
*/
//1.连接池
$memcache = new memcache();
$host     = array(
    array(
        'host' => 'localhost',
        'port' => '11211'
    ),
    array(
        'host' => '127.0.0.1',
        'port' => '11211'
    ),
    array(
        'host' => 'localhost',
        'port' => '11212'
    ),
    array(
        'host' => '127.0.0.1',
        'port' => '11212'
    )
);
$memcache->host($host);
$memcache->alink();
//print_r($memcache->_allserver());
//-------------------------------------------------------------
//2.普通connect/pconnect
$memcache2 = new memcache();
$host      = array(
    array(
        'host' => 'localhost',
        'port' => '11211'
    ),
    array(
        'host' => '127.0.0.1',
        'port' => '11211'
    ),
    array(
        'host' => 'localhost',
        'port' => '11212'
    ),
    array(
        'host' => '127.0.0.1',
        'port' => '11212'
    )
);
$memcache2->host($host);
$memcache2->link();
//print_r($memcache2->_allserver());
//--------------------------------------------------------------
//3.带hash的connect/pconnect
//导入算法类
include('../lib/hash/class.php');
$memcache3 = new memcache();
$host      = array(
    array(
        'host' => 'localhost',
        'port' => '11211'
    ),
    array(
        'host' => '127.0.0.1',
        'port' => '11211'
    ),
    array(
        'host' => 'localhost',
        'port' => '11211'
    ),
    array(
        'host' => '127.0.0.1',
        'port' => '11211'
    )
);
$memcache3->host($host);
//$memcache3->hlink('233ybl');
//print_r($memcache3->_allserver());
//==================================================================================
//内置函数
//set($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)
//add($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)
//get($key, &$flag = 0)
//delete($key, $timeout = 0)
//replace($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)
//increment($key, $var)
//decrement($key, $var)
//append($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)
//prepend($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)
// cas($key, $key2)
$memcache3->hlink('32ybl');
$memcache3->set('32ybl', 55);
//$memcache3->increment('32ybl',333);
echo $memcache3->get('32ybl');
//======================================================================
//系统函数
//获取当前连接服务器信息
//_server($type = '', $slabid = 1, $limit = 1000000)
//获取所有连接服务器信息
//_allserver($type = '', $slabid = 1, $limit = 1000000)
//获取所有key名
//getallkey($limit=1000000)
var_dump($memcache->getallkey());

                           

贞龙网店商城电子商务系统java版
贞龙网店商城电子商务系统java版

BIZOSS-B2C是脱胎于贞龙B2B大型平台的网上商城系统、网上商店系统、网上购物系统的企业级B2C电子商务解决方案。系统设置:这里包含了网店的常用功能和全局配置的开关。包括 商店设置 、支付方式和配送方式 、邮件服务器设置、地区列表、友情链接、自定义导航栏、站点地图。商品管理:网店展示商品的核心。其中包括了 商品分类、商品类型、商品品牌、商品回收站、商品上下架等一些设置。促销管理:这个是我们网

下载

       

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

7

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

4

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

7

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

7

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

42

2025.12.31

html5怎么播放视频
html5怎么播放视频

想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。

4

2025.12.31

关闭win10系统自动更新教程大全
关闭win10系统自动更新教程大全

本专题整合了关闭win10系统自动更新教程大全,阅读专题下面的文章了解更多详细内容。

3

2025.12.31

阻止电脑自动安装软件教程
阻止电脑自动安装软件教程

本专题整合了阻止电脑自动安装软件教程,阅读专题下面的文章了解更多详细教程。

3

2025.12.31

html5怎么使用
html5怎么使用

想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!

2

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

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