时间:2021-07-01 10:21:17 帮助过:47人阅读
memcache
定义
memcache是一套分布式的高速缓存系统
目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的
工作流程
1.先检查客户端访问的数据是否在于memcache,如果有就直接返回
2.如果不在memcache,就去查数据库,同时缓存一份到memcache,大大提高读取速度。
应用和特性
1.用来做网页或数据库高速缓存
2.可用来做session共享
3.适用于数据变动小但多(如微博粉丝+1)
4.存储在内存,不能数据持久化
缓存优化规则:28原则
20%:热数据,经常被访问的数据。用作缓存,存于内存
80%:基本不变化的数据,存于固态硬盘
php装载memcache模块
检测当前php环境
- vim ~/.bash_profile
- PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin/:/usr/local/lnmp/php/bin
. ~/.bash_profile 或者做软链接
- [root@server11 bin]# ln -s /usr/local/lnmp/php/bin /usr/local/bin/
编译
- tar zxf memcache-2.2.5.tgz cd memcache-2.2.5
- phpize 准备预编译环境
./configure make && make install
保证php的执行路径是源码包的路径
- [root@server11 memcache-2.2.5]# which php /usr/local/lnmp/php/bin/php
- cd /usr/local/lnmp/php/etc/
- vim php.ini 记住是.
- 863 extension=memcache.so
- /etc/init.d/php-fpm start
检验
- [root@server11 etc]# php -m |grep memcache
rpm -qa |grep php 保证没有rpm包干扰
空
后台安装配置
- yum install memcached -y
- /etc/init.d/memcached start
监听端口 netstat -antlpue
- udp
- 0
- 0 0.0.0.0:11211
- 0.0.0.0:*
- 498
- 10940
- 3706/memcached
访问memcached数据库
- yum install telnet -y
- telnet localhost 11211
- set name 0 0 6
- westos
- STORED
- get name
- VALUE name 0 6
- westos
- END
- delete name
- DELETED
- get name
- END
编写监控页面
cd memcache-2.2.5
cp memcache.php /usr/local/nginx/html/
vim memcache.php
- 23 define('ADMIN_PASSWORD','westos'); // Admin Password
- 28 $MEMCACHE_SERVERS[] = ''; // add more as an array
- 29 $MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an arra y
编写测试页面
vim test.php
- <?php
- $memcache = new Memcache;
- $memcache->connect('127.0.0.1', 11211) or die ("Could not connect");
- $version = $memcache->getVersion();
- echo "Server's version: ".$version."\n";
- $tmp_object = new stdClass;
- $tmp_object->str_attr = 'test';
- $tmp_object->int_attr = 123;
- $memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the
- server");
- echo "Store data in the cache (data will expire in 10 seconds)\n";
- $get_result = $memcache->get('key');
- echo "Data from the cache:\n";
- var_dump($get_result);
- ?>
启动nginx
nginx
检验
在浏览器访问:
1. 172.25.88.11/memcache.php 监控cache命中率
2. 172.25.88.11/test.php
不断刷新,可以在监控页面看到,缓存的命中率(Hits)越来越大
以上就是php装载memcache模块的示例代码详解(图)的详细内容,更多请关注Gxl网其它相关文章!