memcache通用类 包括一致hash分配

千芳同学_3365

千芳同学_3365

2016-05-23

1344人浏览

原创

1.   class.php

<?php namespace YBL;
class memcache
{
    protected $config = array();
    public $memcache_obj = '';
    protected $host_list = '';
    protected $connectFailures = 0;
    public function __construct($config = array())
    //默认配置
    {
        $this->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 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

<?php namespace YBL;
require_once('../lib/memcache/class.php');
//====================================================================================
//默认配置
$dconfig  = 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分配方案 
    //回调函数
    //参数 主机数组
    '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());

PHP速学视频免费教程(入门到精通)
PHP速学视频免费教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

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

相关专题

更多
Vite 项目如何打包和部署
Vite 项目如何打包和部署

Vite项目打包部署专题汇集生产构建、产物预览、输出目录、资源路径、Nginx与GitHub Pages部署及常见白屏和路由404问题的已审核文章,帮助读者完成上线流程。

2026.10.28

80

16

Vite 如何搭配 Vue.js
Vite 如何搭配 Vue.js

Vite搭配Vue.js专题汇集创建Vue 3项目、配置开发环境、路径别名、Vue插件、代理、多环境构建与调试的已审核文章,帮助读者完成从初始化到日常开发的配置。

2026.10.28

60

15

Vite 项目目录怎么看
Vite 项目目录怎么看

Vite项目目录专题汇集入口文件、src源码、public静态资源、路径别名、项目根目录和构建输出目录相关的已审核文章,帮助读者理解各文件夹的用途与引用方式。

2026.10.28

200

10

Gradle插件开发入门教程
Gradle插件开发入门教程

本专题介绍Gradle插件开发的基本流程,涵盖插件类编写、自定义任务注册、扩展参数设计和插件应用方法,帮助你理解插件如何组织构建逻辑,减少多个项目之间的重复配置。

2026.10.22

140

11

Gradle Wrapper配置教程
Gradle Wrapper配置教程

本专题介绍Gradle Wrapper的基本作用,涵盖文件组成、生成方法、版本配置和任务调用,帮助你理解项目如何使用指定的Gradle版本,减少不同开发环境带来的构建差异。

2026.10.22

120

10

Golangci-lint检查器使用教程
Golangci-lint检查器使用教程

本专题整理Golangci-lint常用Linters的用途与配置方法,覆盖错误处理、无效代码、安全、复杂度、命名和依赖检查,帮助项目合理选规则。

2026.10.22

80

15

Gradle怎么构建Java项目
Gradle怎么构建Java项目

本专题介绍Gradle Java项目构建方法,涵盖Java插件配置、依赖声明、源码编译、测试执行和打包检查,帮助你理解各阶段的任务关系,逐步完成项目构建。

2026.10.22

60

10

PHP时间戳漏洞修复教程
PHP时间戳漏洞修复教程

本专题汇总PHP常见时间戳漏洞,包括2038年溢出、时区漂移、时间戳伪造、重放攻击、时间盲注与日志审计失效等,并给出升级64位环境、使用 DateTimeImmutable、统一UTC、严格超时校验、预处理绑定、hash_equals 比对等修复方案,帮助开发者快速排查和加固时间相关代码。

2026.10.22

80

25

Golangci-lint配置文件与规则设置
Golangci-lint配置文件与规则设置

本专题聚焦Golangci-lint YAML配置文件,讲清运行参数、规则启停、目录排除、问题过滤及严重级别设置,便于团队统一检查标准。

2026.10.22

40

15

热门下载

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

精品课程

更多
热门推荐
/
最新课程
phpStudy极速入门视频教程
phpStudy极速入门视频教程

共6课时 | 54.7万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时 | 134.1万人学习