文档章节

php 一致性hash

ayoway
 ayoway
发布于 2016/05/22 23:38
字数 435
阅读 11
收藏 0

类:https://github.com/pda/flexihash

Memcached 实例

flexihash_memcache.php

require_once 'flexihash.php';
Class FMemcache
{
  
    public $hash = null;
    public $memcache = null;
    public $connectPool = null;
  
    public function __construct()
    {
        $this->hash = new Flexihash();
    }
  
    public function addServers( $servers )
    {
        foreach ($servers as $server)
        {
            $node = $server['host'] . ':' . $server['port'];
            $this->connectPool[$node] = false;
            $targets[] = $node;
        }
        $this->hash->addTargets( $targets );
    }
  
    public function set( $key, $value )
    {
        $nodes = $this->hash->lookupList( $key, count( $this->connectPool ) );
        foreach ($nodes as $node)
        {
            if (!$this->connectPool[$node])
            {
                $server = explode( ':', $node );
                $this->connectPool[$node] = @memcache_connect( $server[0], $server[1] );
            }
            if ($this->connectPool[$node])
            {
                if (memcache_set( $this->connectPool[$node], $key, $value ))
                {
                    return true;
                }
            }
        }
        return false;
    }
  
    public function get( $key )
    {
        $nodes = $this->hash->lookupList( $key, count( $this->connectPool ) );
        foreach ($nodes as $node)
        {
            if (!$this->connectPool[$node])
            {
                $server = explode( ':', $node );
                $this->connectPool[$node] = @memcache_connect( $server[0], $server[1] );
            }
            if ($this->connectPool[$node])
            {
                if (memcache_get( $this->connectPool[$node], $key ))
                {
                    return true;
                }
            }
        }
        return false;
    }
	  
}
	require_once 'flexihash_memcache.php';
	require_once 'Config.php';
	$st = microtime( true );
	$mem = new FMemcache();
	$mem->addServers( $tt_server_pool );
	$wordAmount = 0;
	$getCount = 0;
	foreach ($words as $word)
	{
	    if (empty( $word ))
	    {
	        continue;
	    }
	    $inc = $mem->set( $word, $word );
	}
	$et = microtime( true ) - $st;
	echo "time used:" . $et;

 

Redis 实例

flexihash_redis.php

<?php
require 'flexihash.php';
Class FRedis
{
    public $hash = null;
    public $memcache = null;
    public $connectPool = null;
    public $timeOut = 3;
    public $auth = "";
 
    public function __construct()
    {
        $this->hash = new Flexihash();
    }
 
    public function addServers( $servers )
    {
        foreach ($servers as $server)
        {
            $node = $server['host'] . ':' . $server['port'];
            $this->connectPool[$node] = false;
            $targets[] = $node;
        }
        $this->hash->addTargets( $targets );
        
    }
 
    public function hset($hashkey, $valuekey, $value )
    {
        $node = $this->hash->lookup( $hashkey, count( $this->connectPool ) );
        if (!$this->connectPool[$node])
        {
            $server = explode( ':', $node );
            $redis = new Redis();
            $redis->connect($server[0], $server[1], $this->timeOut);
            if(!empty($this->auth)){
                $redis->auth($this->auth);
            }
            $this->connectPool[$node] = $redis;

        }
        if ($this->connectPool[$node])
        {
            $redis = $this->connectPool[$node];
            if($redis->hset($hashkey,$valuekey,$value)){
                return true;
            }
        }
        return false;
    }
 
    public function hget( $hashkey, $valuekey )
    {
        $node = $this->hash->lookup( $hashkey, count( $this->connectPool ) );
        
        if (!$this->connectPool[$node])
        {
            $server = explode( ':', $node );
            $redis = new Redis();
            $redis->connect($server[0], $server[1], $this->timeOut);
            if(!empty($this->auth)){
                $redis->auth($this->auth);
            }
            $this->connectPool[$node] = $redis;
        
        }
        if ($this->connectPool[$node])
        {
            $redis = $this->connectPool[$node];
            if($value = $redis->hget($hashkey,$valuekey)){
                return $value;
            }
        }
        return false;
    }
}

