Memcache 怎么做集群啊


业务需要,搭建多个 memeched服务端,应该怎么设置呢

memcached php

万恶的百度 12 years ago

如果是PHP用来存放SESSION:

   
  ini_set('session.save_handler', 'memcache');
  
ini_set('session.save_path', 'tcp://192.168.0.1:10001?persistent=1&weight;=1&timeout;=1&retry;_interval=15,tcp://192.168.0.2:10002?persistent=1&weight;=1&timeout;=1&retry;_interval=15');

session_start();

如果是PHP存储和读取,参考以下类

   
  <?php
  
/**
* Memcache Class
*
* Config example
* $MEMCACHE_CONFIG = array(
* '192.168.0.1:10001:100',
* '192.168.0.2:10002:100'
* );
*
*/
class Cache
{
private $mCache = null;
static private $mInstance = array();
static public function Instance($cluster='default')
{
if ( isset( self::$mInstance[ $cluster ] ) )
return self::$mInstance[ $cluster ];
return self::$mInstance[ $cluster ] = new Cache($cluster);
}

static private function _CreateCacheInstance($cluster='default')
{
$cache_instance = new MemCache();
foreach( $GLOBALS['platform_config']['memcache_config'] AS $one )
{
$server = (string) $one;
list($ip, $port, $weight) = explode(':', $server);

$cache_instance->addServer( $ip
,$port
,true
,$weight
,1
,15
,true
,array('Cache','FailureCallback')
);
}
return $cache_instance;
}

private function __construct($cluster='default')
{
$this->mCache = self::_CreateCacheInstance( $cluster );
}

static public function FailureCallback($ip, $port)
{
//var_dump( "$ip:$port" );
return false;
}

function Get($key)
{
return $this->mCache->get($key);
}

function Add($key, $var, $flag=0, $expire=0)
{
return $this->mCache->add($key,$var,$flag,$expire);
}

function Dec($key, $value=1)
{
return $this->mCache->decrement($key, $value);
}

function Inc($key, $value=1)
{
return $this->mCache->increment($key, $value);
}

function Replace($key, $var, $flag=0, $expire=0)
{
return $this->mCache->replace($key, $var, $flag, $expire);
}

function Set($key, $var, $flag=0, $expire=0)
{
return $this->mCache->set($key, $var, $flag, $expire);
}

function Del($key, $timeout=0)
{
if (is_array($key))
{
foreach ($key as $k) $this->mCache->delete($k, $timeout);
} else {
$this->mCache->delete($key, $timeout);
}
}

function Flush()
{
return $this->mCache->flush();
}

function GetFunctionKey($callback, $args=array())
{
$args = ksort($args);
$patt = "/(=>)\s*'(\d+)'/";
$args_string = var_export($args, true);
$args_string = preg_replace($patt, "\\1\\2", $args_string);
$key = "[FUNC]:$callback($args_string)";
return md5( $key );
}

function GetObjectKey($tablename, $id)
{
$key = "[OBJ]:$tablename($id)";
return md5( $key );
}

function GetKey($string)
{
return md5( $string );
}
}
?>

蓬萊山輝月 answered 12 years ago

Your Answer