一,为什么要使用memcached
最优秀的cache软件,有效减轻数据库服务器的压力
不用它还要用哪个?
二,下载
从何处下载?
libevent从何处下载?
~provos/libevent/
php的memcache模块下载:
三,如何进行安装libevent?
tar -zxvf libevent-1.3e.tar.gz
./configure --prefix=/usr
make
make install
四,检查libevent安装是否成功?
[root@datam libevent-1.3e]# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 22 Oct 6 00:20 libevent-1.1a.so.1 -> libevent-1.1a.so.1.0.2
-rwxr-xr-x 1 root root 31596 Jan 7 2007 libevent-1.1a.so.1.0.2
lrwxrwxrwx 1 root root 22 Oct 6 00:36 libevent-1.3e.so.1 -> libevent-1.3e.so.1.0.3
-rwxr-xr-x 1 root root 338439 Oct 6 00:36 libevent-1.3e.so.1.0.3
-rw-r--r-- 1 root root 533978 Oct 6 00:36 libevent.a
-rwxr-xr-x 1 root root 828 Oct 6 00:36 libevent.la
lrwxrwxrwx 1 root root 22 Oct 6 00:36 libevent.so -> libevent-1.3e.so.1.0.3
五,安装memcached
tar -zxvf memcached-1.2.2.tar.gz
安装memcached时,需要指定libevent的安装位置:
./configure --with-libevent=/usr --enable-threads
# make
# make install
六,检查memcached安装是否成功?
1看文件
[root@datam memcached-1.2.2]# ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 141771 Oct 6 00:41 /usr/local/bin/memcached
-rwxr-xr-x 1 root root 151317 Oct 6 00:41 /usr/local/bin/memcached-debug
2,测试运行
[root@datam memcached-1.2.2]# /usr/local/bin/memcached -h
/usr/local/bin/memcached: error while loading shared libraries: libevent-1.3e.so.1: cannot open shared object file: No such file or directory
出错了,怎么办?
进行检查
[root@datam memcached-1.2.2]# LD_DEBUG=libs memcached -v
4962: find library=libevent-1.3e.so.1 [0]; searching
4962: search cache=/etc/ld.so.cache
4962: search path=/lib64/tls/x86_64:/lib64/tls:/lib64/x86_64:/lib64:/usr/lib64/tls/x86_64:/usr/lib64/tls:/usr/lib64/x86_64:/usr/lib64 (system search path)
4962: trying file=/lib64/tls/x86_64/libevent-1.3e.so.1
4962: trying file=/lib64/tls/libevent-1.3e.so.1
4962: trying file=/lib64/x86_64/libevent-1.3e.so.1
4962: trying file=/lib64/libevent-1.3e.so.1
4962: trying file=/usr/lib64/tls/x86_64/libevent-1.3e.so.1
4962: trying file=/usr/lib64/tls/libevent-1.3e.so.1
4962: trying file=/usr/lib64/x86_64/libevent-1.3e.so.1
4962: trying file=/usr/lib64/libevent-1.3e.so.1
4962:
memcached: error while loading shared libraries: libevent-1.3e.so.1: cannot open shared object file: No such file or directory
进行处理:
[root@datam memcached-1.2.2]# ln -s /usr/lib/libevent-1.3e.so.1 /usr/lib64/libevent-1.3e.so.1
[root@datam memcached-1.2.2]# memcached -h
memcached 1.2.2
-p
TCP port number to listen on (default: 11211)
-U UDP port number to listen on (default: 0, off)
-s unix socket path to listen on (disables network support)
-l interface to listen on, default is INDRR_ANY
-d run as a daemon
-r maximize core file limit
-u assume identity of (only when run as root)
-m max memory to use for items in megabytes, default is 64 MB
-M return error on memory exhausted (rather than removing items)
-c max simultaneous connections, default is 1024
-k lock down all paged memory
-v verbose (print errors/warnings while in event loop)
-vv very verbose (also print client commands/reponses)
-h print this help and exit
-i print memcached and libevent license
-b run a managed instanced (mnemonic: buckets)
-P save PID in , only used with -d option
-f chunk size growth factor, default 1.25
-n minimum space allocated for key+value+flags, default 48
至此安装完毕
七,如何启动memcached的服务端?
下面,我们来启动一个Memcached的服务器端:
# /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 -p 12000 -c 256 -P /tmp/memcached.pid
/usr/local/bin/memcached -d -m 128 -l localhost -p 11211 -u root
-d 以守护程序(daemon)方式运行 memcached;
-m 设置 memcached 可以使用的内存大小,单位为 M;
-l 设置监听的 IP 地址,如果是本机的话,通常可以不设置此参数;
-p 设置监听的端口,默认为 11211,所以也可以不设置此参数;
-u 指定用户;
-t number of threads to use, default 4
如果有此项,说明已经支持了线程,就可以在启动的时候使用 -t 选项来启动多线程
然后启动的时候必须加上你需要支持的线程数量:
/usr/local/memcache/bin/memcached -t 1024
[root@datam memcached-1.2.2]# /usr/local/bin/memcached -d -m 128 -l localhost -p 11211 -u root
Illegal address: localhost
[root@datam memcached-1.2.2]# /usr/local/bin/memcached -d -m 128 -l 127.0.0.1 -p 11211 -u root
[root@datam memcached-1.2.2]#
至此启动成功
八,memcached的相关知识?
如何停止?如何重启?如何刷新其内容?等等
使用这个文件来实现:
#! /bin/sh
#
# chkconfig: - 55 45
# description: The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/sysconfig/memcached
# Source function library.
. /etc/rc.d/init.d/functions
PORT1=11211
USER=nobody
MAXCONN=1024
CACHESIZE=64
OPTIONS="-t 8"
MEMDAEMON=/usr/local/bin/memcached
if [ -f /etc/sysconfig/memcached ];then
. /etc/sysconfig/memcached
fi
# Check that networking is up.
if [ "$NETWORKING" = "no" ]
then
exit 0
fi
RETVAL=0
prog="memcached"
start () {
echo -n $"Starting $prog: "
# insure that /var/run/memcached has proper permissions
#chown $USER /var/run/memcached
daemon $MEMDAEMON -d -p $PORT1 -u $USER -m $CACHESIZE -c $MAXCONN $OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached
}
stop () {
echo -n $"Stopping $prog: "
killproc memcached
RETVAL=$?
echo
if [ $RETVAL -eq 0 ] ; then
rm -f /var/lock/subsys/memcached
rm -f /var/run/memcached.pid
fi
}
restart () {
stop
start
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status memcached
;;
restart|reload)
restart
;;
condrestart)
[ -f /var/lock/subsys/memcached ] && restart || :
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}"
exit 1
esac
exit $?
九:配置php能访问memcached
wget
tar -zxvf memcache-2.2.0.tgz
[root@datam memcache-2.2.0]# /usr/local/bin/phpize
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20050922
Zend Extension Api No: 220051025
Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF
environment variable is set correctly and then rerun this script.
没有autoconf
yum install autoconf
[root@datam memcache-2.2.0]# yum install autoconf
[root@datam memcache-2.2.0]# /usr/local/bin/phpize
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20050922
Zend Extension Api No: 220051025
[root@datam memcache-2.2.0]# ./configure --enable-memcache --with-php-config=/usr/local/bin/php-config --with-zlib-dir
make
[root@datam memcache-2.2.0]# make install
Installing shared extensions: /usr/local/lib/php/extensions/no-debug-non-zts-20050922/
至此安装成功
下面我们需把此安装成功的模块添加到php中去
vi php.ini
add下面的这两行
extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20050922/"
extension=memcache.so
然后重启apache
用phpinfo()看到存在以下信息,则表示模块安装成功
memcache support |
enabled |
Active persistent connections |
0 |
Revision |
$Revision: 1.86 $ |
Directive |
Local Value |
Master Value |
memcache.allow_failover |
1 |
1 |
memcache.chunk_size |
8192 |
8192 |
memcache.default_port |
11211 |
11211 |
memcache.hash_function |
crc32 |
crc32 |
memcache.hash_strategy |
standard |
standard |
memcache.max_failover_attempts |
20 |
20 |
十:编写php代码测试访问memcached
[ 接口介绍 ]
服 务器端和客户端都安装配置好了,现在我们就来测试以下我们的成果。Memcache客户端包含两组接口,一组是面向过程的接口,一组是面向对象的接口,具 体可以参考PHP手册 “LXXV. Memcache Functions” 这章。我们为了简单方便,就使用面向对象的方式,也便于维护和编写代码。Memcache面向对象的常用接口包括:
Memcache::connect -- 打开一个到Memcache的连接
Memcache::pconnect -- 打开一个到Memcache的长连接
Memcache::close -- 关闭一个Memcache的连接
Memcache::set -- 保存数据到Memcache服务器上
Memcache::get -- 提取一个保存在Memcache服务器上的数据
Memcache::replace -- 替换一个已经存在Memcache服务器上的项目(功能类似Memcache::set)
Memcache::delete -- 从Memcache服务器上删除一个保存的项目
Memcache::flush -- 刷新所有Memcache服务器上保存的项目(类似于删除所有的保存的项目)
Memcache::getStats -- 获取当前Memcache服务器运行的状态
示例程序:
php
//连接
$mem = new Memcache;
$mem->connect("127.0.0.1", 11211);
//保存数据
$mem->set('key1', 'This is first value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val ."
";
//替换数据
$mem->replace('key1', 'This is replace value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "
";
//保存数组
$arr = array('aaa', 'bbb', 'ccc', 'ddd');
$mem->set('key2', $arr, 0, 60);
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "
";
//删除数据
$mem->delete('key1');
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "
";
//清除所有数据
$mem->flush();
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "
";
//关闭连接
$mem->close();
?>
如果正常的话,浏览器将输出:
Get key1 value: This is first value
Get key1 value: This is replace value
Get key2 value: Array ( [0] => aaa [1] => bbb [2] => ccc [3] => ddd )
Get key1 value:
Get key2 value:
十一:测试memcached缓存数据库的查询结果
/*
测试memcache缓存mysql的查询结果
by hd
2007.10.06
*/
//定义访问数据库的常量
define(SERVER_DATA_HOST,"127.0.0.1");
define(SERVER_DATA_USER,"root");
define(SERVER_DATA_PASS,"");
define(SERVER_DATA_BASE,"liuyan");
//定义访问数据库的常量
define(SERVER_MEM_HOST,"127.0.0.1");
define(SERVER_MEM_PORT,"11211");
//建立到数据库的连接
$dblink=mysql_connect(SERVER_DATA_HOST,SERVER_DATA_USER,SERVER_DATA_PASS);
mysql_select_db(SERVER_DATA_BASE,$dblink);
//定义到缓存的链接
$memlink = memcache_connect(SERVER_MEM_HOST,SERVER_MEM_PORT);
//定义访问数据库的函数
function sqlres($sqlstr)
{
global $dblink;
global $memlink;
$memstr = md5($sqlstr);
$memres = array();
if(!$memlink->get($memstr))
{
$dbres = mysql_query($sqlstr,$dblink);
$arrdata=array();
while ($dbrow = mysql_fetch_assoc($dbres))
{
$arrdata[]=$dbrow;
}
$memlink->set($memstr,$arrdata);
}
else
{
$memres = $memlink->get($memstr);
}
return $memres;
}
$sqlstr="select * from rank";
$arrdata=sqlres($sqlstr);
print_r($arrdata);
?>
十一:需要注意的问题
十一:封装一个访问memcached的类供调用
十一: