A detailed _php tutorial based on PHP cache

Source: Internet
Author: User
Tags table definition
Nginx Cache
Nginx has two kinds of caching mechanisms: Fastcgi_cache and Proxy_cache
Let's talk about the difference between the two caching mechanisms.
The Proxy_cache role is to cache the contents of the backend server, possibly any content, including static and dynamic
The Fastcgi_cache function is to cache fastcgi generated content, in many cases the dynamic content generated by PHP
Proxy_cache cache reduces the number of nginx-to-backend communications, saving time and backend bandwidth
The Fastcgi_cache cache reduces the number of nginx-to-PHP communications and reduces the pressure on PHP and the database.

Proxy_cache Cache Settings
#注: The path specified by Proxy_temp_path and Proxy_cache_path must be in the same partition
#设置Web缓存区名称为cache_one, the size of the memory cache is automatically cleared for content that is not accessed for 200mb,1 days, and the disk cache space size is 30GB.
Proxy_cache_path/data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

Listen 80;
server_name www.yourdomain.com;
Index index.html index.htm;

#如果后端的服务器返回502, 504, execution timeout, etc., automatically forwards the request to another server in the upstream load balancer pool for failover.
Proxy_next_upstream http_502 http_504 error timeout invalid_header;
Proxy_cache Cache_one;
Proxy_cache_valid 304 12h;
#以域名, URI, parameters combined into the Web cache key value, Nginx hash according to the key value, Store the cached content in the level two cache directory
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;

#用于清除缓存, assuming that a URL is, you can clear the cache of the URL by accessing Http://
Location ~/purge (/.*)
Deny all;
Proxy_cache_purge cache_one $host $1$is_args$args;

Dynamic applications that end in #扩展名以. PHP,. JSP,. CGI are not cached.
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
Fastcgi_cache_path/tt/cache levels=1:2 keys_zone=name:2880m inactive=2d max_size=10g;
Fastcgi_cache_key "$scheme $request_method$host$uri$arg_filename$arg_x$arg_y";

server {
Listen 8080;
server_name www.example. com;
Index index.html index.htm index.php;

Location ~ (|. PHP) $ {

Fastcgi_cache NAME;
Fastcgi_cache_valid 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, the sentence needs to be defined by the search
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;
In General, Nginx Proxy_cache and Fastcgi_cache cache configuration is similar.
memcache Cache
Before we discuss the memcache cache, let's look at MySQL's memory cache.
MySQL's memory cache can be specified in MY.CNF size: the memory table and the temporary table are different, the temporary table is also in memory, the temporary table maximum memory needs to be set through tmp_table_size=128m. When the data checked the temporary table maximum setting, automatically to the disk table, at this time because of the need for IO operation, performance will be greatly reduced, and the memory table will not, when the memory is full, will prompt the data full error.
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 disk with the extension. frm, so reboots are not lost
2. Memory table data is stored in memory, reboot will lose data
3. The memory table uses a fixed length format
4. The memory table does not support blob or text columns, such as varchar and text fields are not supported
5. Memory table supports auto_increment columns and indexes on columns that can contain null values
6. Memory table does not support things
7. The memory table is a table lock and performance may degrade when frequently modified

Let's take a look at Memcache, which is relatively limited by MySQL's memory table.
Use of Memcache
1. Improve the concurrency of the system
2. Reducing the burden on the database
Note: The Memcache Linux system 32-bit supports only 4G of memory, while the Memcache maximum storage time is 30 days.

http://www.bkjia.com/PHPjc/327137.html www.bkjia.com true http://www.bkjia.com/PHPjc/327137.html techarticle Nginx Cache Nginx has two kinds of caching mechanism: Fastcgi_cache and Proxy_cache below we say the difference between the two caching mechanisms Proxy_cache role is to cache the contents of the backend server, it may be ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.