Home  >  Article  >  Backend Development  >  Detailed explanation based on php caching_PHP tutorial

Detailed explanation based on php caching_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:10:07857browse

nginx缓存
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
  {
    listen       80;
    server_name  www.yourdomain.com 192.168.8.42;
    index index.html index.htm;
    root  /data0/htdocs/www; 

    location /
    {
         #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
         proxy_next_upstream http_502 http_504 error timeout invalid_header;
         proxy_cache cache_one;
         #对不同的HTTP状态码设置不同的缓存时间
         proxy_cache_valid  200 304 12h;
         #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
         proxy_cache_key $host$uri$is_args$args;
         proxy_set_header Host  $host;
         proxy_set_header X-Forwarded-For  $remote_addr;
         proxy_pass http://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_header Host  $host;
         proxy_set_header X-Forwarded-For  $remote_addr;
         proxy_pass http://backend_server;
    }

access_log off;
}
}

fastcgi_cache cache settings
#Define the folder where the cache is stored
fastcgi_cache_path /tt/cache levels=1 :2 keys_zone=NAME:2880m inactive=2d max_size=10G;
#Define caching different url requests
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;

fast cgi_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;
param SCRIPT_FILENAME /scripts$fastcgi_script_name;
               include       fastcgi.conf; It was found that the cookie cannot be obtained. After checking, it is necessary to define this sentence
fastcgi_pass_header Set-Cookie;
}

log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
The cache configurations of proxy_cache and fastcgi_cache are similar.

-------------------------------------------------- ------------------------------------

memcache cache
Before discussing the memcache cache, let’s first understand the memory cache of mysql
The memory cache of mysql can be specified in my.cnf: the memory table is different from the temporary table. The temporary table is also stored in the memory, and the largest memory of the temporary table is Needs to be set through tmp_table_size=128M. When the data checks the maximum value setting of the temporary table, it is automatically converted to a disk table. At this time, due to the need for IO operations, the performance will be greatly reduced, but the memory table will not. When the memory is full, a data full error will be prompted.
Example: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


Features of the memory table:

1. The table definition of the memory table is stored on the disk with an extension of .frm, so it will not be lost after restarting

2. The data of the memory table is Stored in memory, data will be lost when restarting

3. The memory table uses a fixed length format4. The memory table does not support blob or text columns, such as varchar and text fields will not be supported
5 .The memory table supports auto_increment columns and indexes on columns that can contain null values
6. The memory table does not support transactions
7. The memory table is a table lock, and when it is modified frequently, performance may decrease

Let’s take a look at memcache. Relatively speaking, mysql’s memory table has more restrictions.

Purposes of memcache

1. Improve the concurrency capability of the system
2. Reduce the burden on the database
Note: memcache linux system 32-bit only supports 4G memory. The maximum storage time of memcache is 30 days.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327137.htmlTechArticlenginx cache nginx has two caching mechanisms: fastcgi_cache and proxy_cache Let’s talk about the differences between these two caching mechanisms. The function of proxy_cache is to cache the content of the back-end server, which may be...
Statement:
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