- <?php
- function test($key='name'){
- $md5 = substr(md5($key), 0, 8);
- $seed = 31;
- $hash = 0;
- for($i=0; $i<8; $i++){
- $hash = $hash * $seed + ord($md5[$i]);
- }
- return $hash & 0x7FFFFFFF;
- }
- $mamcacheList = array(
- array('host'=>'192.168.1.2', 'port'=>6379),
- array('host'=>'192.168.1.3', 'port'=>6379),
- array('host'=>'192.168.1.4', 'port'=>6379),
- array('host'=>'192.168.1.5', 'port'=>6379),
- );
- $key = 'username';
- $value = 'lane';
- //根据KEY获取hash
- $hash = $this->test($key);
- $count = count($mamcacheList);
- $mamcache = $mamcacheList[$hash % $count];
- $mc = new Mamcached($mamcache);
- $mc->set($key, $value);
- ?>
- //该片段来自于http://www.codesnippet.cn/detail/190620149816.html
来源: http://www.codesnippet.cn/detail/190620149816.html