zoukankan      html  css  js  c++  java
  • 缓存服务器哈希一致性操作

    这两天在研究缓存服务器的哈希一致性,看介绍文档对一些具体概念操作起来有点模糊,搜索到一篇具体的代码实现,记录下来,以备温习。废话不多说,直接上代码,非常简单清晰明了

    <?php
    /**
     *@author:xiaojiang 20140222
     * 一致性哈希php 实现
     */
    
    class MyHash{
        
        //虚拟节点数
        private $_virtualCounts = 2; 
        //虚拟节点集合
        private $_circleItems = array();
        //实际节点
        private $_items = array();
        //实际节点数
        private $_itemsCount = 0;
        //是否需要排序
        private $_itemRelKey = array();
        private $needSort = false;
    
    
        private $algo;
    
        public function __construct( hash_algo $algo = null ){
            if( !$algo ){
                $this->algo = new algo_md5();
            }
        }
    
        public function addItem( $_item ){
            
            if( isset( $this->_items[$_item]) ){
                throw new Exception("item exists");
            }
            $this->_items[$_item] = array();
            for( $i = 0; $i < $this->_virtualCounts; $i++ ){
                 $_virturalKey = $this->algo->run( $_item.$i );
                 $this->_circleItems[$_virturalKey] = $_item;
                 $this->_itemRelKey[$_item][] = $_virturalKey; 
            }
            $this->needSort = true;
            $this->_itemsCount++;
        }
    
        public function removeItem( $_item ){
        
            if( !isset( $this->_items[$_item] ) ){
                throw new Exception("item is not exists");
            }
            foreach( $this->_itemRelKey[$_item] as $key){
                unset( $this->_circleItems[$key] );
            }
            unset($this->_items[$_item]);
            $this->_itemsCount--;
        }
    
    
        public function getKey( $str ){
    
            if($this->needSort){
                $this->sortItems();
            }    
            
            $_sk = $this->algo->run( $str );
            echo $_sk;
            foreach( $this->_circleItems as $key => $_item){
                
                if( $key > $_sk ){
                    return $_item;
                }
            }
            $ret = array_values(array_slice($this->_circleItems , 0 ,1));
            return $ret[0];
    
        }
    
        private function sortItems(){
            
             ksort( $this->_circleItems ,SORT_STRING );
             $this->needSort = false;
        }
    
        public function _t(){
            print_r($this->_circleItems);
        }
    }
    Interface  hash_algo{
        function run();
    }
    class algo_md5{
        function run( $_str ){
            return MD5( $_str );
        }
    }
    $_tstr = "B8aaaaa";
    $thash = new MyHash();
    $thash->addItem("10.100.200.3");
    $thash->addItem("10.100.200.4");
    
    //$a = $thash->getKey($_tstr);
    //$thash->_t();
    
    $thash->removeItem("10.100.200.4");
    $thash->_t();
    
    
    ?>
  • 相关阅读:
    写了一个数据库的连继ID号(格式:xxxx000001)
    热心的网友<寒羽枫>帮忙解决水晶报表打印纸张问题
    解决vs2005自带水晶报表次数的限制的次数
    WebWork教程 Interceptor(拦截器)
    由于最近网站内容需要更新的还是满多的,于是想开发一个采集系统。收集了一下资料。
    ASP.NET AJAX 1.0 Beta 2 发布
    水晶报表的显示与打印不一至问题
    去年治疗过敏性鼻炎所用的药。
    正则表达式快速入门教程
    sql复制一条相同的记录最快最好的办法。
  • 原文地址:https://www.cnblogs.com/dawq/p/5763978.html
Copyright © 2011-2022 走看看