基于 libmemcahce 的memcache 操作
<?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 操作的更多相关文章
- memcached基于socket访问memcache缓存服务器
memcached基于socket访问memcache缓存服务器 操作memcache常用三种方法: .memcache基于php_memcache.dll扩展(php扩展) .memcached基于 ...
- 基于 php-redis 的redis操作
基于 php-redis 的redis操作 林涛 发表于:2016-5-13 12:12 分类:PHP 标签:php,php-redis,redis 203次 redis的操作很多的,下面的例子都是基 ...
- 一个基于PDO的数据库操作类(新) 一个PDO事务实例
<?php /* * 作者:胡睿 * 日期:2011/03/19 * 电邮:hooray0905@foxmail.com * * 20110319 * 常用数据库操作,如:增删改查,获取单条记录 ...
- memcache 操作类
<?php /** * memcache 操作实现 * @author timeless */ class Memcache_manage { //CI原始的信息 private $_ci; p ...
- AngularJS基于MVC的复杂操作案例
AngularJS基于MVC的复杂操作案例 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...
- 从基于 SQL 的 CURD 操作转移到基于语义 Web 的 CURD 操作
中文名称 CURD 含义 数据库技术中的缩写词 操作对象 一般的项目开发的各种参数 作用 用于处理数据的基本原子操作 它代表创建(Create).更新(Update).读取(Retrieve) ...
- 基于gtid复制主要操作记录
基于gtid复制主要操作记录 一.安装系统依赖包 在主从上都要安装该依赖包. yum -y install perl-DBI yum -y install perl-DBD-MySQL yum -y ...
- 基于iTextSharp的PDF操作(PDF打印,PDF下载)
基于iTextSharp的PDF操作(PDF打印,PDF下载) 准备 1. iTextSharp的简介 iTextSharp是一个移植于java平台的iText项目,被封装成c#的组件来用于C#生成P ...
- 基于 Redis 实现 CAS 操作
基于 Redis 实现 CAS 操作 Intro 在 .NET 里并发情况下我们可以使用 Interlocked.CompareExchange 来实现 CAS (Compare And Swap) ...
随机推荐
- NTP服务器
server 1.cn.pool.ntp.org server 1.asia.pool.ntp.org server 0.asia.pool.ntp.org pool.ntp.org
- AutoLayout--masonry使用
[label1 mas_makeConstraints:^(MASConstraintMaker *make) { //使左边间距为 make.left.equ ...
- Conversion between json and object using SBJson lib
Define two methods in an object class as follows: @interface MyObject : NSObject @property (nonatomi ...
- Pencil-一个开源免费的UI原型工具,自带ios和android模板
Pencil是一个开源免费的UI原型工具,自带ios和android模板,可以很方便的做mockup. 下图是一个官方展示的模板:
- APP安全环节缺失,手游运营商怎样应对APP破解困境
2013年手游行业的规模与收入均实现了大幅增长,发展势头强劲.然而,在手游快速发展的同一时候,因为监管.审核等方面存在着漏洞,手机游戏软件被破解后注入恶意代码.盗取用户財产.窃取用户设备信息的现象屡见 ...
- MST最小生成树及Prim普鲁姆算法
MST在前面学习了Kruskal算法,还有一种算法叫做Prim的.这两者的区别是Prim算法适合稠密图,比如说鸟巢这种几乎所有点都有相连的图.其时间复杂度为O(n^2),其时间复杂度与边的数目无关:而 ...
- rsyslog 详解3
http://blog.clanzx.net/2013/12/31/rsyslog.html http://www.centosabc.com/archives/601 http://www.cnbl ...
- iOS 复杂动画之抽丝剥茧
一.前言 随着开发者的增多和时间的累积,AppStore已经有非常多的应用了,每年都有很多新的APP产生.但是我们手机上留存的应用有限,所以如何吸引用户,成为产品设计的一项重要内容.其中炫酷的动画效果 ...
- laravel扩展包开发步骤总结
1. 创建包 php artisan workbench vendor/package --resources 注: vendor:开发商名 package:包名 2.修改下包里co ...
- LiLei&HanMeiMei的隐式马尔可夫爱情
一篇非常棒的隐马尔可夫入门文章...推荐! from: http://staffwww.dcs.shef.ac.uk/people/W.Liu/hmm.html