下面总结了三种缓存文件方法,一种是nginx下的缓存fastcgi_cache和proxy_cache,一种利用memcache缓存,另一种是利用php文件缓存哦。
nginx有两种缓存机制:fastcgi_cache和proxy_cache
下面我们来说说这两种缓存机制的区别吧
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:200m inactive=1d max_size=30g;
代码如下 | 复制代码 |
server location / #扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存。 access_log off; |
fastcgi_cache缓存设置
#定义缓存存放的文件夹
代码如下 | 复制代码 |
fastcgi_cache_path /tt/cache levels=1:2 keys_zone=NAME:2880m inactive=2d max_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.html index.htm index.php; } location ~ (|.php)$ { root /www; fastcgi_pass 127.0.0.1:9000; fastcgi_cache NAME; fastcgi_cache_valid 200 48h; fastcgi_cache_min_uses 1; fastcgi_cache_use_stale error timeout invalid_header http_500; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi.conf; #设置缓存的过程中发现无法获取cookie,经查需要定义这句话 fastcgi_pass_header Set-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操作,性能会大大下降,而内存表不会,内存满了后,会提示数据满错误。
例:
代码如下 | 复制代码 |
create table test ( id int unsigned not null auto_increment primary key state char(10), type char(20), date char(30) )engine=memory default charset=utf8 |
内存表的特性:
1.内存表的表定义存放在磁盘上,扩展名为.frm,所以重启不会丢失
2.内存表的数据是存放在内存中,重启会丢失数据
3.内存表使用一个固定的长度格式
4.内存表不支持blob或text列,比如varchar与text字段就不会被支持
5.内存表支持auto_increment列和对可包含null值的列的索引
6.内存表不支持事物
7.内存表是表锁,当修改频繁时,性能可能会下降
分享一个存php缓存类
代码如下 | 复制代码 |
class Cache private static $_instance; const CLEANING_MODE_ALL = 'all'; protected $_options = array( private function __construct(){} $this->_cacheId = $id; $this->_cacheId = $id; /** $this->_cacheId = $id; if(null !== $lifeTime) case CLEANING_MODE_ALL: case CLEANING_MODE_OLD: /**
|

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

Eclipse용 SAP NetWeaver 서버 어댑터
Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

Atom Editor Mac 버전 다운로드
가장 인기 있는 오픈 소스 편집기

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경

VSCode Windows 64비트 다운로드
Microsoft에서 출시한 강력한 무료 IDE 편집기

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경