flexihash_redis.php

<?php
require_once 'flexihash_redis.php';
$st = microtime( true );
$fredis = new FRedis();
$tt_server_pool = array(
    '0' => array(
        'host' => '192.168.1.101',
        'port' => '6379',
    ),
    '1' => array(
        'host' => '192.168.1.102',
        'port' => '6379',
    ),
);
$fredis->addServers( $tt_server_pool );

for($i=0;$i<10;$i++){
    for($k=0;$k<10;$k++){
        $fredis->hset("hashkey:".$i,"valuekey".$k,md5($i*$k));
    }
}

$et = microtime( true ) - $st;
echo "time used:" . $et."<br/>";
echo $fredis->hget("hashkey:0","valuekey9");

 

本文转载自:https://github.com/pda/flexihash

ayoway
粉丝 3
博文 44
码字总数 13558
作品 0
深圳
高级程序员
私信 提问
【转载】memcache分布式 [一致性hash算法] 的php实现

最近在看一些分布式方面的文章,所以就用php实现一致性hash来练练手,以前一般用的是最原始的hash取模做分布式,当生产过程中添加或删除一台memcache都会造成数据的全部失效,一致性hash就是...

hengfeng_su
2015/04/01
178
0
memcache一致性hash的php实现方法

memcache一致性hash的php实现方法 本文实例讲述了memcache一致性hash的php实现方法。分享给大家供大家参考。具体如下: 最近在看一些分布式方面的文章,所以就用php实现一致性hash来练练手,...

开元中国2015
2015/04/20
82
0
提高Memcached缓存命中率(在php中的实现)

Memcached默认是采用余数hash策略,这样在增加和减少机器的时候会导致很大部分的Memcached无法命中,可以采用Consistent Hashing(一致性Hash算法)可以有效缓解这个问题。 //php iniset("memc...

Jean
2014/01/16
2.1K
3
nginx一致性hash

consistencehash https://github.com/replay/ngxhttpconsistenthash->download https://github.com/replay/ngxhttpconsistenthash/archive/master.zip [root@localhost soft]# wget https://......

泡海椒
2016/03/08
506
0
nginx+PHP+memcached+MySQL+ip-hash做memcached集群

1、nginx与memcached整合 #安装memcached支持的事务库libevent wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gztar zxf......

80后小菜鸟
2017/09/01
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Linux 运行shell文件,出现 $'\r': command not found

运行编写的shell脚本时,出现了 $'\\r': command not found 这样的错误提示。 报错的原因是我们在windows系统操作时,编辑器里的换行符是\r\n ,而Linux上为\n,两个系统之间有差异导致的。 ...

芥末无敌
今天
10
0
Java数据结构(上)

枚举(Enumeration) 位集合(BitSet) 向量(Vector) 栈(Stack) 1.Enumeration(枚举) boolean hasMoreElements( ):测试是否有更多的元素 Object nextElement( ):如果此枚举对象至少还...

Firefly-
昨天
18
0
vue 跨层组件通讯 provide inject

https://cn.vuejs.org/v2/api/#provide-inject 类型: provide:Object | () => Object inject:Array<string> | { [key: string]: string | Symbol | Object } 详细: provide 和 inject 主......

阿豪boy
昨天
14
0
黑马程序员面试宝典(Java)Beta6.0免费下载

场景 JavaSE基础 面向对象特征以及理解 访问权限修饰符区别 理解clone对象 JavaSE语法 java有没有goto语句 &和&&的区别 如何跳出当前的多重嵌套循环? 是否可以继承String? 重载与重写的区别...

badaoliumang
昨天
14
0
监控linux系统状态

查看系统负载: w/uptime 最后面三个数字表示1分钟,5分钟,15分钟平均有多少个进程占用CPU 占用CPU的进程可以是Running,也可以是Waiting 某一时刻1颗CPU只能有一个进程在使用其资源 #查看c...

asnfuy
昨天
16
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部