use memcached and php-memcache(d)

For memcached 1.2 to 1.3, protocol changed for delete(). For memcached 1.4, the wrong format will cause fatal error:
failed with: CLIENT_ERROR bad command line format.
So php pecl-memcache will get failed with the new version.

for php pecl-memcached, because it’s using libmemcache, so no this issue.
We write a function to choose which memcache extension we will use in our code:

$mem_flag = 0;
$mem_conn = null;

function mcached_conn()
{
	global $config, $mem_flag, $mem_conn;
	if($mem_flag != 1)
	{
		if ($config['mcache']['version']==0){
		$mem_conn = memcache_pconnect($config['mcache']['host'], 11211);
		} else {
			$mem_conn = new Memcached();
			$mem_conn->addServer($config['mcache']['host'], 11211);
		}
		$mem_flag = 1;
	}
	return $mem_conn;
}

function memcache_version_set($memcache_obj, $key,$value,$flag,$ttl){
	global $config;
	if ($config['mcache']['version']==0){
		return memcache_set($memcache_obj, $key,$value,$flag,$ttl);
	} else {
		return $memcache_obj->set($key,$value,$ttl);
	}
}
	
function memcache_version_get($memcache_obj, $key){
	global $config;
	if ($config['mcache']['version']==0){
		return memcache_get($memcache_obj, $key);
	} else {
		return $memcache_obj->get($key);
	}
}

function memcache_version_del($memcache_obj, $key){
	global $config;
	if ($config['mcache']['version']==0){
		return memcache_delete($memcache_obj, $key);
	} else {
		return $memcache_obj->delete($key);
	}
}


转载于:https://www.cnblogs.com/jiunadianshi/articles/1999855.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值