<?php
echo '<pre>'; //测试的键值的数量
$count = 30;
$mem = create_memcache();
//var_dump($mem->isPersistent());
//var_dump($mem->isPristine());
//flush_data($mem);
//test_consistent_hash_set($mem, $count);
//test_consistent_hash_get($mem, $count);
//var_dump($mem->getServerList());
//var_dump($mem->getStats());
//检测memcache 权重匹配实现
//get_server_key_statistic(); function get_server_key_statistic() {
//memcahed 分布式
//测试 memcache 一致性hash 一台 memcahed 服务器宕机之后其他的键值存储问题还有添加一台服务器之后对之前的数据影响。
$stas = array();
for ($i = 1; $i <= 30; $i++) {
$key = 'item' . $i;
$arr = $mem->getServerByKey($key);
$port = $arr['port'];
if (!isset($stas[$port])) {
$stas[$port] = 1;
} else {
$stas[$port] = ++$stas[$port];
}
$server_key = $port . 'server_key';
$stas[$server_key][] = $key;
}
print_r($stas);
} /**
* 生成么memcache 对象
*/
function create_memcache() {
$mem = new Memcached('demo');
$server = array(
array('127.0.0.1', 11211, 20),
array('127.0.0.1', 11212, 20),
array('127.0.0.1', 11213, 20),
array('127.0.0.1', 11214, 20),
array('127.0.0.1', 11215, 20),
);
if (!count($mem->getServerList())) {
$mem->addServers($server);
//Socket reading timeout, in microseconds. In cases where you cannot use non-blocking I/O this will allow you to still have timeouts on the reading of data.
$mem->setOption(Memcached::OPT_RECV_TIMEOUT, 1000);
$mem->setOption(Memcached::OPT_SEND_TIMEOUT, 1000);
$mem->setOption(Memcached::OPT_TCP_NODELAY, true);
$mem->setOption(Memcached::OPT_SERVER_FAILURE_LIMIT, 50);
$mem->setOption(Memcached::OPT_CONNECT_TIMEOUT, 500);
$mem->setOption(Memcached::OPT_RETRY_TIMEOUT, 300);
$mem->setOption(Memcached::OPT_REMOVE_FAILED_SERVERS, true);
$mem->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
$mem->setOption(Memcached::OPT_HASH, Memcached::HASH_CRC);
$mem->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
}
return $mem;
} /**
* 测试一致性hash
*/
function test_consistent_hash_set($mem, $count) {
//memcahed 分布式
//测试 memcache 一致性hash 一台 memcahed 服务器宕机之后其他的键值存储问题还有添加一台服务器之后对之前的数据影响。
$stats = array();
for ($i = 1; $i <= $count; $i++) {
$server_key = 'item' . $i;
$arr = $mem->getServerByKey($server_key);
$val = create_rand_str(6);
$key = $i;
$mem->setByKey($server_key, $key, $val);
$port = $arr['port'];
if (!isset($stats[$port])) {
$stats[$port] = 1;
} else {
$stats[$port] = ++$stats[$port];
}
$server_key_val = $port . 'server_info';
$val_key_val = $port . 'val_info';
$stats[$server_key_val][] = $server_key;
$md5_key = md5($key);
$stats[$val_key_val][] = "{$md5_key}=>{$val}";
}
print_r($stats);
} function create_rand_str($length) {
$randstr = '';
for ($i = 0; $i < $length; $i++) {
$randstr .= chr(mt_rand(33, 126));
}
return $randstr;
} /**
* 测试memcache get
*/
function test_consistent_hash_get($mem, $count) {
for ($i = 1; $i <= $count; $i++) {
$server_key = 'item' . $i;
$key = $i;
$result = $mem->getByKey($server_key, $key);
$server_info = $mem->getServerByKey($server_key);
var_dump($server_info);
exit;
$port = $server_info['port'];
$weight = $server_info['weight'];
if ($result !== FALSE) {
echo "server info : 服务器端口:{$port} 权重:{$weight}, {$server_key}——{$key}=>{$result}<br>";
}
}
} /**
* 清除数据
*/
function flush_data($mem) {
$mem->flush();
}

