memcached 就是这么简单
memcached以key:value方式存储数据的服务器
以URI地址作为key,以实际页面作为value,缓存http对象数据
memcached: 缓存服务器,但本身无法决定缓存任何数据 ;非持久性
一半依赖于客户端(决定缓存哪些数据,缓存时长,key)
一半依赖于服务器(提供存储缓存数据的能力)
清除缓存数据的机制:
lazy:惰性,只要还有剩余缓存空间可用,之前缓存的数据不会自动清除
LRU:最近最少使用
非持久性服务器
缓存数据保存到内存中
48bytes最小
1M最大
避免内存碎片
buddy system:伙伴系统
避免内存外碎片(内存页面间的碎片)
将页面间的碎片重新组合成一个连续空间使用
slab allocator: slab分配器
存储小于内存单个页面大小(4K)的数据时,内存事先分配好符合各种小数据的空间使用
避免内存内碎片
slab class
memcached分配的内存空间大小的类别(48bytes的,80bytes的)
slab chunk
每个类别中分割的内存块的大小
memcached:不通信的分布式缓存服务器
memcached安装配置
安装libevent,用于提供基于事件驱动的API
# tar zxf libevent-2.0.20-stable.tar.gz
# cd libevent-2.0.20-stable
# ./configure --prefix=/usr/local/libevent
# make && make install
安装memcached
# yuminstall -y cyrus-sasl-devel
# tar zxf memcached-1.4.15.tar.gz
# cd memcached-1.4.15
# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/ --enable-sasl
# make && make install
memcached启动选项:
-p :指定监听在TCP协议的端口,默认为11211
-U :指定监听在UDP协议的端口,默认为11211;为0表示关闭监听UDP协议
-s :指定监听在本地套接字
-l :指定监听的地址
-d:以daemon服务方式运行
-u :指定运行的用户
-m :以MB为单位,指定memcached使用的最大内存空间,默认为64M
-t :指定用于处理入站请求的最大线程数,仅在memcached编译时开启支持线程才有效
-f : 指定slab allocator定义预先分配内存空间大小固定的块时的增长因子
-n :指定最小的slab chunk大小,单位是字节 默认为48bytes
-P :指定PID文件
-S:启用SASL认证功能
-c : 最大并发连接数据,默认为1024
启动memcached进程
# /usr/local/memcached/bin/memcached -m 128 -n 20 -f 1.1 -d -u nobody
# netstat -tunlp | grep memcached
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 6979/memcached
tcp 0 0 :::11211 :::* LISTEN 6979/memcached
udp 0 0 0.0.0.0:11211 0.0.0.0:* 6979/memcached
udp 0 0 :::11211 :::* 6979/memcached
memcached服务启动脚本:
#!/bin/bash
#
# Init file for memcached
#
# chkconfig: - 86 14
# description: Distributed memory caching daemon
#
# processname: memcached
# config: /etc/sysconfig/memcached
. /etc/rc.d/init.d/functions
## Default variables
PORT="11211"
USER="nobody"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
RETVAL=0
prog="/usr/local/memcached/bin/memcached"
desc="Distributed memory caching"
lockfile="/var/lock/subsys/memcached"
start() {
echo -n $"Starting $desc (memcached): "
daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE"$OPTIONS"
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $lockfile
return $RETVAL
}
stop() {
echo -n $"Shutting down $desc (memcached): "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $lockfile
return $RETVAL
}
restart() {
stop
start
}
reload() {
echo -n $"Reloading $desc ($prog): "
killproc $prog -HUP
RETVAL=$?
echo
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[ -e $lockfile ] && restart
RETVAL=$?
;;
reload)
reload
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL
memcached常用命令
add命令:添加一个键
add keyname flag timeout datasize
get命令:获取键对应的值
get keyname
# telnet 10.1.1.1 11211
Trying 10.1.1.1...
Connected to 10.1.1.1.
Escape character is '^]'.
add mykey 0 30 5//添加一个键,名称为mykey
hello
STORED
get mykey //获取mykey键对应的值
VALUE mykey 0 5
hello
END
perl module
cache::memcached
php
memcache
memcached
C/C++
libmemcached
memadmin基于GUI管理memcached的工具
配置PHP整合memcached
1、安装memcache
# tar zxf memcache-2.2.5.tgz
# cd memcache-2.2.5
# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
# make && make install
安装完成后,会有如下提示信息:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
2、编辑/etc/php.d/memcache.ini文件,指定PHP支持memcache.so扩展模块
# vim /etc/php.d/memcache.ini
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/memcache.so
3、重启php-fpm服务
# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpmdone
通过客户端浏览器访问index.php页面,查看memcache模块信息
创建如下php测试页面,查看php是否启用memcached
# vim /web/htdocs/test.php
页:
[1]