首頁 後端開發 php教程 php中使用檔案快取的例子

php中使用檔案快取的例子

Jul 25, 2016 am 08:45 AM

在web开发中,可以通过文件缓存,大大缓解数据库的压力。 如下代码是php中使用文件缓存的例子。

CacheLayer.php

  1. <?php
  2. class CacheLayer{
  3. protected $root = "";
  4. protected $cache = "";
  5. protected $key = "";
  6. protected $life = 0;
  7. public function __construct($key, $root = "/cachelayer"){
  8. $this->root = $_SERVER["DOCUMENT_ROOT"].$root;
  9. $this->key = $key;
  10. }
  11. public function expired($life_span){
  12. $this->life = $life_span;
  13. $file = $this->root."/".$this->key.".cachelayer";
  14. if(is_file($file)){
  15. $mtime = filemtime($file);
  16. return (time() >= ($mtime $this->life));
  17. }else{
  18. return true;
  19. }
  20. }
  21. public function put($content){
  22. $file = $this->root."/".$this->key.".cachelayer";
  23. if(!is_dir(dirname($this->root))){
  24. return false;
  25. }
  26. $this->delete();
  27. $content = json_encode($content);
  28. return (bool)file_put_contents($file, $content);
  29. }
  30. public function get(){
  31. $file = $this->root."/".$this->key.".cachelayer";
  32. if(is_file($file)){
  33. return json_decode(file_get_contents($file), true);
  34. }
  35. return array();
  36. }
  37. public function delete(){
  38. $file = $this->root."/".$this->key.".cachelayer";
  39. if(is_file($file)){
  40. unlink($file);
  41. return true;
  42. }
  43. return false;
  44. }
  45. }
复制代码

example.php

  1. <?php
  2. // Load the cachelayer and the database connection (db connection is optional)
  3. require_once "CacheLayer.php";
  4. require_once "db_connection.php";
  5. // Create a instance of the cachelayer
  6. $cl_nav = new CacheLayer("navigation");
  7. // Check to see if the cache is expired (60 * 10 = 10 minutes)
  8. if($cl_nav->expired(60 * 10)){
  9. echo "Cache doesn't exist or is expired. Rebuilding...<br />";
  10. // if the cache is expired rebuild it
  11. $result = mysql_query("select id, title from navigation");
  12. $new_cache = array();
  13. while($row = mysql_fetch_assoc($result)){
  14. $new_cache[] = $row;
  15. }
  16. // Save the array into the cache
  17. $cl_nav->put($new_cache);
  18. }
  19. echo "Loading from cache...<br />";
  20. // Get the cache
  21. $cache = $cl_nav->get();
  22. // Display the cache
  23. foreach($cache as $row){
  24. $id = $row["id"];
  25. $title = $row["title"];
  26. echo "<a href='/$id/$title'>$title</a><br />";
  27. }
复制代码

php


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

11個最佳PHP URL縮短腳本(免費和高級) 11個最佳PHP URL縮短腳本(免費和高級) Mar 03, 2025 am 10:49 AM

11個最佳PHP URL縮短腳本(免費和高級)

在Laravel中使用Flash會話數據 在Laravel中使用Flash會話數據 Mar 12, 2025 pm 05:08 PM

在Laravel中使用Flash會話數據

簡化的HTTP響應在Laravel測試中模擬了 簡化的HTTP響應在Laravel測試中模擬了 Mar 12, 2025 pm 05:09 PM

簡化的HTTP響應在Laravel測試中模擬了

Instagram API簡介 Instagram API簡介 Mar 02, 2025 am 09:32 AM

Instagram API簡介

構建具有Laravel後端的React應用程序:第2部分,React 構建具有Laravel後端的React應用程序:第2部分,React Mar 04, 2025 am 09:33 AM

構建具有Laravel後端的React應用程序:第2部分,React

php中的捲曲:如何在REST API中使用PHP捲曲擴展 php中的捲曲:如何在REST API中使用PHP捲曲擴展 Mar 14, 2025 am 11:42 AM

php中的捲曲:如何在REST API中使用PHP捲曲擴展

在Codecanyon上的12個最佳PHP聊天腳本 在Codecanyon上的12個最佳PHP聊天腳本 Mar 13, 2025 pm 12:08 PM

在Codecanyon上的12個最佳PHP聊天腳本

宣布 2025 年 PHP 形勢調查 宣布 2025 年 PHP 形勢調查 Mar 03, 2025 pm 04:20 PM

宣布 2025 年 PHP 形勢調查

See all articles