Details based on php cache

  • 2020-06-03 05:55:41
  • OfStack

nginx cache
nginx has two caching mechanisms :fastcgi_cache and proxy_cache
Let's talk about the differences between the two caching mechanisms
proxy_cache is used to cache the contents of the backend server, which can be anything, both static and dynamic
fastcgi_cache is used to cache content generated by fastcgi, in many cases dynamic content generated by php
The proxy_cache cache reduces the number of times that nginx communicates with the back end, saving transmission time and back end bandwidth
The fastcgi_cache cache reduces the number of communications between nginx and php, reducing the stress on php and the database.

proxy_cache cache Settings
Note: the path specified by proxy_temp_path and proxy_cache_path must be in the same partition
proxy_temp_path /data0/proxy_temp_dir;
Set Web cache name to cache_one, memory cache size to 200MB, contents not accessed in 1 day are automatically cleared, hard disk cache size to 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 /
{
If the backend server returns errors such as 502, 504, execution timeout, etc., the request is automatically forwarded to another server in the upstream load balancing pool to achieve failover.
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
Set different cache times for different HTTP status codes
proxy_cache_valid 200 304 12h;
# Combine the Key value of Web cache with the domain name, URI and parameters. Nginx stores the cache contents into the level 2 cache directory according to the hash of Key value
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;
}

# is used to clear the cache, assuming a URL http: / / 192.168.8.42 test txt, by visiting http: / / 192.168.8.42 purge/test txt can remove the URL cache.
location ~ /purge(/.*)
{
Set the URL cache to clear only the IP or IP segments specified.
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}

# Dynamic applications ending 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
Define the folder where the cache resides
fastcgi_cache_path /tt/cache levels=1:2 keys_zone=NAME:2880m inactive=2d max_size=10G;
# Define to cache 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;

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;
# It was found that cookie could not be obtained while setting the cache
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, proxy_cache and fastcgi_cache have similar cache configurations.
--------------------------------------------------------------------------------
memcache cache
Before discussing the memcache cache, let's take a look at the memory cache of mysql
The memory cache for mysql can be specified in ES228en.cnf: Memory tables are different from temporary tables, which are also stored in memory. The maximum memory for temporary tables needs to be set by tmp_table_size=128M. When the data checks the maximum setting of the temporary table, it will be automatically converted to the disk table. At this time, due to the need for IO operation, the performance will be greatly reduced, but the memory table will not. When the memory is full, the data full error will be prompted.
Ex. :
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

Memory table features:
1. The table definitions for the in-memory tables are stored on disk with the.frm extension, so the restart is not lost
2. The data of memory table is stored in memory, and data will be lost if restarted
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 will not be supported
5. In-memory tables support auto_increment columns and indexes to columns that can contain null values
6. Memory tables do not support things
7. Memory tables are table locks and performance can degrade when changes occur frequently

Let's take a look at memcache, which has relatively more limited memory tables.
The purpose of the memcache
1. Improve the concurrency of the system
2. Lighten the burden of database
Note: THE 32-bit memcache linux system only supports 4G memory, and the maximum storage time of memcache is 30 days.


Related articles: