Memcache

1.简介:
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信


2.为什么要用?

MySQL:磁盘上的数据库,读写速度都比较慢
Memcached:内存中的数据库,读写速度都比较快,缺点就是数据容易丢失。

数据存储,数据仓库选择MySQL这种磁盘数据库。
在高并发,业务比较大的情况下建议选择Memcached数据库

工作中,MySQL和Memcached一起使用
3.Memcached在企业的应用场景

缓存服务器Memcache

作为数据库的前端缓存应用,减少数据库被高并发大量的访问的压力

优点:
Ⅰ.对于用户来讲,用户访问网站更快了,查询数据直接可以从memcached数据库直接调取数据信息
Ⅱ.对于网站来说,数据库降低了压力,只有当内存没有数据时才会请求MySQL数据库。
Ⅲ. 提升了网站并发访问的效率,减少服务器的数量

4.环境准备

centos6.9 web1 IP 10.0.0.7 能够打开word press
centos7.2 memcached IP 10.0.0.21

5.安装Memcached

centos7.2主机安装:

yum -y install memcached

systemctl start memcached.service
缓存服务器Memcache

测试:

创建:
[root@cache01 ~]# printf "set key008 0 0 10\r\noldboy0987\r\n"|nc 10.0.0.21 11211
STORED

查看:
[root@cache01 ~]# printf "get key008\r\n"|nc 10.0.0.21 11211
VALUE key008 0 10
oldboy0987
END

删除:
[root@cache01 ~]# printf "delete key008\r\n"|nc 10.0.0.21 11211
DELETED

缓存服务器Memcache

7.web01操作

上传软件包

缓存服务器Memcache

tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5
/application/php/bin/phpize
./configure --enable-memcache --with-php-config=/application/php/bin/php-config --with-zlib-dir
make
make install

执行成功后:
缓存服务器Memcache
sed -i '$a extension=memcache.so' /application/php/lib/php.ini
pkill php
/application/php/sbin/php-fpm -t
/application/php/sbin/php-fpm
/application/php/bin/php -m|grep memcache

执行成功后:
缓存服务器Memcache

7.PHP代码测试

cat >>/application/nginx/html/blog/mc.php<<'EOF'
<?php
$memcache = new Memcache;
$memcache->connect('10.0.0.21', 11211) or die ("Could not connect");
$memcache->set('key20180314', 'hello,world');
$get_value = $memcache->get('key20180314');
echo $get_value;
?>
EOF

8.WEB管理memcached

tar xf memadmin-1.0.12.tar.gz -C /application/nginx/html/blog/
浏览器访问http://blog.etiantian.org/memadmin

缓存服务器Memcache
缓存服务器Memcache

缓存服务器Memcache
缓存服务器Memcache!
cd /application/nginx/html/blog/wp-content/
rz
grep 127.0.0.1 object-cache.php
sed -i 's#127.0.0.1#10.0.0.21#' object-cache.php

缓存服务器Memcache

缓存服务器Memcache

web使用word press上传博文利用memcached查看缓存

缓存服务器Memcache

缓存服务器Memcache

posted @ 2018-05-09 18:09  地铁昌平线  阅读(129)  评论(0编辑  收藏  举报