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缓存设置
#定义缓存存放的文件夹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.内存表是表锁,当修改频繁时,性能可能会下降
下面我们来看看memcache,相对而言mysql的内存表限制较多。
memcache的用途
1.提高系统的并发能力
2.减轻数据库的负担
注:memcache linux系统32位只支持4G内存,同时memcache最长保存时间为30天。

Calculating the total number of elements in a PHP multidimensional array can be done using recursive or iterative methods. 1. The recursive method counts by traversing the array and recursively processing nested arrays. 2. The iterative method uses the stack to simulate recursion to avoid depth problems. 3. The array_walk_recursive function can also be implemented, but it requires manual counting.

In PHP, the characteristic of a do-while loop is to ensure that the loop body is executed at least once, and then decide whether to continue the loop based on the conditions. 1) It executes the loop body before conditional checking, suitable for scenarios where operations need to be performed at least once, such as user input verification and menu systems. 2) However, the syntax of the do-while loop can cause confusion among newbies and may add unnecessary performance overhead.

Efficient hashing strings in PHP can use the following methods: 1. Use the md5 function for fast hashing, but is not suitable for password storage. 2. Use the sha256 function to improve security. 3. Use the password_hash function to process passwords to provide the highest security and convenience.

Implementing an array sliding window in PHP can be done by functions slideWindow and slideWindowAverage. 1. Use the slideWindow function to split an array into a fixed-size subarray. 2. Use the slideWindowAverage function to calculate the average value in each window. 3. For real-time data streams, asynchronous processing and outlier detection can be used using ReactPHP.

The __clone method in PHP is used to perform custom operations when object cloning. When cloning an object using the clone keyword, if the object has a __clone method, the method will be automatically called, allowing customized processing during the cloning process, such as resetting the reference type attribute to ensure the independence of the cloned object.

In PHP, goto statements are used to unconditionally jump to specific tags in the program. 1) It can simplify the processing of complex nested loops or conditional statements, but 2) Using goto may make the code difficult to understand and maintain, and 3) It is recommended to give priority to the use of structured control statements. Overall, goto should be used with caution and best practices are followed to ensure the readability and maintainability of the code.

In PHP, data statistics can be achieved by using built-in functions, custom functions, and third-party libraries. 1) Use built-in functions such as array_sum() and count() to perform basic statistics. 2) Write custom functions to calculate complex statistics such as medians. 3) Use the PHP-ML library to perform advanced statistical analysis. Through these methods, data statistics can be performed efficiently.

Yes, anonymous functions in PHP refer to functions without names. They can be passed as parameters to other functions and as return values of functions, making the code more flexible and efficient. When using anonymous functions, you need to pay attention to scope and performance issues.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
