内存缓存服务器memcahe

Memcache

在许多web应用中都将数据保存到RDBMS中,应用服务器从中读取出来并在浏览器上显示。但是随着数据量的增大、访问的集中,就会出现RDBMS的负担加重、数据库相应恶化、网站显示延迟等重大影响。

这时就该memcached大显身手了。Memcached是提高性能的分布式内存缓存服务器。一般的使用目的是,通过缓存数据库查询的结果,减少数据库访问的次数,以提高动态web应用的速度、提高可扩展性。

Memcached的特征

协议简单

基于libevent的时间处理

内置内存存储方式

Memcached不互相通信的分布式

使用memcached提高性能

memcached参数

-p监听端口

-l<ip_addr>连接的ip地址默认是本机的

Listenon<ip_addr>;defaulttoINADDR_ANY.Thisisanimportant

-dstart启动memcached作为一个服务Runmemcachedasadaemon.

Restart重启

Stop|shutdown关闭服务

Install安装服务

Uninstall卸载

-u<username>以什么身份运行

Assumetheidentityof<username>(onlywhenrunasroot).

-m<num>最大的内存使用

Use<num>MBmemorymaxtous

-f<factor>块大小增长因子,默认是1.25

Use<factor>asthemultiplierforcomputingthesizesofmemory

-M内存消耗尽时返回的错误,而不是删除项

chunksthatitemsarestoredin.Alowervaluemayresultin

lesswastedmemorydependingonthetotalamountofmemory

availableandthedistributionofitemsizes.Thedefaultis

1.25.

-n<size>最小分配空间

lamp+memcached--->lammp

1.安装libevent

2.memcached服务器,memcached基于libevent的事件处理机制,所以要指明libevent的安装路径

./configure--with-libevent=/usr/local/libevent/

make&&makeinstall

开启以nobody的身份memcached服务

memcached-unobody-dstart

查看memcached的进程及监听端口

213225858.png

手工编写memcached服务控制脚本

#chkconfig:23458999

#description:httpdservice

lockfile=/var/lock/subsys/memcached

prog=/usr/local/memcache/bin/memcached

./etc/init.d/functions

start(){

[-f$lockfile]&&echo"memcachedisstarted"&&exit

echo-n"memcachedisstarting....."

sleep1

$prog-unobody-dstart&&echo"ok"&&touch$lockfile||echo"failed"

}

stop(){

[!-f$lockfile]&&echo"memcachedisstoped"&&exit

echo-n"memcachedisstoping....."

sleep1

killprocmemcached&&echo"ok"&&rm-rf$lockfile||echo"failed"

}

case"$1"in

start)

Start

;;

stop)

stop

;;

restart)

stop

start

;;

*)

echo"usage:(start|stop|restart)"

;;

Esac

重启memcached服务

Servicememcachedrestart

[root@localhostbin]#netstat-tupln|grepmemcached

tcp000.0.0.0:112110.0.0.0:*LISTEN16174/memcached

tcp00:::11211:::*LISTEN16174/memcached

udp000.0.0.0:112110.0.0.0:*16174/memcached

udp00:::11211:::*16174/memcached

memcached控制脚本加入到chkconfig,在345级别下开机自动启动memcached服务

Chkconfig--addmemcached

Chkconfig--list|grepmemcached

[root@localhostbin]#chkconfig--list|grepmemcached

memcached0:off1:off2:on3:on4:on5:on6:off

Telnet192.168.20.20011211

Stats查看memcached的状态

STATpid19681memcached的进程id

STATuptime1858

STATtime1378208617服务开启的时间

STATversion1.4.13memcached的版本

STATlibevent2.0.16-stable

STATpointer_size32

STATrusage_user0.002999

STATrusage_system0.005999

STATcurr_connections10

STATtotal_connections11

STATconnection_structures11

STATreserved_fds20

STATcmd_get0

STATcmd_set0

STATcmd_flush0

STATcmd_touch0

STATget_hits0get下载键值的内容

STATget_misses0

STATdelete_misses0

STATdelete_hits0删除键值

STATincr_misses0

STATincr_hits0

STATdecr_misses0

STATdecr_hits0

STATcas_misses0

STATcas_hits0

STATcas_badval0

STATtouch_hits0

STATtouch_misses0

STATauth_cmds0

STATauth_errors0

STATbytes_read15

STATbytes_written14

STATlimit_maxbytes67108864

STATaccepting_conns1

STATlisten_disabled_num0

STATthreads4

STATconn_yields0

STAThash_power_level16

STAThash_bytes262144

STAThash_is_expanding0

STATexpired_unfetched0

STATevicted_unfetched0

STATbytes0

STATcurr_items0

STATtotal_items0

STATevictions0

STATreclaimed0

END

addnewkey0605add添加键值

12345键值的内容

STORED

getnewkey下载键值

VALUEnewkey05

12345

END

3.php添加memchace扩展,安装phpmemcache客户端,指明php的配置文件所在的路径

执行/usr/local/php/bin/phpize产生memcache的安装配置文件

./configure--enable-cache--with-php-config=/usr/local/php/bin/php-config

/usr/local/php/lib/php/extensions/no-debug-zts-20090626/目录下形成memcache的模块

打开php的初始化文件/etc/php.ini,添加一条语句指明memcache模块所在的位置

Extension=/usr/local/php/lib/php/extensions/no-debug-zts-20090626/

你可能感兴趣的:(数据库查询,default,可扩展性,important,数据库访问)