php之memcache,memcached

1 篇文章 0 订阅
/*************************** memcache start *****************************************/
$memcache = new Memcache;
// $memcache_obj = memcache_pconnect('memcache_host', 11211);
if ($memcache->getVersion() === false) {
    throw new Exception('Please, verify the Memcache configuration!');
}
$memcache_obj->connect('memcache_host', 11211);

$memcache_obj->add('var_key', 'test variable'[, false[, 30]]);
$var = $memcache_obj->get('some_key');
$memcache_obj->set('var_key', 'some really big variable', MEMCACHE_COMPRESSED, 50);
$memcache_obj->setCompressThreshold(20000[, 0.2]);   // 超过多少时,自动压缩。0.2表示压缩率,默认值。
$memcache_obj->replace("test_key", "some variable", false, 30);

$new_value = $memcache_obj->decrement('test_item', 3);
$new_value = $memcache_obj->increment('counter', 3);
$memcache_obj->delete('key_to_delete');

$memcache_obj->getStats();
echo $memcache->getServerStatus('memcache_host', 11211);    //online:int(1); offline:int(0);
$stats = $memcache_obj->getExtendedStats();    //returns a two-dimensional associative array with server statistics

$memcache->addServer('memcache_host', 11211);
$memcache->setServerParams('memcache_host', 11211, 1, 15, true, '_callback_memcache_failure');
// Changes server parameters and status at runtime.

$memcache_obj->flush();    //使缓存全部失效,并没有删除缓存,等下次调用的时候就删除,缓存过期同样处理
$memcache_obj->close();
/*************************** memcache end *****************************************/


/*************************** memcached start *****************************************/
/*************************** memcached end *****************************************/

普通hash分布

function myHash($key){
    $md5 = substr(md5($key), 0, 8);
    $seed = 31;
    $hash = 0;
    for($i = 0; $i < 8; $i ++){
        $hash = $hash * $seed + ord($md5{$i});
        $i ++;
    }
    return $hash & 0x7FFFFFFF;
}

<?php
$servers = array(
    array('192.168.1.1', '11212'),
    array('192.168.40.131', '11211'),
);
$key = 'myKey';
$value = 'myValue';
$toServer = $servers[myHash($key) % 2];

$memcached = new Memcached();
$memcached->addServer($toServer[0], $toServer[1]);
$memcached -> set($key, $value);
echo $memcached -> get($key);

一致性hash分布

<?php
class FlexiHash{
    private $aServers = array();
    private $isSorted = FALSE;

    function addServer($server){
        $hash = myHash($server);
        if(! isset($this -> aServers[$hash])){
            $this -> aServers[$hash] = $server;
        }
        $this -> isSorted = FALSE;
        return true;
    }

    function removeServer($server){
        $hash = myHash($server);
        if(isset($this -> aServers[$hash])){
            unset($this -> aServers[$hash]);
        }
        $this -> isSorted = FALSE;
        return true;
    }

    function lookup($key){    //找到合适的服务器存放 $key
        $hash = myHash($key);
        if(! $this -> isSorted){
            krsort($this -> aServers, SORT_NUMERIC);
            $this -> isSorted = TRUE;
        }
        foreach($this -> aServers as $pos => $server){
            if($hash >= $pos){
                return $server;
            }
        }
        return $this -> aServers[count($this -> aServers) - 1];
    }
}

/** test start **/
$oFlexiHash = new FlexiHash();
$oFlexiHash->addServer('localhost');
echo 'save key1 to'.$oFlexiHash->lookup('key1');
$oFlexiHash->addServer('www.moguwu.com');
echo 'save key1 to'.$oFlexiHash->lookup('key1');
echo 'save key2 to'.$oFlexiHash->lookup('www.moguwu.com');

php-memcached

get(‘mykey’)会在你的服务器群组中找出mykey的值返回。

如果getByKey(‘str’,’mykey’),意思是针对hash(‘str’)这个服务器去找出key的值,很有可能不在这个服务器中,所以可能返回false。

猜想当你set时候,系统会根据本身的算法(以key为参数)找出一个服务器存放你的值。取出会按照同样算法取出。setByKey同理,只不过它是根据第一个参数去算出存储的服务器。所以他们的效率是一样的。

那么*ByKey有什么效果呢。就像官方手册说的那样,就是把你所认为有关联的存到同一个服务器。例如

$mem->setByKey('JackMessage','Name','Jack',86400);
$mem->setByKey('JackMessage','Age','18',86400);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值