PHP Development တွင် MD5_File () function ကိုမကြာခဏအသုံးပြုသည်။ ဖိုင်တစ်ခု၏ MD5 hash တန်ဖိုးကိုရရှိရန်အသုံးပြုသည်။ သို့သော် MD5_File () သည် အကြိမ်များစွာခေါ်ဆိုရန်လိုအပ်သည့်အခါမကြာခဏဆိုသလို Disk IO စစ်ဆင်ရေးသည်ပရိုဂရမ်ကွပ်မျက်မှုစွမ်းဆောင်ရည်ကိုသိသိသာသာလျှော့ချနိုင်သည်။ ဤအဆုံးတွင်ကျွန်ုပ်တို့သည် MD5_File () function ၏လုပ်ဆောင်မှုများကို cache ကို cache လုပ်ခြင်းယန္တရားမှတဆင့်ဟုခေါ်သည့်အခါကွပ်မျက်မှုစွမ်းဆောင်ရည်ကိုတိုးတက်စေနိုင်သည်။
MD5_File () function ကိုခေါ်သည့်အခါတိုင်း၎င်းသည် disk ထဲမှဖိုင်တစ်ခုလုံးကိုဖတ်ပြီး MD5 တန်ဖိုးကိုတွက်ချက်သည်။ အကယ်. ဖိုင်အလားတူဖိုင်ကိုအကြိမ်ပေါင်းများစွာဟုခေါ်သည်ဆိုပါကဖိုင်အမျိုးအစားကိုထပ်ခါတလဲလဲဖတ်ပါ,
cache ယန္တရား၏အဓိကအယူအဆမှာ - MD5_File () ကို ပထမဆုံးအကြိမ်ခေါ်ဝေါ်စဉ်သည်ထပ်မံတွက်ချက်မှုကိုရှောင်ရှားရန်နောက်တစ်ကြိမ်ထပ်မံတောင်းခံသည့်အခါ,
Cache ကိုမှတ်ဉာဏ် (static variable များ), ဖိုင်များသို့မဟုတ်အခြားသိုလှောင်မှုမီဒီယာများကဲ့သို့မှတ်ဉာဏ်တွင်သိမ်းဆည်းထားနိုင်သည်။
MD5_File () ရလဒ်များကိုအကောင်အထည်ဖော်သည့် Memory Cache ကို အခြေခံ. Memory Cache ကို အခြေခံ. ရိုးရှင်းသောဥပမာတစ်ခုမှာ -
<?php
class Md5FileCache {
private static $cache = [];
/**
* ဖိုင်ကိုရယူပါMD5အဘိုး,Cache ကိုထောက်ပံ့ပါ
*
* @param string $filePath ဖိုင်လမ်းကြောင်း
* @return string|false ပြန်လာMD5ကြိုး,失败ပြန်လာfalse
*/
public static function getMd5(string $filePath) {
// cache ကိုပထမ ဦး ဆုံးတည်ရှိမရှိ, ရှိမရှိစစ်ဆေးပါ
if (isset(self::$cache[$filePath])) {
return self::$cache[$filePath];
}
// cache ကိုမတည်ရှိပါဘူး,ရေတွက်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); // ဒီမှာ cache ကိုကနေတိုက်ရိုက်ရယူပါ,避免重复ရေတွက်
echo "MD5: " . $md5_1 . PHP_EOL;
?>
ဖိုင်၏ပြုပြင်မွမ်းမံခြင်းကြောင့် cache ရရှိခြင်းကို invalidable ဖြစ်နိုင်သည်, ထို့ကြောင့် cache verification ကိုဖိုင်၏ပြုပြင်မွမ်းမံချိန်နှင့်ပေါင်းစပ်။ လုပ်ဆောင်သင့်သည် ( 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;
}
}
?>
အကယ်. ပရိုဂရမ်သည်အချိန်ကြာမြင့်စွာပြေးနေလျှင်သို့မဟုတ်တောင်းဆိုမှုများကိုဖြတ်ကျော်ရန်လိုအပ်ပါကသင်လုပ်နိုင်သည်