没能理解的一个‘类’,求解释
以下代码是一个程序中的一个类文件(lock.php),先说下他lock.php实际的作用:
在未知(是我不理解)的情况下,会在缓存文件夹 cache下生成一个.lock文件,例如index.php.lock 如果出现这个index.php.lock文件,那网站的首页就打不开了,必须手动删除它,再刷新首页,出现正常的缓存文件index.php 才能正常打开系统。
这个故障是不定时发生的,我现在也不理解在什么情况下才会发生,不理解为什么源码的作者为什么要设置这个功能。因为不知道最终来源,也没办法找源码的作者咨询了。
前几天,我把这个类文件lock.php删除了,网站一直正常,我以为这样就不会再有index.php.lock 文件了,但今天系统又打不开了,发现正常的cache/index.php缓存不见了,把这个类文件还原,然后再刷新系统,出现正常的cache/index.php,系统也就正常了。
lock.php的源码如下:
<?php<br /> <br /> class lock{<br /> private $num,$lock_marker,$lock_file,$timeout;<br /> public $islock;<br /> private function file_timeout(){<br /> if(file_exists($this->lock_file)){<br /> if(time()-filemtime($this->lock_file)>=$this->timeout){<br /> @unlink($this->lock_file);<br /> return true;<br /> }<br /> return false;<br /> }else return true;<br /> }<br /> function __construct($num=1,$timeout=3,$wait=false,$wait_time=0,$marker = ''){<br /> $this->num=$num;<br /> $marker ||$marker = $_SERVER['SCRIPT_FILENAME'];<br /> $marker = md5($marker);<br /> $this->lock_marker = $marker;<br /> $this->timeout=$timeout;<br /> $this->lock_file=d('./cache/lock/'.$this->lock_marker.$this->num.'.lock');<br /> if(file_exists($this->lock_file)){<br /> $this->islock=!$this->file_timeout();<br /> }else $this->islock=false;<br /> if(!$this->islock)touch($this->lock_file);<br /> else {<br /> if($wait){<br /> $wait_start=0;<br /> while(!$this->file_timeout()){<br /> if($wait_time>0){<br /> $wait_start+=100000;<br /> if($wait_start>$wait_time)break;<br /> }<br /> }<br /> file_exists($this->lock_file)&&@unlink($this->lock_file);<br /> touch($this->lock_file);<br /> $this->islock=false;<br /> }<br /> }<br /> }<br /> function __destruct(){<br /> $this->close();<br /> }<br /> public function close(){<br /> !$this->islock&&file_exists($this->lock_file)&&@unlink($this->lock_file);<br /> }<br /> }<br /> <br /> ?>
------解决方案--------------------
我怎么觉得你这个代码不能正常运行呢?
if(!$this->islock) touch($this->lock_file);//修改缓存文件的访问时间
执行的条件是 $this->islock 为假
而 $this->islock 为假的一个情况是:
$this->islock=!$this->file_timeout();
即方法 $this->file_timeout 返回真
而 $this->file_timeout 方法返回真时有
@unlink($this->lock_file);//删除缓存文件
return true;
既然缓存文件被删除了,那么 touch($this->lock_file) 不就要报错了吗?
Warning: touch() [function.touch]: Unable to create file because Invalid argument
另外下面还有
file_exists($this->lock_file)&&@unlink($this->lock_file);//缓存文件存在就删除掉
touch($this->lock_file);//又在修改不存在的文件的访问时间

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

使用依賴注入(DI)的原因是它促進了代碼的松耦合、可測試性和可維護性。 1)使用構造函數注入依賴,2)避免使用服務定位器,3)利用依賴注入容器管理依賴,4)通過注入依賴提高測試性,5)避免過度注入依賴,6)考慮DI對性能的影響。

phpperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovessetimes.2)優化

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

TOOPTIMIZEPHPAPPLICITIONSFORPERSTORANCE,USECACHING,數據庫imization,opcodecaching和SererverConfiguration.1)InlumentCachingWithApcutCutoredSatfetchTimes.2)優化的atabasesbasesebasesebasesbasesbasesbaysbysbyIndexing,BeallancingAndWriteExing

依賴性注射inphpisadesignpatternthatenhancesFlexibility,可檢驗性和ManiaginabilybyByByByByByExternalDependencEctenceScoupling.itallowsforloosecoupling,EasiererTestingThroughMocking,andModularDesign,andModularDesign,butquirscarecarefulscarefullsstructoringDovairing voavoidOverOver-Inje

PHP性能優化可以通過以下步驟實現:1)在腳本頂部使用require_once或include_once減少文件加載次數;2)使用預處理語句和批處理減少數據庫查詢次數;3)配置OPcache進行opcode緩存;4)啟用並配置PHP-FPM優化進程管理;5)使用CDN分發靜態資源;6)使用Xdebug或Blackfire進行代碼性能分析;7)選擇高效的數據結構如數組;8)編寫模塊化代碼以優化執行。

opcodecachingsimplovesphperforvesphpermance bycachingCompiledCode,reducingServerLoadAndResponSetimes.1)itstorescompiledphpcodeinmemory,bypassingparsingparsingparsingandcompiling.2)useopcachebachebachebachebachebachebachebysettingparametersinphametersinphp.ini,likeememeryconmorysmorysmeryplement.33)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具