1

PHP stat缓存是什么?

 1 year ago
source link: https://www.jdon.com/67842.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

PHP stat缓存是什么?

为什么要了解它的工作原理?因为有时需要使用 clearstatcache() 函数来解决缓存问题,以使 PHP 代码在运行时不会出错。

上下文背景
让我们从头开始。什么是 stat?它是对文件系统的 C 级 API 调用,用于查询文件的元数据,如用户、组、可读性、可写性、大小、修改次数等。

从历史上看,执行这一操作的成本很高。

许多 PHP 函数都需要这一功能,当然包括 PHP 的 stat 本身,还有 file_exists、is_readable、is_writable 等等:它可以缓存低级stat 操作的结果。但有一个重要的注意事项。缓存只缓存一个文件的信息。最后一个使用缓存的文件。

这意味着,如果 PHP 对某个文件执行了低级stat 操作,而该文件之前并不在stat 缓存中,那么它将覆盖之前存储的上一个文件的stat 数据。

缓存是如何实现的?

<?php
class FileStat
{
    private static string $CurrentStatFile;
    private static PhpStreamStatBuffer $ssd;

public function stat(string $path)
    {
        if ($path === self::$CurrentStatFile) {
            return self::$ssd;
        }

$statBuffer = new PhpStreamStatBuffer(stat($path));
        self::$CurrentStatFile = $path;
        self::$ssd = $statBuffer;

return $statBuffer;
    }
}

你可以在 " “ext/standard/filestat.c” in “php_stat” function"中找到这段代码。

结论
那么,这告诉了我们关于stat 缓存的什么信息呢?它有用吗?很难说,尤其是在现代 Linux 和 SSD 磁盘上。

但如果你想使用它,请记住不能对多个文件进行交错操作,否则会导致缓存被覆盖。

您可以在clearstatcache()函数的文档中找到使用stat-cache的函数的完整列表。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK