搜尋
首頁後端開發php教程php中apc和檔案快取類別的實作程式碼

  1. class CacheException extends Exception {}

  2. /**
  3. * 快取抽象類別
  4. */
  5. 抽象class Cache_tracts {
  6. /**
  7. * 讀取快取變數
  8. *
  9. * @param string $key 快取下標
  10. * @return mixed
  11. */
  12. 抽象公用函數fetch($key);
  13. /**

  14. * 快取變數
  15. *
  16. * @param string $key 快取變數下標
  17. * @param string $value 快取變數的值
  18. * @return bool
  19. */
  20. 抽象公用函數store( $key, $value);
  21. /**

  22. * 刪除快取變數
  23. *
  24. * @param string $key 快取下標
  25. * @return Cache_Abstract
  26. */
  27. 抽象公用函數delete($key);
  28. /**

  29. * 清(刪除)除所有快取
  30. *
  31. * @return Cache_Abstract
  32. */
  33. 抽象公用函數clear();
  34. /**

  35. * 鎖定快取變數
  36. *
  37. * @param string $key 快取下標
  38. * @return Cache_Abstract
  39. */
  40. 抽象公用函數lock ($key);
  41. /* *
  42. * 快取變數解鎖
  43. *
  44. * @param string $key 快取下標
  45. * @return Cache_Abstract
  46. */
  47. 抽象公用函數unlock($key);
  48. /**
  49. * 取得快取變數是否被鎖定
  50. *
  51. * @param string $key 快取下標
  52. * @return bool
  53. */
  54. 抽象公用函數isLocked($key) ;
  55. /**
  56. * 確保不是鎖定狀態
  57. * 最多做$tries次睡眠等待解鎖,超時則跳過並解鎖
  58. *
  59. * @param string $key 快取下標
  60. */
  61. public function checkLock($key) {
  62. if (!$this->isLocked($key)) {
  63. return $this;
  64. }
  65. $tries = 10;
  66. $count = 0;
  67. do {
  68. usleep(200);
  69. $count ++;
  70. } while ($count isLocked( $key)); // 最多做十次睡眠等待解鎖,超時則跳過並解鎖
  71. $this->isLocked($key) && $this->unlock($key);
  72. return $this ;
  73. }
  74. }
  75. /**

  76. * APC擴充快取實作
  77. *
  78. * @by bbs.it-home.org
  79. * @category Mjie
  80. * @package Cache
  81. * @category Mjie
  82. * @package Cache
  83. * @license New BSD License
  84. * @package Cache
  85. * @license New BSD License
  86. * @package Cache
  87. * @license New BSD License
  88. * @package Cache
  89. * @license New BSD License
  90. * @package Cache
  91. * @license New BSD License
  92. * @package Cache
  93. * @license New BSD License
  94. * @package Cache
  95. * @license New BSD >* @version $Id: Cache/Apc.php 版本號2010-04-18 23:02 cmpan $
  96. */
  97. class Cache_Apc extends Cache_Abstract {
  98. 受保護$_prefix = 'cache.mjie.net';

  99. public function __construct() {

  100. if (!function_exists('apc_cache_info')) {
  101. throw new Caches('apc_cache_info')) {
  102. throw new CacheException( '未未安裝apc 擴充功能');
  103. }
  104. }
  105. /**

  106. * 儲存快取變數
  107. *
  108. * @param string $key
  109. * @param mixed $value
  110. * @return bool
  111. */
  112. public function store($key, $value ) {
  113. return apc_store($this->_storageKey($key), $value);
  114. }
  115. /**

  116. * 讀取快取
  117. *
  118. * @param string $key
  119. * @return mixed
  120. */
  121. public function fetch($key) {
  122. return apc_fetch($this->_storageKey($key));
  123. }
  124. /**

  125. * 清除快取
  126. *
  127. * @return Cache_Apc
  128. */
  129. public functionclear() {
  130. apc_clear_cachec_clear_cachec_clear_cache ();
  131. return $this;
  132. }
  133. /**

  134. * 刪除快取單元
  135. *
  136. * @return Cache_Apc
  137. */
  138. public function delete($key) {
  139. apc_delete($this ->_storageKey($key));
  140. return $this;
  141. }
  142. /* *

  143. * 快取單元是否被鎖定
  144. *
  145. * @param string $key
  146. * @return bool
  147. */
  148. public function isLocked($key) {
  149. if ((apc_fetch($this->_storageKey($key) . '.lock')) === false) {
  150. 回傳false;
  151. }
  152. 回傳true;
  153. }
  154. /**

  155. * 鎖定快取單元
  156. *
  157. * @param string $key
  158. * @return Cache_Apc
  159. */public function lock($key) { apc_store($this->_storageKey($key) . '.lock' , '', 5); return $this; }

    /** * 快取單元解鎖 * * @param string $key * @return Cache_Apc*/公用函數解鎖($key) { apc_delete($this->_storageKey($key) .'.lock'); 回傳$this;}

  160. /**

  161. * 完整快取名稱
  162. *
  163. * @param string $key
  164. * @return string
  165. */
  166. 原生函數_storageKey($key) {
  167. 回傳$this->_prefix 。 }
  168. }
  169. /**
  170. * 檔案快取實作
  171. *
  172. * @by bbs.it-home.org
  173. * @category Mjie
  174. * @package Cache
  175. * @license New BSD License
  176. * @version $Id: Cache/File.php 版本號2010-04-18 16:46 cmpan $
  177. */
  178. class Cache_File extends Cache_Abstract {
  179. protected $_cachesDir = 'cache';

  180. public function __construct() {

  181. if (define('DATA_DIR')) {
  182. $this->_setCacheDir(DATA_DIR . '/cache');
  183. }
  184. }
  185. /**

  186. * 取得快取檔案
  187. *
  188. * @param string $key
  189. * @return string
  190. */
  191. 受保護函數_getCacheFile($key) {
  192. 回傳$this->_cachesDir 。 ( $key, 0, 2) 。 $ cacheFile = self::_getCacheFile($key);
  193. if (file_exists($cacheFile) && is_read($cacheFile)) {
  194. return unserialize(@file_get_contents($cacheFile, false, NULL, 13));
  195. }
  196. return false;
  197. }
  198. /**
  199. * 讀取快取變數
  200. * 為防止資訊洩露,快取文件格式為php文件,並以""開頭
  201. *
  202. * @param string $key 緩存下標
  203. * @return mixed
  204. */
  205. public function store($key, $value) {
  206. $cacheFile = self::_getCacheFile( $ key);
  207. $cacheDir = dirname($cacheFile);
  208. if(!is_dir($cacheDir)) {
  209. if([url=mailto:!@mkdir($cacheDir]!@mkdir($cacheDir[/url], 0755, true)) {
  210. throw new CacheException("無法創建緩存目錄");
  211. }
  212. }
  213. return @file_put_contents($cacheFile, '' .serialize($value)); }
  214. /**
  215. * 快取變數
  216. * 為防止資訊洩露,快取文件格式為php文件,並以""開頭
  217. *
  218. * @param string $key 快取變數下標
  219. * @param string $value 快取變數的值
  220. * @return bool
  221. */
  222. public function delete($key) {
  223. if(empty($key)) {
  224. throw new CacheException(" 缺少參數1 for Cache_File:: delete() ");
  225. }
  226. $cacheFile = self::_getCacheFile($key);
  227. if([url=mailto:!@unlink($cacheFile]!@ unlink($ cacheFile[/ url])) {
  228. throw new CacheException("快取檔案無法刪除");
  229. }
  230. return $this;
  231. }
  232. /**
  233. * 刪除快取變數
  234. *
  235. * @param string $key 快取下標
  236. * @return Cache_File
  237. */
  238. public function isLocked($key) {
  239. $cacheFile = self::_getCacheFile($key);
  240. clearstatcache();
  241. return file_exists($cacheFile . '.lock');
  242. >/**
  243. * 快取單元是否已鎖定
  244. *
  245. * @param string $key
  246. * @return bool
  247. */
  248. public function lock($key) {
  249. $cacheFile = self::_getCacheFile($key);
  250. $cacheDir = 目錄名稱($cacheFile) ;
  251. if(!is_dir($cacheDir)) {
  252. if([url=mailto:!@mkdir($cacheDir]!@mkdir($cacheDir[/url], 0755, true)) {
  253. if( !is_dir($cacheDir)) {
  254. throw new CacheException("無法建立快取目錄");
  255. }
  256. }
  257. }
  258. // 設定快取鎖定檔案的存取和修改時間
  259. @touch($cacheFile . '.lock');
  260. return $this;
  261. }
  262. /**
  263. * 鎖定
  264. *
  265. * @param string $key
  266. * @return Cache_File
  267. */
  268. 公用函數unlock($key) {
  269. $cacheFile = self: :_getCacheFile($key);
  270. @unlink($cacheFile . '.lock');
  271. return $this;
  272. }
  273. /**
  274. * 解鎖
  275. *
  276. * @param string $key
  277. * @return Cache_File
  278. */
  279. 受保護函數_setCacheDir($dir) {
  280. $this->_cachesDir = rtrim(str_replace('\', '/', trimrim ( $dir)), '/');
  281. clearstatcache();
  282. if(!is_dir($this->_cachesDir)) {
  283. mkdir($this->_cachesDir, 0755, true) ;
  284. }
  285. //
  286. return $this;
  287. }
  288. /**
  289. * 設定檔案快取目錄
  290. * @param string $dir
  291. * @return Cache_File
  292. */
  293. public functionclear() {
  294. // 提交目錄清除儲存
  295. $cacheDir = $this->_cachesDir;
  296. $d = dir($cacheDir);
  297. while(false !== ($entry = $d->read()) ) {
  298. if('.' == $entry[0]) {
  299. 繼續;
  300. }
  301. $cacheEntry = $cacheDir 。 '/' 。 $entry;
  302. if(is_file($cacheEntry)) {
  303. @unlink($cacheEntry);
  304. } elseif(is_dir($cacheEntry)) {
  305. // 儲存資料夾有兩級
  306. $d2 = dir($cacheEntry);
  307. while(false !== ($entry = $d2->read())) {
  308. if('.' == $entry[0] ) {
  309. 繼續;
  310. }
  311. $cacheEntry .= '/' 。 $entry;
  312. if(is_file($cacheEntry)) {
  313. @unlink($cacheEntry);
  314. }
  315. }
  316. $d2->close();
  317. }
  318. }
  319. }
  320. $d->close();
  321. return $this;
  322. }
  323. }
  324. /**
  325. * 快取單元的資料結構
  326. * array(
  327. * 'time' => time(), // 快取寫入時的時間戳記
  328. * 'expire' => $expire, / / 快取過期時間
  329. * 'valid' => true, // 快取是否有效
  330. * 'data' => $value // 快取的值
  331. * );
  332. */
  333. 最終類別快取{
  334. /**
  335. * 快取過期時間長度(s)
  336. *
  337. * @var int
  338. */
  339. 私有$_expire = 3600;
  340. /**
  341. * 快取處理類別
  342. *
  343. * @var Cache_Abstract
  344. */
  345. 私有$_storage = null;
  346. /**
  347. * @return 快取
  348. *//
  349. static public function createCache($cacheClass = 'Cache_File') {
  350. return new self($cacheClass);
  351. }
  352. private function __construct($cacheClass) {
  353. * 設定快取
  354. *
  355. * @param string $key
  356. * @param mixed $value
  357. * @param int $expire
  358. $ = new $cacheClass();
  359. }
  360. /**
  361. * 讀取快取
  362. *
  363. * @param string $key
  364. * @return mixed
  365. */
  366. public function set($key, $value, $expire = false) {
  367. if (! $expire) {
  368. $expire = $this->_expire;
  369. }
  370. $this->_storage->checkLock($key);
  371. $data = array('time' => time(), 'expire' => $expire, 'valid' => true, 'data' => $value);
  372. $this->_storage- >lock($key);
  373. 嘗試{
  374. $this->_storage->store($key, $data);
  375. $this->_storage->unlock ($key);
  376. } catch (CacheException $e) {
  377. $this->_storage->unlock($key);
  378. throw $e;
  379. }
  380. }
  381. /**
  382. * 讀取緩存,包括過期的和無效的,取得完整的存貯結構
  383. *
  384. * @param string $key
  385. */
  386. public function get($ key) {
  387. $data = $this->fetch($key);
  388. if ($data && $data[' valid'] && !$data['isExpired']) {
  389. return $ data['data'];
  390. }
  391. return false;
  392. }
  393. /**
  394. * 刪除快取
  395. *
  396. * @param string $key
  397. */
  398. public function fetch($key) {
  399. $ this->_storage->checkLock($key);
  400. $data = $this->_storage->fetch( $key);
  401. if ($data) {
  402. $data['isExpired'] = (time() - $data['time']) >; $data['過期'] ? true : false;
  403. return $data;
  404. }
  405. return false;
  406. }
  407. /**
  408. * 把快取設為無效
  409. *
  410. * @param string $key
  411. */
  412. public function delete($key) {
  413. $this->_storage->checkLock($key)
  414. ->lock($key)
  415. ->delete($key)
  416. ->unlock($key) ;
  417. }
  418. 公用函數clear() {

  419. $this->_storage-> clear();
  420. }
  421. /**
  422. * 設定快取過期時間(s)
  423. *
  424. * @param int $expire
  425. */
  426. public function setInvalidate($key) {
  427. $this->_storage->checkLock($key)
  428. -> lock($key);
  429. 嘗試{
  430. $data = $this->_storage->fetch($key);
  431. if ($data) {
  432. $data['valid'] = false;
  433. $this->; _storage->store($key, $data);
  434. }
  435. $this->_storage->unlock($key);
  436. } catch (CacheException $ e) {
  437. $ this->_storage->unlock($key);
  438. throw $e;
  439. }
  440. }
  441. /** */

  442. public function setExpire($expire) {
  443. $this->_expire = (int) $expire;
  444. return $this;
  445. }
  446. }
  447. ?> p>
複製程式碼


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
超越炒作:評估當今PHP的角色超越炒作:評估當今PHP的角色Apr 12, 2025 am 12:17 AM

PHP在現代編程中仍然是一個強大且廣泛使用的工具,尤其在web開發領域。 1)PHP易用且與數據庫集成無縫,是許多開發者的首選。 2)它支持動態內容生成和麵向對象編程,適合快速創建和維護網站。 3)PHP的性能可以通過緩存和優化數據庫查詢來提升,其廣泛的社區和豐富生態系統使其在當今技術棧中仍具重要地位。

PHP中的弱參考是什麼?什麼時候有用?PHP中的弱參考是什麼?什麼時候有用?Apr 12, 2025 am 12:13 AM

在PHP中,弱引用是通過WeakReference類實現的,不會阻止垃圾回收器回收對象。弱引用適用於緩存系統和事件監聽器等場景,需注意其不能保證對象存活,且垃圾回收可能延遲。

解釋PHP中的__ Invoke Magic方法。解釋PHP中的__ Invoke Magic方法。Apr 12, 2025 am 12:07 AM

\_\_invoke方法允許對象像函數一樣被調用。 1.定義\_\_invoke方法使對象可被調用。 2.使用$obj(...)語法時,PHP會執行\_\_invoke方法。 3.適用於日誌記錄和計算器等場景,提高代碼靈活性和可讀性。

解釋PHP 8.1中的纖維以進行並發。解釋PHP 8.1中的纖維以進行並發。Apr 12, 2025 am 12:05 AM

Fibers在PHP8.1中引入,提升了並發處理能力。 1)Fibers是一種輕量級的並發模型,類似於協程。 2)它們允許開發者手動控制任務的執行流,適合處理I/O密集型任務。 3)使用Fibers可以編寫更高效、響應性更強的代碼。

