Maison php教程 PHP开发 apache利用mod_cache缓存图片等

apache利用mod_cache缓存图片等

Dec 21, 2016 am 11:38 AM

一,引言

以前我写过二篇有关图片,静态文件的缓存文章,一个是用varnish来缓存,一个用squid来缓存,apache当然也可以用来做图片,静态文件的缓存,下面我将详细说明一下,怎么完装和配置

二,安装mod_cache,mod_mem_cache,mod_disk_cache

先查一下,安装apache的时候,有没有安装这些模块,如果没有安装就安装一下,请参考apache怎么安装模块,查看是否安装

[zhangy@BlackGhost error]$ /usr/local/apache2/bin/httpd -l

如果没有mod_cache.c,mod_mem_cache.c,mod_disk_cache.c说明没有装mod_mem_cache,mod_disk_cache不一定要都装,你可以二选一

三,内存缓存配置

nano /usr/local/apache2/conf/httpd.conf

LoadModule cache_module modules/mod_cache.so
<IfModule mod_cache.c>
LoadModule mem_cache_module modules/mod_mem_cache.so
<IfModule mod_mem_cache.c>
CacheEnable mem /images
MCacheSize 4096
MCacheRemovalAlgorithm LRU
MCacheMaxObjectCount 100
MCacheMinObjectSize 1
MCacheMaxObjectSize 2048
CacheMaxExpire 864000
CacheDefaultExpire 86400
CacheDisable /php
</IfModule>
</IfModule>

说明:

1,CacheEnable mem /images 缓存images下面的内容,这里的 mem只是一个缓存类型,指示mod_cache使用内存的存储管理器通过实施mod_mem_cache 。缓存类型disk指示mod_cache使用基于磁盘的存储管理的实施mod_disk_cache 。缓存类型,fd指示mod_cache使用文件描述符缓存实施mod_mem_cache

2,MCacheSize最大内存使用量,插入在缓存和对象大小的对象是大于剩余内存,将被删除,直到新的对象可以被缓存。 被删除的对象选择使用指定的算法MCacheRemovalAlgorithm

3,MCacheRemovalAlgorithm缓存算法:

LRU (最近最少使用)

LRU删除文件,没有时间最长的被访问的。

GDSF(GreadyDual尺寸)

GDSF分配一个优先的文件缓存文件的费用的基础上,缓存大小和怀念。以最低的优先权文件被删除第一次。

4,MCacheMaxObjectCount最大缓存对像个数

5,MCacheMaxObjectCount最大缓存对像字节数

6,MCacheMinObjectSize最小缓存对像字节数

7,CacheMaxExpire 最大缓存结止时间

8,CacheDefaultExpire  默认缓存结止时间

9,CacheDisable /php       不缓存php下面的内容

查看内存是不是缓存了东西

apache查看有没有在内存中缓存东西,不是很好查,没有专门的工具来查看,我的方法如下,

1,浏览图片http://localhost/images/http_imgload.cgi.jpeg

2,查看一下[zhangy@BlackGhost error]$ top -b -n1

3,浏览图片http://localhost/images/myself.jpeg

4,查看一下[zhangy@BlackGhost error]$ top -b -n1

6018 zhangy    20   0 52612 8172 2484 S    0  0.8   0:00.03 httpd
6020 zhangy    20   0 52604 8168 2488 S    0  0.8   0:00.02 httpd
6021 zhangy    20   0 52604 8100 2440 S    0  0.8   0:00.00 httpd
6022 zhangy    20   0 52604 8100 2440 S    0  0.8   0:00.00 httpd
6033 zhangy    20   0  6584 1808 1396 S    0  0.2   0:00.02 bash
6076 zhangy    20   0 52612 8136 2464 S    0  0.8   0:00.02 httpd
6077 zhangy    20   0 52612 8124 2448 S    0  0.8   0:00.05 httpd
6078 zhangy    20   0 52612 8168 2488 S    0  0.8   0:00.12 httpd
6079 zhangy    20   0 52612 8168 2488 S    0  0.8   0:00.03 httpd
6080 zhangy    20   0 52612 8168 2488 S    0  0.8   0:00.06 httpd
6081 zhangy    20   0 52612 8116 2448 S    0  0.8   0:00.00 httpd

