基于php缓存的详解
nginx缓存
下面我们来说说这两种缓存机制的区别吧
proxy_cache作用是缓存后端服务器的内容,可能是任何内容,包括静态的和动态的
fastcgi_cache作用是缓存fastcgi生成的内容,很多情况是php生成的动态内容
proxy_cache缓存减少了nginx与后端通信的次数,节省了传输时间和后端带宽
fastcgi_cache缓存减少了nginx与php的通信次数,更减轻了php和数据库的压力。
proxy_cache缓存设置
#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path /data0/proxy_temp_dir;
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200minactive=1dmax_size=30g;
server
{
listen 80;
server_name www.yourdomain.com192.168.8.42;
indexindex.htmlindex.htm;
root /data0/htdocs/www;
location/
{
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header;
proxy_cachecache_one;
#对不同的HTTP状态码设置不同的缓存时间
proxy_cache_valid 20030412h;
#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_key$host$uri$is_args$args;
proxy_set_headerHost $host;
proxy_set_headerX-Forwarded-For $remote_addr;
proxy_passhttp://backend_server;
expires 1d;
}
#用于清除缓存,假设一个URL为http://192.168.8.42/test.txt,通过访问http://192.168.8.42/purge/test.txt就可以清除该URL的缓存。
location~/purge(/.*)
{
#设置只允许指定的IP或IP段才可以清除URL缓存。
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
#扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存。
location~.*\.(php|jsp|cgi)?$
{
proxy_set_headerHost $host;
proxy_set_headerX-Forwarded-For $remote_addr;
proxy_passhttp://backend_server;
}
access_log off;
}
}
fastcgi_cache缓存设置
#定义缓存存放的文件夹
fastcgi_cache_path /tt/cache levels=1:2keys_zone=NAME:2880minactive=2dmax_size=10G;
#定义缓存不同的url请求
fastcgi_cache_key"$scheme$request_method$host$uri$arg_filename$arg_x$arg_y";
server{
listen 8080;
server_name www.example.com;
location/{
root /www;
index index.htmlindex.htmindex.php;
}
location~(|.php)${
root /www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_cache NAME;
fastcgi_cache_valid20048h;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_staleerror timeoutinvalid_headerhttp_500;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
#设置缓存的过程中发现无法获取cookie,经查需要定义这句话
fastcgi_pass_headerSet-Cookie;
}
log_format access "$remote_addr-$remote_user[$time_local]"$request""
"$status$body_bytes_sent"$http_referer""
""$http_user_agent"$http_x_forwarded_for";
access_log /httplogs/access.log access;
}
总的来说 nginx的proxy_cache和fastcgi_cache的缓存配置差不多。
--------------------------------------------------------------------------------
memcache缓存
在讨论memcache缓存之前,我们先了解下mysql的内存缓存吧
mysql的内存缓存可以在my.cnf中指定大小:内存表和临时表不同,临时表也是存放内存中,临时表最大的内存需要通过tmp_table_size=128M设定。当数据查过临时表的最大值设定时,自动转为磁盘表,此时因需要进行IO操作,性能会大大下降,而内存表不会,内存满了后,会提示数据满错误。
例:
createtabletest
(
idintunsignednotnullauto_incrementprimarykey
statechar(10),
typechar(20),
datechar(30)
)engine=memorydefaultcharset=utf8
内存表的特性:
1.内存表的表定义存放在磁盘上,扩展名为.frm,所以重启不会丢失
2.内存表的数据是存放在内存中,重启会丢失数据
3.内存表使用一个固定的长度格式
4.内存表不支持blob或text列,比如varchar与text字段就不会被支持
5.内存表支持auto_increment列和对可包含null值的列的索引
6.内存表不支持事物
7.内存表是表锁,当修改频繁时,性能可能会下降
下面我们来看看memcache,相对而言mysql的内存表限制较多。
memcache的用途
1.提高系统的并发能力
2.减轻数据库的负担
注:memcachelinux系统32位只支持4G内存,同时memcache最长保存时间为30天。
相关文章
- PHP时区设置相差8小时间程序与php.ini配置方法
- php new reflectionclass,PHP: ReflectionClass – Manual
- PHP 浏览器缓存_php缓存引擎
- PHP编译参数configure配置详解,以及php.ini说明
- PHP include和require:文件包含语句
- php+redis缓存类详解大数据
- [PHP] apache在worker模式配置fastcgi使用php-fpm详解编程语言
- MySQL扩展:为PHP带来强大的数据库支持(php的mysql扩展)
- 助力MySQL优化:PHP助力提升效率(mysql优化php)
- PHP扩展Redis:建立强大的缓存架构(php扩展redis)
- 使用PHP和MySQL精心构建表单(php表单mysql)
- 开发使用PHP进行Redis项目开发(redis项目php)
- PHP配置指南:MySQL编码设置详解(php设置mysql编码)
- 使用PHP实现Redis快速构建应用(redis实例php)
- 轻松设置PHP连接MySQL数据库(php设置连接mysql)
- MongoDB整合PHP:快速配置你的开发环境(mongodb配置php)
- 用Redis作为PHP缓存:提高性能和效率(php中redis做缓存)
- PHP操作MySQL数据库详解,快速学习调用方法(php怎么调用mysql)
- 解决PHP读取MSSQL数据库乱码问题(php读mssql乱码)
- PHP如何连接MS SQL服务器(php怎么链接mssql)
- PHP与MSSQL联手谱写新篇章(php大马 mssql)
- Linux下查看 PHP 进程信息(linux查看php进程)
- 记录PHP之MySQL踩坑:如何防止重复记录(php mysql 重复)
- 如何使用Redis技术来优化PHP性能(清楚redis php)
- php下实现伪url的超简单方法[转]
- PHP输出简单动态WAP页面
- windows环境下php配置memcache的具体操作步骤