基于 libmemcahce 的memcache 操作的更多相关文章

  1. memcached基于socket访问memcache缓存服务器

    memcached基于socket访问memcache缓存服务器 操作memcache常用三种方法: .memcache基于php_memcache.dll扩展(php扩展) .memcached基于 ...

  2. 基于 php-redis 的redis操作

    基于 php-redis 的redis操作 林涛 发表于:2016-5-13 12:12 分类:PHP 标签:php,php-redis,redis 203次 redis的操作很多的,下面的例子都是基 ...

  3. 一个基于PDO的数据库操作类(新) 一个PDO事务实例

    <?php /* * 作者:胡睿 * 日期:2011/03/19 * 电邮:hooray0905@foxmail.com * * 20110319 * 常用数据库操作,如:增删改查,获取单条记录 ...

  4. memcache 操作类

    <?php /** * memcache 操作实现 * @author timeless */ class Memcache_manage { //CI原始的信息 private $_ci; p ...

  5. AngularJS基于MVC的复杂操作案例

    AngularJS基于MVC的复杂操作案例 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...

  6. 从基于 SQL 的 CURD 操作转移到基于语义 Web 的 CURD 操作

    中文名称 CURD 含义 数据库技术中的缩写词 操作对象 一般的项目开发的各种参数 作用 用于处理数据的基本原子操作     它代表创建(Create).更新(Update).读取(Retrieve) ...

  7. 基于gtid复制主要操作记录

    基于gtid复制主要操作记录 一.安装系统依赖包 在主从上都要安装该依赖包. yum -y install perl-DBI yum -y install perl-DBD-MySQL yum -y ...

  8. 基于iTextSharp的PDF操作(PDF打印,PDF下载)

    基于iTextSharp的PDF操作(PDF打印,PDF下载) 准备 1. iTextSharp的简介 iTextSharp是一个移植于java平台的iText项目,被封装成c#的组件来用于C#生成P ...

  9. 基于 Redis 实现 CAS 操作

    基于 Redis 实现 CAS 操作 Intro 在 .NET 里并发情况下我们可以使用 Interlocked.CompareExchange 来实现 CAS (Compare And Swap) ...

随机推荐

  1. LaTeX中参考文献管理以及注意事项

    注意:版权所有,转载请注明出处 使用LaTeX写作,很重要的一个部分就是参考文献的制作,LaTeX使用bibtex管理文章的参考文献,bibtex即是一种工具,又是一种格式的代表,它要求使用*.bib ...

  2. microsoft visual studio 不能逐句执行?

    最近在使用vs2013过程中,vs2013不能逐行执行,在不设断点的情况下,根本停不下来按一下F11,整个程序就执行完了.百度了很久也没找到方法(看的还不够仔细),卸载安装了很多次(看我够笨的吧),最 ...

  3. flexpaper 与js 交互

    flash 代码//写到要响应的方法体中import flash.external.ExternalInterface;ExternalInterface.call("alert" ...

  4. poj 2752 Seek the Name, Seek the Fame【KMP算法分析记录】【求前后缀相同的子串的长度】

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14106   Ac ...

  5. iOS 应用程序本地化

    由于iPhone,iPad等苹果产品在全世界范围内的广泛流行,那么通过App Store下载应用程序的用户也将是来自世界范围的人们,所以开发者在开发过程中势必要考虑到不同语言环境下用户使用,好在iOS ...

  6. DOM方法

    DOM常用方法: 方法 描述 getElementById() 返回带有指定 ID 的元素. getElementsByTagName() 返回包含带有指定标签名称的所有元素的节点列表(集合/节点数组 ...

  7. 405. Convert a Number to Hexadecimal

    ..感觉做的很蠢. 主要就是看负数怎么处理. 举个例子,比如8位: 0111 1111 = 127 1111 1111 = -1 1000 0000 = -128 正常情况1111 1111应该是25 ...

  8. Windows - 子系统(subsystem)错误

    Windows - 子系统(subsystem)错误 本文地址: http://blog.csdn.net/caroline_wendy VS2012生成错误: "error LNK2019 ...

  9. Python程序的执行原理(转载)

    Python程序的执行原理 2013-09-17 10:35 佚名 tech.uc  1. 过程概述 Python先把代码(.py文件)编译成字节码,交给字节码虚拟机,然后虚拟机一条一条执行字节码指令 ...

  10. IAAS云计算产品畅想-云主机产品内涵

    这里所涉及的主要还是狭义的云主机产品. 主要还是谈云主机产品中公有云产品与私有云产品相比赋予更多的含义: 产品广义理解:公有云主机的最大特点就是基础资源按需支付 从这一句话中可以体现出来两个含义: 产 ...