<?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. 3 kafka介绍

     本博文的主要内容有 .kafka的官网介绍 http://kafka.apache.org/ 来,用官网上的教程,快速入门. http://kafka.apache.org/documentatio ...

  2. [转]C++ map容器用法

    C++ map的基本操作和使用 原文地址:http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html Map是c++的一个标准容器,她提供了很好一对一的关 ...

  3. request和response的中文乱码问题

    request乱码指的是:浏览器向服务器发送的请求参数中包含中文字符,服务器获取到的请求参数的值是乱码: response乱码指的是:服务器向浏览器发送的数据包含中文字符,浏览器中显示的是乱码:   ...

  4. 【转】HTML5游戏开发经典视频教程、电子书汇总

    HTML5游戏开发经典视频教程.电子书汇总 HTML5是用于取代1999年所制定的 HTML 4.01 和 XHTML 1.0 标准的 HTML 标准版本,现在仍处于发展阶段,但大部分浏览器已经支持某 ...

  5. 语音控制的tab选项卡

    前端开发whqet,csdn,王海庆,whqet,前端开发专家 ladies and 乡亲们,程序猿同志们,周末仍然坚守工作岗位,或者学习不辍的童鞋们,福音来了. 语音识别高不高端.难不难? 今天给大 ...

  6. [RxJS] Combination operator: zip

    CombineLatest and withLatestFrom are both AND-style combination operators. In this lesson, we will l ...

  7. 廖雪锋笔记1---python变量类型

    整型:a/b a//b a%b 浮点型:.2 字符串: "" '' r"" r'' '''...''' r'''...'''' 变量值共享:写时复制 NULL型 ...

  8. Cocos2d-x游戏开发中的消息机制:CCNotificationCenter的使用

    在HTML5游戏开发中,js可以使用Event对象的addEventListener(添加事件监听).dispatchEvent(触发事件)实现监听机制,如果在coocos2d-x中,去实现这种机制该 ...

  9. (原创)googlemap开发(一)

    听说我们的客户有了外国淫,所以领导问我目前的项目里高德地图和讯飞语音支持英文和英文发音不,按照我以往的经验判断,讯飞支持英语发音和识别英语是没有问题的,但是高德这玩意貌似只有我大天朝的地图吧.于是,找 ...

  10. eclipse下将普通的java工程转换成web工程

    开发过程中需要对普通的java工程转换成动态的web工程,网络上查询了资料很简单的几步操作就可以搞定,操作步骤如下: 编辑.project 修改以下配置 <nature>org.eclip ...