通过上面的4步操作,你可以发现上面加粗部分的RES是不断增加的,由起可见,配置已经成功。

四,硬盘缓存配置

nano /usr/local/apache2/conf/httpd.conf

LoadModule cache_module modules/mod_cache.so
<IfModule mod_cache.c>
LoadModule disk_cache_module modules/mod_disk_cache.so
<IfModule mod_disk_cache.c>
CacheRoot /home/zhangy/cachetest
#CacheSize 256
CacheEnable disk /
CacheDirLevels 4
#CacheMaxFileSize 64000
#CacheMinFileSize 1
#CacheGcDaily 23:59
CacheDirLength 3
</IfModule>
</IfModule>

说明:

1,cacheroot /home/zhangy/cachetest  缓存存放的目录

2,#CacheSize 256         缓存空间大小单位KB

3,CacheEnable disk /    设置缓存方式

4,#CacheMaxFileSize 64000    最大缓存文件大小

5,#CacheMinFileSize 1      最小缓存文件大小

6,#CacheGcDaily 23:59     缓存清理时间

7,CacheDirLength 3        缓存文件夹名子字符长度

8,CacheDirLevels 4        缓存目录,子目录层次

注释掉的部分,我的apache版本不支持。官方手册上面有。具体需求请参考官方网站

查看缓存:

1,查看cacheroot有没有东西

apache cache

如果CacheRoot下面有东西,说明已经OK了。

2,用htcacheclean来查看

[root@BlackGhost cache]# /usr/sbin/htcacheclean -v -p /home/zhangy/cachetest -l 1024M
Statistics:
size limit 1024.0M
total size was 29.2K, total size now 29.2K
total entries was 3, total entries now 3

htcacheclean  部分参数说明下

-d  每隔多长时间清一个缓存

-D 模拟清除缓存,但不是真的清

-v 显示统计信息

-r 彻底清除

-t 清除空目录

-p 缓存目录

-l 限定缓存大小

 以上就是apache利用mod_cache缓存图片等的内容,更多相关内容请关注PHP中文网(www.php.cn)! 


Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Article chaud

Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD
R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌

Article chaud

Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD
R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌

Tags d'article chaud

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Le C++ est-il front-end ou back-end ? Le C++ est-il front-end ou back-end ? Apr 22, 2024 pm 05:33 PM

Le C++ est-il front-end ou back-end ?

Comparaison des performances du framework PHP : la confrontation ultime entre vitesse et efficacité. Comparaison des performances du framework PHP : la confrontation ultime entre vitesse et efficacité. Apr 30, 2024 pm 12:27 PM

Comparaison des performances du framework PHP : la confrontation ultime entre vitesse et efficacité.

Comment générer une URL à partir d'un fichier HTML Comment générer une URL à partir d'un fichier HTML Apr 21, 2024 pm 12:57 PM

Comment générer une URL à partir d'un fichier HTML

Le module évasif protège votre site Web des attaques DOS de la couche application Le module évasif protège votre site Web des attaques DOS de la couche application Apr 30, 2024 pm 05:34 PM

Le module évasif protège votre site Web des attaques DOS de la couche application

Intégration et extension du contrôle de concurrence des fonctions Golang et des bibliothèques tierces Intégration et extension du contrôle de concurrence des fonctions Golang et des bibliothèques tierces Apr 25, 2024 am 09:27 AM

Intégration et extension du contrôle de concurrence des fonctions Golang et des bibliothèques tierces

Comment effectuer des tests de concurrence et le débogage dans la programmation simultanée Java ? Comment effectuer des tests de concurrence et le débogage dans la programmation simultanée Java ? May 09, 2024 am 09:33 AM

Comment effectuer des tests de concurrence et le débogage dans la programmation simultanée Java ?

Comment ajouter un serveur dans Eclipse Comment ajouter un serveur dans Eclipse May 05, 2024 pm 07:27 PM

Comment ajouter un serveur dans Eclipse

Application d'algorithmes dans la construction de 58 plateformes de portraits Application d'algorithmes dans la construction de 58 plateformes de portraits May 09, 2024 am 09:01 AM

Application d'algorithmes dans la construction de 58 plateformes de portraits

See all articles