您的位置:首页 > 数据库 > Memcache

memcached、php-memcache安装笔记

2009-11-10 09:54 525 查看
这篇日志主要记录了我编译安装memcached、php-memcache的过程。

Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力。
它可以应对任意多个连接,使用非阻塞的网络IO。由于它的工作机制是在内存中开辟一块空间,然后建立一个HashTable,Memcached自管理这些HashTable。
Memcache官方网站:http://www.danga.com/memcached

到官方网站下载最新版本,编译安装之~
编译之前需要确保你的系统中有libevent及其开发包,如果没有的话…先安装吧。

1
2
3
4
5

tar xvf memcached-1.2.6.tar.gz
cd memcached-1.2.6
./configure --prefix=/usr/local/memcached
make
make install

安装好了以后,先建立memcached运行临时目录:

1
2

mkdir -pv /var/run/memcached
chown -R nobody:nobody /var/run/memcached

然后就可以启动memcached的守护进程了:

1

/usr/local/memcached/bin/memcached -p 11211 -l 172.16.236.150 -d -u nobody -P /var/run/memcached/memcached.pid -m 64M -c 1024

几个参数的解释:

-p memcached监听的TCP端口
-l 监听的ip地址,172.16.236.150是我服务器的IP地址,如果你需要多个服务器都能够读取这台memcached的缓存数据,那么就必须设定这个ip
-d 以daemon方式运行,将程序放入后台
-u memcached的运行用户,我设定的是nobody
-P memcached的pid文件路径
-m memcached可以使用的最大内存数量
-c memcached同时可以接受的最大的连接数

如果你希望以socket方式来访问memcached,那么在启动的时候就必须去掉 -l和-p参数,并加上-s参数:

-s memcached的socket文件路径

下面安装php的extension,你可以在php的pcel项目站点找到memcache的扩展:http://pecl.php.net/package/memcache,目前最新版本是3.0.3
注意了,在pcel站点上有两个关于memcached的php扩展,一个叫memcache,一个叫memcached,看介绍,这两个扩展的功能都是一样的,但是后者似乎是今年才开始的项目,综合考虑,我还说选用了前者,毕竟开发时间长,以后找文档应该也会方便得多。

下载php-memcache之后,编译安装:

1
2
3
4
56

tar xvf memcache-3.0.3.tgz
cd memcache-3.0.3
/usr/local/php5/bin/phpize
./configure --with-php-config=/usr/local/php5/bin/php-config --enable-memcache
make
make install

按照我的环境,编译出来的memcache.so保存在 /usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/ 目录下,如果你的环境不一样,你得根据自己情况修改你的php.ini了。

接着要做的工作就是让php加载这个扩展,编辑你的php.ini,在适当位置(通常是最后,也可以是独立的一个ini文件)加入如下行:

1

extension=memcache.so

然后重启你的phpfastcgi进程或者apache,运行一个phpinfo()来确认一下,正常的话你应该可以看到这个了:




现在,你已经可以在你的php程序中存取memcached中的缓存数据了,在php-memcache的文档中提供了相关的接口:

1
2
3
4
56
7
8
9
10
1112
13
14
15
16
17
18
19
20
2122
23
24
25
26
27
28
29
30
3132
33
34
35
36
37
38
39
40
4142
43
44
45
46
47
48
49
50
5152
53
54
55
56
57
58
59
60
6162
63
64
65
66
67
68
69
70
71

class MemcachePool() {
/**
*	连接到memcached
*/
bool connect(string host, int tcp_port = 11211, int udp_port = 0, bool persistent = true, int weight
= 1, int timeout = 1, int retry_interval = 15)

/**
*	增加一个memcached服务器
*/
bool addServer(string host, int tcp_port = 11211, int udp_port = 0, bool persistent = true, int weigh
t = 1, int timeout = 1, int retry_interval = 15, bool status = true)

/**
*	设置memcached参数(运行时的)
*/
bool setServerParams(string host, int tcp_port = 11211, int timeout = 1, int retry_interval = 15, boo
l status = true)

/**
* 读取memcached中的数据
*/
mixed get(mixed key, mixed &flags = null, mixed &cas = null)

/**
* 保存memcached数据,支持以数组方式的复合设置,例子:
*  $memcache->set(array('key1' => 'val1', 'key2' => 'val1'), null, 0, 60)
*/
bool add(mixed key, mixed var = null, int flag = 0, int exptime = 0)
bool set(mixed key, mixed var = null, int flag = 0, int exptime = 0)
bool replace(mixed key, mixed var = null, int flag = 0, int exptime = 0)

/**
* Compare-and-Swap, uses the CAS param from MemcachePool::get()
*/
bool cas(mixed key, mixed var = null, int flag = 0, int exptime = 0, int cas = 0)

/**
* Prepends/appends a value to an existing one
*/
bool append(mixed key, mixed var = null, int flag = 0, int exptime = 0)
bool prepend(mixed key, mixed var = null, int flag = 0, int exptime = 0)

/**
* 删除缓存数据
*  $memcache->delete(array('key1', 'key2'))
*/
bool delete(mixed key, int exptime = 0)

/**
* Supports multi-key operations, for example
*  $memcache->increment(array('key1', 'key2'), 1, 0, 0)
*
* The new defval (default value) and exptime (expiration time) are used
* if the key doesn't already exist. They must be supplied (even if 0) for
* this to be enabled.
*/
mixed increment(mixed key, int value = 1, int defval = 0, int exptime = 0)
mixed decrement(mixed key, int value = 1, int defval = 0, int exptime = 0)

/**
* Assigns a pool-specific failure callback which will be called when
* a request fails. May be null in order to disable callbacks. The callback
* receive arguments like
*
*  function mycallback($host, $tcp_port, $udp_port, $error, $errnum)
*
* 发生错误是的回调函数
*/
bool setFailureCallback(function callback)
}

比如,一段最典型的代码可能就是这样的:

1
2
3
4
56
7
8
9
10
1112

$obj	=	new MemcachePool();
$obj->addServer('172.16.236.150');
if ( $obj->get('id') ) {
echo 'Old value : '.$obj->get('id')	;
if ( isset($_GET['id'] ) {
$obj->set('id',$_GET['id']));
echo '<br />New value setted.';
}
}else {
$obj->add('id' , $_GET['id']);
}
unset($obj);

基本工作已经做完了,当然为了让memcached运行得更舒服、为了系统的安全性,你还可以写一个脚本把memcached加入系统启动进程,并设置一下iptables等等,这些不是本文重点,就略去不说了~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: