分类: 服务器与存储
2010-04-06 16:00:43
一、安装Memcached及Magent(Ubuntu-9.04-Server)
先安装libevent在安装Memcached
wget ~provos/libevent- tar -xzvf libevent- cd libevent- ./configure --prefix=/usr/local/libevent make make install |
wget
tar -xzvf memcached- cd memcached- ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent make make install ln -s /usr/local/libevent/lib/libevent-1.4.so.2 /usr/lib/ |
安装Magent
mkdir magent cp magent-0.5.tar.gz magent cd magent tar -xzvf magent-0.5.tar.gz /sbin/ldconfig sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile vi magent.c 添加 #include make |
二、使用Memcached和Magent
1、Memcached参数解析及实例
-p -d
以守护进程方式运行Memcached -u -m -c -v 输出警告和错误信息 -vv 打印客户端的请求和返回信息 -h
打印帮助信息 -i 打印 memcached和 libevent 的版权信息 -l
设置监听的 IP 地址,如果是本机的话,通常可以不设置此参数 |
命令:/usr/local/memcached/bin/memcached -d -m 32 -l 192.168.1.233 -p 10000 -u administrator |
2、Magent参数解析及实例
-h 帮助文档 -u 用户ID -g 组ID -p 监听端口,默认为:11211 -s
ip:port, 设定Memcached服务器的ip地址和端口 -b
ip:port, 设定备有Memcached服务器的ip地址和端口 -l 本地绑定的ip地址 -n 最大连接数, 默认为4096 -D
don't go to background -k 使用ketama算法 -i 保持激活的连接数, 默认为 20 -v 输出警告和错误信息 |
命令:./magent -u administrator -n 51200 -l 192.168.1.239 -p 10000 -s 192.168.1.233:10000 -s 192.168.1.234:10000 -b 127.0.0.1:10001 |
三、详解Memcached使用
1.
Memcached对C语言的接口
1)
安装C/C++ Memcached客户端
开发库官网: 开发库下载: lib
memcached 库安装: # tar
zxvf libmemcached-0.25.tar.gz #cd
libmemcached-0.25 #./configure
--prefix=/usr # make # make install |
2)
简单应用
#include #include #include #include #include #define
MAMCACHED_MAX_KEY 100 #define
FILENAME "/usr/local/nginx/html/index.html" #define
CACHENAME "/index.html" #define
CACHELEN 11 long
fileSize(){ int fd = open(FILENAME, O_RDONLY); long size = lseek(fd, 0L, SEEK_END); close(fd); return size; }//end fileSize int main(int
argc, char *argv[]) { memcached_st *memc; memcached_return rc; memcached_server_st *servers; long fsize = fileSize(); char bytebuf[fsize]; FILE *fp; int i = 0; printf("%ld\n", fsize); //image2byte fp = fopen(FILENAME, "rb"); fread(bytebuf, 1, fsize, fp); fclose(fp); printf("%c\n", bytebuf[0]); printf("%ld\n",
sizeof(bytebuf)); //connect server memc = memcached_create(NULL); servers =
memcached_server_list_append(NULL, "localhost", 12000, &rc); rc = memcached_server_push(memc,
servers); memcached_server_free(servers); //save data printf("a\n"); printf("%c\n", bytebuf[0]); rc = memcached_set(memc, CACHENAME,
CACHELEN, bytebuf, sizeof(bytebuf), (time_t)0,
(uint32_t)0); if(rc == MEMCACHED_SUCCESS){ printf("Save key:key1
data:\"%s\"success.\n",bytebuf); }//fi int fd = open(FILENAME,
O_WRONLY|O_CREAT); write(fd, bytebuf, sizeof(bytebuf)); close(fd); //Fetch data char return_key[MAMCACHED_MAX_KEY]; size_t return_key_length; char *return_value; size_t return_value_length; const char *keys[] = {CACHENAME}; size_t key_length[] = {CACHELEN}; uint32_t flags; rc = memcached_mget(memc, keys,
key_length, 1); return_value = memcached_fetch(memc,
return_key, &return_key_length, &return_value_length,
&flags, &rc); if(rc == MEMCACHED_SUCCESS){ printf("Fetch key: %s
data: %s\n", return_key, return_value); }//FI //Delet data rc = memcached_delete(memc,
"key1", 4, (time_t)0); if(rc == MEMCACHED_SUCCESS){ printf("Delete
success\n"); }//fi //free memcached_free(memc); return 0; }//end main |
2.
Memcached对PHP语言的接口
1)
PHP对Memcached的扩展
Memcache扩展安装: # tar
zxvf memcache-2.2.4.tgz #
memcache-2.2.4 #
/usr/local/php/bin/phpize #./configure
--with-php-config=/usr/local/php5/bin/php-config #make # make install |
配置配置 # ls -l
/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/memcache.so # vim
/usr/local/php/lib/php.ini 新增配置内容: extension_dir
= "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/" extension = memcache.so extension = memcache.so |
2)
Memcached提供的PHP接口
Memcache::add — 添加一个值,如果已经存在,则返回false |
3)
简单应用
//连接Memcached $mem = new Memcache;
$mem->connect("192.168.1.219", 12000); $mem->set("key1", 'This
is first value', 0, 60); $val = $mem->get('key1'); if($val == NULL) echo
"hello"," echo "Get key1
value:".$val, " //删除数据 $mem->delete('key1'); //关闭连接 $mem->close(); ?> |