php memcached缓存集群
一、需求描述
一linode xen vps 1G RAM,40+sites,IO频繁,openfiles ulimit已65535
US时间访问量大增,nginx不定时502
二、解决办法
pages cache化,但这一台vps内存不够用,于是把闲置的几台服务器都装上memcached做缓存集群
再根据网络延迟设定权重
memcached server官方: http://memcached.org/
三、代码
访问memcached server需要php支持,可以选择memcached库和memcache库,注意2者名字的区别
详细对比:https://code.google.com/p/memcached/wiki/PHPClientComparison
我采用的是php memcache: http://php.net/manual/en/book.memcache.php 因为memcache不依赖libmemcached
|
1
2
3
4
5
6
7
8
9
10
11
|
//集群数组$memserver = array ( array ( 'host' => 'fast1.xxx.com' , 'port' => 13579 , 'weight' => 40 ), array ( 'host' => 'fast2.xxx.com' , 'port' => 13579 , 'weight' => 20 ), array ( 'host' => 'fast3.xxx.com' , 'port' => 13579 , 'weight' => 20 ), array ( 'host' => 'slow1.xxx.com' , 'port' => 24680 , 'weight' => 5 ), array ( 'host' => 'slow2.xxx.com' , 'port' => 24680 , 'weight' => 5 ), array ( 'host' => 'local.xxx.com' , 'port' => 11211 , 'weight' => 10 )); |
键值读、写、删就没什么说的了直接按文档sample写即可
考虑到以后的移植问题,记得首先检测环境是否支持memcache: class_exists(‘Memcache’)
下面是我的实现,因为特定需求,几个函数写的相对独立,memcache也没做全局实例化。
你可以根据自己需求把memcache在全局实例化,优化操作。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
//---get content from memcache---
function getCache( $key )
{
Global $memserver;
$ret = false;
$key = md5( $key );
$mc = new Memcache();
foreach ( $memserver as $s )
$mc->addServer( $s['host'], $s['port'], FALSE, $s['weight'] );
$ret = $mc->get( $key );
$mc->close();
unset( $mc );
return $ret;
}
//---set content from memcache---
function setCache( $key, $val )
{
Global $memserver;
$ret = false;
$key = md5( $key );
$mc = new Memcache();
foreach ( $memserver as $s )
$mc->addServer( $s['host'], $s['port'], FALSE, $s['weight'] );
if( isset($val['cached']) )
{
$val['cached'] = 1;
$ret = $mc->set( $key , $val);
}
$mc->close();
unset( $mc );
return $ret;
}
//---delete content from memcache---
function delCache( $key )
{
Global $memserver;
$ret = false;
$key = md5( $key );
$mc = new Memcache();
foreach ( $memserver as $s )
$mc->addServer( $s['host'], $s['port'], FALSE, $s['weight'] );
$ret = $mc->delete( $key , 0);
$mc->close();
unset( $mc );
return $ret;
}
|
php memcached缓存集群的更多相关文章
- Memcached【Magent+Memcached】集群
Memcached介绍 事件处理libevent是个程序库,它将Linux的epoll.BSD类操作系统的kqueue等事件处理功能封装成统一的接口.即使对服务器的连接数增加,也能发挥O(1)的性能 ...
- memcached构建集群分析之一
memcached本身是不支持集群的,集群所关注的容灾.容错.宕机恢复机制统统都没有,实战中需要自己实现容灾机制. memcached集群相比memcached的优势: 巨量数据分布到集群的多台应用主 ...
- Window平台搭建Redis分布式缓存集群 (一)server搭建及性能測试
百度定义:Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对很多其它.包含string(字符串).list(链表).set(集合).zset(sort ...
- Nginx+Tomcat+Memcached 实现集群部署时Session共享
Nginx+Tomcat+Memcached 实现集群部署时Session共享 一.简介 我们系统经常要保存用户登录信息,有Cookie和Session机制,Cookie客户端保存用户信息,Sessi ...
- Nginx+Memcached+Tomcat集群配置实践(Sticky Session)
准备工作 创建一个简单的web应用,名为session.其中有两个页面,分别如下所示: 页面login.jsp <%@ page language="java" conten ...
- 分布式缓存集群方案特性使用场景(Memcache/Redis(Twemproxy/Codis/Redis-cluster))优缺点对比及选型
分布式缓存集群方案特性使用场景(Memcache/Redis(Twemproxy/Codis/Redis-cluster))优缺点对比及选型 分布式缓存特性: 1) 高性能:当传统数据库面临大规模 ...
- Redis缓存集群方案
由于单台Redis服务器的内存管理能力有限,使用过大内存的Redis又会使得服务器的性能急剧下降,一旦服务器发生故障将会影响更大范围业务,而Redis 3.0 beta1支持的集群功能还不适合生产环境 ...
- Nginx+Memcached+Tomcat集群配置(MSM--win7 64bit)
本次主要是在win7 64 上演示操作. web应用构建 Memcached安装配置启动 Tomcat配置 所需jar包 memcached-session-manager 序列化 contextxm ...
- EhCache 分布式缓存/缓存集群
开发环境: System:Windows JavaEE Server:tomcat5.0.2.8.tomcat6 JavaSDK: jdk6+ IDE:eclipse.MyEclipse 6.6 开发 ...
随机推荐
- 如何为你的初创应用App开发公司建立战略计划(商业战略竞争五力学)
首先,什么是战略计划?战略计划可以定义为一个为了达到目标而需要执行的一系列动作步骤的计划. 根据当今全球第一战略权威,商业管理界公认的"竞争战略之父"Michael Porter著 ...
- AngularJS模块的详解
AngularJS模块的详解 在讲angularjs的模块之前,我们先介绍一下angular的一些知识点: AngularJS是纯客户端技术,完全用Javascript编写的.它使用的是网页开发的常规 ...
- 74LS183 加法器 【数字电路】
74LS183 搭的一个还有点意思的加法电路,串行进位的 2+6 == 8 大家都懂的哈哈
- leetcode第九题--Palindrome Number
Problem: Determine whether an integer is a palindrome. Do this without extra space. click to show sp ...
- 微软BI 之SSIS 系列 - 使用 Script Task 访问非 Windows 验证下的 SMTP 服务器发送邮件
原文:微软BI 之SSIS 系列 - 使用 Script Task 访问非 Windows 验证下的 SMTP 服务器发送邮件 开篇介绍 大多数情况下我们的 SSIS 包都会配置在 SQL Agent ...
- [转]Android与电脑局域网共享之:Samba Client
在上一篇文章中我提到如何在Android手机上建立Windows共享服务器,现在来说说一个反向的问题,就是,如何在Android手机上访问Windows计算机中的共享资源,当然,前提也是需要软件,这里 ...
- Tomcat源码学习一
这段时间工作不太忙,所以抽时间学习了TOMCAT, TOMCAT实际就是负责保持TCP连接传递到部署的项目中.浏览器实质就是TCP发送器.将用户的请求封装成TCP发送请求.当然格式是双方协定的.使用的 ...
- T4模版
T4模版 这讲适合所有的MVC程序 很荣幸,我们的系统有了体验的地址了.演示地址 之前我们发布了一个简单的代码生成器,其原理就是读取数据库的表结构,生成文本的一个方式来生成代码! 为了替代重复的劳动, ...
- SZU : A11 Sequence
Description We are given a integer sequence, your job is find the length of the longest contiguous s ...
- JS数量输入控件
JS数量输入控件 很早看到kissy首页 有数量输入控件,就随便看了下功能 感觉也不怎么难 所以也就试着自己也做了一个, 当然基本的功能和他们的一样,只是用了自己的编码思想来解决这么一个问题.特此给大 ...