`
zhengdl126
  • 浏览: 2509744 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

PHP的Realpath Cache

 
阅读更多

PHP的缓存有很多种,包括输出缓冲(ob系列函数),opcode缓存(APC,eAccelerator,XCache等扩展实现),这些大家已经很熟悉了,接下来介绍一下一个不太被人注意的PHP缓存机制:realpath_cache。

介绍

require,require_once,include,include_once这四个语句(并非函数)大家经常会用到,如果用这类语句去包含文件(相对路径)的话,那么PHP会去include_path所 指定的路径中去查找相关文件。一个应用中会存在大量的require_once语句调用,如果每次调用都去include_path中查找相应的文件,势 必会对应用的性能产生负面影响。为了避免这种负面效应产生的影响,PHPER们会使用文件的绝对路径来包含所需的文件,这样就减少了查询 include_path的次数。

其实,PHP自5.1.0起,就引入了RealpathCache。RealpathCache可以把PHP所用到文件的realpath进行缓存,以便PHP再使用这些文件的时候不需要再去include_path中查找,加快PHP的执行速度。

配置

realpath cache的配置项有两个,分别为realpath_cache_size和realpath_cache_ttl,可以在php.ini中进行修改:

; Determines the size of the realpath cache to be used by PHP. This value should
; be increased on systems where PHP opens many files to reflect the quantity of
; the file operations performed.
; http://php.net/realpath-cache-size
;realpath_cache_size = 16k

; Duration of time, in seconds for which to cache realpath information for a given
; file or directory. For systems with rarely changing files, consider increasing this
; value.
; http://php.net/realpath-cache-ttl
;realpath_cache_ttl = 120

其中realpath_cache_size指定了realpath cache的大小,默认为16k,如果你觉得这个容量太小,可以适当增加;realpath_cache_ttl指定了缓存的过期时间,默认为120秒, 对于不经常修改的生产环境来说,这个数字可以调整的更大些。

问题

由于realpath会 展开symlink(即软连接),所以如果你使用修改symlink目标这种方式发布应用的新版本的话,realpath cache会导致一些问题的出现:当你修改symlink使其指向一个新的release目录时候,由于realpath cache所缓存内容还没有过期,于是就会出现应用使用的还是旧的release,直到realpath cache所缓存内容过期失效为止(默认120秒),或者重启php-fpm。

看个例子:
基础环境:nginx + fastcgi + php-fpm
应用环境:/var/www/app是一个symlink,并做为document_root,在/var/www下存在version0.1,version0.2两个版本的release。初始情况下/var/www/app指向version0.1

lrwxr-xr-x    1 weizhifeng  staff    10 10 22 16:41 app -> version0.1
drwxr-xr-x    3 weizhifeng  staff   102 10 22 16:43 version0.1
drwxr-xr-x    3 weizhifeng  staff   102 10 22 16:43 version0.2

version0.1,version0.2内部各有一个hello.php

[weizhifeng@Jeremys-Mac www]$ cat version0.1/hello.php
<?php
echo 'in version0.1';
?>

[weizhifeng@Jeremys-Mac www]$ cat version0.2/hello.php
<?php
echo 'in version0.2';
?>

nginx配置文件片段:

location / {
            root /var/www/app;   #app为symlink
            index  index.php index.html index.htm;
}

location ~ \.php$ {
            root /var/www/app; #app为symlink
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi_params;
}

此时通过HTTP访问hello.php,得到的内容是’in version0.1′;修改/var/www/app,使其指向version0.2

[weizhifeng@Jeremys-Mac www]$ rm -f app && ln -s version0.2/ app

修改完成之后通过HTTP访问hello.php,得到的内容仍旧是”in version0.1″,可见是realpath cache在作祟了,此时你可以重启php-fpm或者等待120秒钟让realpath cache失效。

你可以使用clearstatcache来清 除realpath cache,但是这个只对当前调用clearstatcache函数的PHP进程有效,而其他的PHP进程还是无效,由于PHP进程池(php-fpm生 成,或者Apache在prefork模式下产生的N个httpd子进程)的存在,这个方法不是很适用。

参考:

http://php.net/manual/en/ini.core.php#ini.sect.performance

http://sixohthree.com/1517/php-and-the-realpath-cache

分享到:
评论

相关推荐

    matlab开发-realpath

    matlab开发-realpath。绝对路径名。

    rh-nodejs6-nodejs-fs.realpath-1.0.0-3.el7.noarch.rpm

    官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装

    realpath:这是Go(golang)中realpath()函数的实现。 它告诉给定相对路径的绝对路径

    真实路径 这是Go(golang)中realpath()函数的实现。 如果为它提供有效的相对路径/别名路径,它将为您返回系统中其真实绝对路径的字符串。 原始版本是Taru Karttunen在golang-nuts组中创建的。 您可以阅读。安装go...

    node.js中的fs.realpath方法使用说明

    fs.realpath(path, [cache], [callback(err , resolvedPath)]) 由于该方法属于fs模块,使用前需要引入fs模块(var fs= require(“fs”) ) 接收参数: path 路径 cache 可选,一个文字的映射路径可用于强制一个...

    php.ini-development

    user_ini.cache_ttl = 300 ;;;;;;;;;;;;;;;;;;;; ; Language Options ; ;;;;;;;;;;;;;;;;;;;; ; Enable the PHP scripting language engine under Apache. ; http://php.net/engine engine = On ; This directive...

    处理Word文档的PHP库.zip

    $vendorDirPath = realpath(__DIR__ . '/vendor'); if (file_exists($vendorDirPath . '/autoload.php')) {  require $vendorDirPath . '/autoload.php'; } else {  throw new Exception(  ...

    realpath:绝对路径名。-matlab开发

    解析输入中的相对路径和额外的 fileep 字符。... 句法: 输出 = REALPATH(THEPATH) 其中 THEPATH 是文件或文件夹的路径,它应该存在。 例子: realpath('../myfile.txt'); % 返回 /home/user/whatever/myfile.txt

    realpath-ext:* nix系统的realpath()(带有一些扩展)的实现

    真实路径扩展 * nix系统的realpath()(带有一些扩展名)的实现。

    PHP实现批量清空删除指定文件夹所有内容的方法

    cleancache.php: &lt;?php // 清文件缓存 $dirs = array( realpath(dirname(__FILE__) . '/../data/cache_data'), realpath(dirname(__FILE__) . '/../data/cache_file'), realpath(dirname(__FILE__) . '/../...

    开发PHP扩展详细教程

    如果用PHP不能再满足你的需求,最好的办法就是开发PHP扩展。这有一些好处: 1、增加自己的特殊功能。 2、保护自己的专利代码。 这是几年前的一篇英文文章,现在已被翻译成中文版的。 作者应该是hshq_cn。 链接是:...

    realpath:展开符号链接并打印已解析的绝对文件路径

    真实路径 展开符号链接并打印已... $ npm install --global realpath 用法 $ realpath --help Usage $ realpath Example $ realpath ../unicorn /Users/sindresorhus/dev/unicorn 执照 麻省理工学院:copyright:

    处理PPT幻灯片的PHP库.zip

    $vendor = realpath(__DIR__ . '/../vendor'); if (file_exists($vendor . "/autoload.php")) {  require $vendor . "/autoload.php"; } else {  $vendor = realpath(__DIR__ . '/../../../'...

    ApiGen-masterAPI文档PHP库.zip

    $rootDir = realpath(__DIR__ . '/../'); // Autoloader for standalone install (installed via `composer create-project` or cloned locally) $autoloader = realpath($rootDir . '/vendor/...

    php下载excel无法打开的解决方法

    php下载excel文件,1、在下载的过程中不要 输出任何非文件信息,比如 echo ... $file_name)){ //$this-&gt;logger-&gt;error(‘file realpath:’.realpath(CACHE_PATH . $file_name)); header( ‘Pragma: public’ ); hea

    根据输入的路径,php自动生成并输出图片

    1)目前只是png的后缀,其他的后缀可以自己修改 2)根据字体文件的大小,字体文件包太小的话个别汉字会出现?...$font = realpath("./font/1.ttf"); //字体文件 //$height =$height + $h*$size; //这里给注释掉

    realpath:解析输入中的相对路径和额外的 fileep 字符。-matlab开发

    M MA 使用对文件提交和附加测试提出的建议更新了 realpath 版本。 适用于 Windows,可能需要对 Unix 进行一些调整。 在测试中比原始版本快得多。 解析输入中的相对路径和额外的 fileep 字符。 句法: 输出 = ...

    ThinkPHP3.2 集成 php-resque: PHP Resque Worker

    require realpath($file); } $logLevel = 0; $LOGGING = getenv('LOGGING'); $VERBOSE = getenv('VERBOSE'); $VVERBOSE = getenv('VVERBOSE'); if (!empty($LOGGING) || !empty($VERBOSE)) { $logLevel = ...

    php获取网站根目录物理路径的几种方法(推荐)

    在PHP中获取网站根目录物理路径。 在php程序开发中经常需要获取当前网站的目录,我们可以通过常量定义获取站点根目录物理路径,方便在程序中使用。...define('WWW_PATH',str_replace('\\','/',realpath(d

Global site tag (gtag.js) - Google Analytics