Home > Database > Mysql Tutorial > 用Memcached缓存MySQL数据

用Memcached缓存MySQL数据

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 17:24:44
Original
758 people have browsed it

Memcached使用了libevent来均衡任何数量的打开链接,使用非阻塞的网络I/O,对内部对象实现引用计数(因此,针对多样的客户端,对象

Memcached是一个分布式的内存对象缓存系统,通常用于动态Web应用以减轻数据库负载。
 
Memcached是基于一个存储键对的hashmap,当表格满了以后,就使用LRU(最近最小使用)算法机制替换掉。
 
Memcached使用了libevent来均衡任何数量的打开链接,,使用非阻塞的网络I/O,对内部对象实现引用计数(因此,针对多样的客户端,对象可以处在多样的状态), 使用自己的页块分配器和哈希表,因此虚拟内存不会产生碎片并且虚拟内存分配的时间复杂度可以保证为O(1).
 
下面具体说明在CentOS环境下安装memcached并结合mysql,php来测试Memcache的缓存机制
 
1、安装memcached
 
wget
 
wget
 
 
 
tar xf libevent-1.4.8-stable.tar.gz
 
cd libevent-1.4.8-stable
 
./configure
 
make && make install
 
ln -sv /usr/local/lib/libevent* /usr/lib/
 
 
 
tar xf  memcached-1.4.15.tar.gz
 
cd memcached-1.4.15
 
./configure
 
make && make install
 
 
 
尝试启动memcached,启动方法
 
[root@www ~]#memcached -d -unobody -m50 -p11211 -P /tmp/memcached.pid
 
说明:
 
-d 以独立方式运行
 
-u 以什么用户启动
 
-m 占用内存大小(默认是100)
 
-p 监听端口号
 
-P 指定pid位置
 
不报信息就ok,netstat -tnlp查看一下

2、为php安装memcached扩展(我是用的是php5.4.4)
 
[root@www ~]#wget
 
[root@www ~]#tar zxvf memcache-2.2.5.tgz
 
[root@www ~]#cd memcache-2.2.5/
 
[root@www ~]#/usr/local/php/bin/phpize
 
[root@www ~]#./configure --with-php-config=/usr/local/php/bin/php-config
 
[root@www ~]#make
 
[root@www ~]#make install
 
在配置文件中添加memcache扩展,命令如下
 
sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20100525/"\nextension = "memcache.so"#' /usr/local/php/etc/php.ini

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template