在PHP开发中,md5_file() 函数常用于获取文件的MD5哈希值,便于校验文件完整性或检测文件变化。然而,当需要对同一批文件多次调用 md5_file() 时,频繁的磁盘IO操作会显著降低程序执行效率。为此,我们可以通过缓存机制来提升 md5_file() 函数多次调用时的执行效率。
md5_file() 函数每次调用时都会从磁盘读取整个文件内容,计算出MD5值。如果对同一个文件多次调用,文件内容被重复读取,造成了不必要的资源消耗,特别是文件较大或文件数量较多的情况下,性能损失更加明显。
缓存机制的核心思想是:第一次调用 md5_file() 时计算并保存结果,后续再次请求同一文件时直接从缓存获取哈希值,避免重复计算。
缓存可以存储于内存(如数组、静态变量)、文件或其他存储介质,视实际场景选择。
下面是一个基于内存缓存的简单示例,实现了对 md5_file() 结果的缓存:
<?php
class Md5FileCache {
private static $cache = [];
/**
* 獲取文件的MD5值,支持緩存
*
* @param string $filePath 文件路徑
* @return string|false 返回MD5字符串,失敗返回false
*/
public static function getMd5(string $filePath) {
// 先檢查緩存是否存在
if (isset(self::$cache[$filePath])) {
return self::$cache[$filePath];
}
// 緩存不存在,計算MD5
if (!file_exists($filePath)) {
return false;
}
$md5 = md5_file($filePath);
if ($md5 !== false) {
self::$cache[$filePath] = $md5;
}
return $md5;
}
}
// 使用示例
$file = '/path/to/your/file.txt';
$md5_1 = Md5FileCache::getMd5($file);
$md5_2 = Md5FileCache::getMd5($file); // 這裡直接從緩存取,避免重复計算
echo "MD5: " . $md5_1 . PHP_EOL;
?>
缓存结果可能因文件被修改而失效,因此应结合文件的修改时间 (filemtime()) 进行缓存验证。
<?php
class Md5FileCache {
private static $cache = [];
public static function getMd5(string $filePath) {
if (!file_exists($filePath)) {
return false;
}
$mtime = filemtime($filePath);
if (isset(self::$cache[$filePath]) && self::$cache[$filePath]['mtime'] === $mtime) {
return self::$cache[$filePath]['md5'];
}
$md5 = md5_file($filePath);
if ($md5 !== false) {
self::$cache[$filePath] = [
'md5' => $md5,
'mtime' => $mtime,
];
}
return $md5;
}
}
?>
如果程序长时间运行或需要跨请求缓存,可以