PHP社區:資源,支持和發展PHP社區:資源,支持和發展Apr 12, 2025 am 12:04 AM

PHP社區提供了豐富的資源和支持,幫助開發者成長。 1)資源包括官方文檔、教程、博客和開源項目如Laravel和Symfony。 2)支持可以通過StackOverflow、Reddit和Slack頻道獲得。 3)開發動態可以通過關注RFC了解。 4)融入社區可以通過積極參與、貢獻代碼和學習分享來實現。

PHP與Python:了解差異PHP與Python:了解差異Apr 11, 2025 am 12:15 AM

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

php:死亡還是簡單地適應?php:死亡還是簡單地適應?Apr 11, 2025 am 12:13 AM

PHP不是在消亡,而是在不斷適應和進化。 1)PHP從1994年起經歷多次版本迭代,適應新技術趨勢。 2)目前廣泛應用於電子商務、內容管理系統等領域。 3)PHP8引入JIT編譯器等功能,提升性能和現代化。 4)使用OPcache和遵循PSR-12標準可優化性能和代碼質量。

PHP的未來:改編和創新PHP的未來:改編和創新Apr 11, 2025 am 12:01 AM

PHP的未來將通過適應新技術趨勢和引入創新特性來實現:1)適應云計算、容器化和微服務架構,支持Docker和Kubernetes;2)引入JIT編譯器和枚舉類型,提升性能和數據處理效率;3)持續優化性能和推廣最佳實踐。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

mPDF

mPDF

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