国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php緩存技術詳細介紹及php緩存實現代碼

2024-05-04 21:48:41
字體:
來源:轉載
供稿:網友

有些信息比方經常不變的,但是還是能變的信息放在緩存中以加快顯示速度,這是很有價值的,所謂的緩存,通俗的理解就是一些保存在服務器端的共用信息.它是于服務器同生死的,我們在保存緩存的時候可以指定下次更新的時間的判斷,比方要在5分鐘更新一次

數據緩存:這里所說的數據緩存是指數據庫查詢PHP緩存機制,每次訪問頁面的時候,都會先檢測相應的緩存數據是否存在,如果不存在,就連接數據庫,得到數據,并把查詢結果序列化后保存到文件中,以后同樣的查詢結果就直接從緩存表或文件中獲得。

用的最廣的例子看Discuz的搜索功能,把結果ID緩存到一個表中,下次搜索相同關鍵字時先搜索緩存表。

舉個常用的方法,多表關聯的時候,把附表中的內容生成數組保存到主表的一個字段中,需要的時候數組分解一下,這樣的好處是只讀一個表,壞處就是兩個數據同步會多不少步驟,數據庫永遠是瓶頸,用硬盤換速度,是這個的關鍵點。

頁面緩存:每次訪問頁面的時候,都會先檢測相應的緩存頁面文件是否存在,如果不存在,就連接數據庫,得到數據,顯示頁面并同時生成緩存頁面文件,這樣下次訪問的時候頁面文件就發揮作用了。(模板引擎和網上常見的一些PHP緩存機制類通常有此功能)

時間觸發緩存:檢查文件是否存在并且時間戳小于設置的過期時間,如果文件修改的時間戳比當前時間戳減去過期時間戳大,那么就用緩存,否則更新緩存.

內容觸發緩存:當插入數據或更新數據時,強制更新PHP緩存機制.

靜態緩存:這里所說的靜態緩存是指靜態化,直接生成HTML或XML等文本文件,有更新的時候重生成一次,適合于不太變化的頁面,這就不說了.

以上內容是代碼級的解決方案,我直接CP別的框架,也懶得改,內容都差不多,很容易就做到,而且會幾種方式一起用,但下面的內容是服務器端的緩存方案,非代碼級的,要有多方的合作才能做到.

內存緩存:Memcached是高性能的,分布式的內存對象PHP緩存機制系統,用于在動態應用中減少數據庫負載,提升訪問速度.

php的緩沖器:有eaccelerator,apc,phpa,xcache,這個這個就不說了吧,搜索一堆一堆的,自己看啦,知道有這玩意就OK.

MYSQL緩存:這也算非代碼級的,經典的數據庫就是用的這種方式,看下面的運行時間,0.09xxx之類的.

我貼段根據藍色那家伙修改后部分my.ini吧,2G的MYISAM表可以在0.05S左右,據說他前后改了有快一年.

基于反向代理的Web緩存:如Nginx,SQUID,mod_proxy(apache2以上又分為mod_proxy和mod_cache)

NGINX的例子,用google找到一些php緩存技術方法,發個PHP緩存實現,實現了apc和文件緩存,繼承Cache_Abstract即可實現調用第三方的緩存工具.

參考shindig的緩存類和apc,Php代碼如下:

  1. <?php    
  2. class CacheException extends Exception {}    
  3. /**   
  4.  * 緩存抽象類   
  5.  */    
  6. abstract class Cache_Abstract {    
  7.     /**   
  8.      * 讀緩存變量   
  9.      *   
  10.      * @param string $key 緩存下標   
  11.      * @return mixed   
  12.      */    
  13.     abstract public function fetch($key);    
  14.         
  15.     /**   
  16.      * 緩存變量   
  17.      *   
  18.      * @param string $key 緩存變量下標   
  19.      * @param string $value 緩存變量的值   
  20.      * @return bool   
  21.      */    
  22.     abstract public function store($key$value);    
  23.         
  24.     /**   
  25.      * 刪除緩存變量   
  26.      *   
  27.      * @param string $key 緩存下標   
  28.      * @return Cache_Abstract   
  29.      */    
  30.     abstract public function delete($key);    
  31.         
  32.     /**   
  33.      * 清(刪)除所有緩存   
  34.      *   
  35.      * @return Cache_Abstract   
  36.      */    
  37.     abstract public function clear();    
  38.         
  39.     /**   
  40.      * 鎖定緩存變量   
  41.      *   
  42.      * @param string $key 緩存下標   
  43.      * @return Cache_Abstract   
  44.      */    
  45.     abstract public function lock($key);    
  46.     
  47.     /**   
  48.      * 緩存變量解鎖   
  49.      *   
  50.      * @param string $key 緩存下標   
  51.      * @return Cache_Abstract   
  52.      */    
  53.     abstract public function unlock($key);    
  54.     
  55.     /**   
  56.      * 取得緩存變量是否被鎖定   
  57.      *   
  58.      * @param string $key 緩存下標   
  59.      * @return bool   
  60.      */    
  61.     abstract public function isLocked($key);    
  62.     
  63.     /**   
  64.      * 確保不是鎖定狀態   
  65.      * 最多做$tries次睡眠等待解鎖,超時則跳過并解鎖   
  66.      *   
  67.      * @param string $key 緩存下標   
  68.      */    
  69.     public function checkLock($key) {    
  70.         if (!$this->isLocked($key)) {    
  71.             return $this;    
  72.         }    
  73.             
  74.         $tries = 10;    
  75.         $count = 0;    
  76.         do {    
  77.             usleep(200);    
  78.             $count ++;    
  79.         } while ($count <= $tries && $this->isLocked($key));  // 最多做十次睡眠等待解鎖,超時則跳過并解鎖    
  80.     
  81.         $this->isLocked($key) && $this->unlock($key);    
  82.             
  83.         return $this;    
  84.     }    
  85. }    
  86.     
  87.     
  88. /**   
  89.  * APC擴展緩存實現   
  90.  *    
  91.  *    
  92.  * @category   Mjie   
  93.  * @package    Cache   
  94.  * @author     流水孟春   
  95.  * @copyright  Copyright (c) 2008- <cmpan(at)qq.com>   
  96.  * @license    New BSD License   
  97.  * @version    $Id: Cache/Apc.php 版本號 2010-04-18 23:02 cmpan $   
  98.  */    
  99. class Cache_Apc extends Cache_Abstract {    
  100.         
  101.     protected $_prefix = 'cache.mjie.net';    
  102.         
  103.     public function __construct() {    
  104.         if (!function_exists('apc_cache_info')) {    
  105.             throw new CacheException('apc extension didn't installed');    
  106.         }    
  107.     }    
  108.         
  109.     /**   
  110.      * 保存緩存變量   
  111.      *   
  112.      * @param string $key   
  113.      * @param mixed $value   
  114.      * @return bool   
  115.      */    
  116.     public function store($key$value) {    
  117.         return apc_store($this->_storageKey($key), $value);    
  118.     }    
  119.         
  120.     /**   
  121.      * 讀取緩存   
  122.      *   
  123.      * @param string $key   
  124.      * @return mixed   
  125.      */    
  126.     public function fetch($key) {    
  127.         return apc_fetch($this->_storageKey($key));    
  128.     }    
  129.         
  130.     /**   
  131.      * 清除緩存   
  132.      *   
  133.      * @return Cache_Apc   
  134.      */    
  135.     public function clear() {    
  136.         apc_clear_cache();    
  137.         return $this;    
  138.     }    
  139.         
  140.     /**   
  141.      * 刪除緩存單元   
  142.      *   
  143.      * @return Cache_Apc   
  144.      */    
  145.     public function delete($key) {    
  146.         apc_delete($this->_storageKey($key));    
  147.         return $this;    
  148.     }    
  149.         
  150.     /**   
  151.      * 緩存單元是否被鎖定   
  152.      *   
  153.      * @param string $key   
  154.      * @return bool   
  155.      */    
  156.     public function isLocked($key) {    
  157.         if ((apc_fetch($this->_storageKey($key) . '.lock')) === false) {    
  158.             return false;    
  159.         }    
  160.             
  161.         return true;    
  162.     }    
  163.         
  164.     /**   
  165.      * 鎖定緩存單元   
  166.      *   
  167.      * @param string $key   
  168.      * @return Cache_Apc   
  169.      */    
  170.     public function lock($key) {    
  171.         apc_store($this->_storageKey($key) . '.lock''', 5);    
  172.         return $this;    
  173.     }    
  174.         
  175.     /**   
  176.      * 緩存單元解鎖   
  177.      *   
  178.      * @param string $key   
  179.      * @return Cache_Apc   
  180.      */    
  181.     public function unlock($key) {    
  182.         apc_delete($this->_storageKey($key) . '.lock');    
  183.         return $this;    
  184.     }    
  185.         
  186.     /**   
  187.      * 完整緩存名   
  188.      *   
  189.      * @param string $key   
  190.      * @return string   
  191.      */    
  192.     private function _storageKey($key) {    
  193.         return $this->_prefix . '_' . $key;    
  194.     }    
  195. }    
  196.     
  197. /**   
  198.  * 文件緩存實現   
  199.  *    
  200.  *    
  201.  * @category   Mjie   
  202.  * @package    Cache   
  203.  * @author     流水孟春   
  204.  * @copyright  Copyright (c) 2008- <cmpan(at)qq.com>   
  205.  * @license    New BSD License   
  206.  * @version    $Id: Cache/File.php 版本號 2010-04-18 16:46 cmpan $   
  207.  */    
  208. class Cache_File extends Cache_Abstract {    
  209.     public $useSubdir     = false;    
  210.         
  211.     protected $_cachesDir = 'cache';    
  212.         
  213.     public function __construct() {    
  214.         if (defined('DATA_DIR')) {    
  215.             $this->_setCacheDir(DATA_DIR . '/cache');    
  216.         }    
  217.     }    
  218.         
  219.     /**   
  220.      * 獲取緩存文件   
  221.      *   
  222.      * @param string $key   
  223.      * @return string   
  224.      */    
  225.     protected function _getCacheFile($key) {    
  226.         $subdir = $this->useSubdir ? substr($key, 0, 2) . '/' : '';    
  227.         return $this->_cachesDir . '/' . $subdir . $key . '.php';    
  228.     }    
  229.     
  230.     /**   
  231.      * 讀取緩存變量   
  232.      * 為防止信息泄露,緩存文件格式為php文件,并以"<?php exit;?>"開頭   
  233.      *    
  234.      * @param string $key 緩存下標   
  235.      * @return mixed   
  236.      */    
  237.     public function fetch($key) {    
  238.         $cacheFile = self::_getCacheFile($key);    
  239.         if (file_exists($cacheFile) && is_readable($cacheFile)) {    
  240.             // include 方式    
  241.             //return include $cacheFile;    
  242.             // 系列化方式    
  243.     
  244.             return unserialize(@file_get_contents($cacheFile, false, NULL, 13));    
  245.         }    
  246.     
  247.         return false;    
  248.     }    
  249.     
  250.     /**   
  251.      * 緩存變量   
  252.      * 為防止信息泄露,緩存文件格式為php文件,并以"<?php exit;?>"開頭   
  253.      *   
  254.      * @param string $key 緩存變量下標   
  255.      * @param string $value 緩存變量的值   
  256.      * @return bool   
  257.      */    
  258.     public function store($key$value) {    
  259.         $cacheFile = self::_getCacheFile($key);    
  260.         $cacheDir  = dirname($cacheFile);    
  261.     
  262.         if(!is_dir($cacheDir)) {    
  263.             if(!@mkdir($cacheDir, 0755, true)) {    
  264.                 throw new CacheException("Could not make cache directory");   
  265.             }    
  266.         }    
  267.     // 用include方式    
  268.         //return @file_put_contents($cacheFile, '<?php return ' . var_export($value, true). ';');    
  269.     
  270.         return @file_put_contents($cacheFile'<?php exit;?>' . serialize($value));    
  271.     }    
  272.     
  273.     /**   
  274.      * 刪除緩存變量   
  275.      *   
  276.      * @param string $key 緩存下標   
  277.      * @return Cache_File   
  278.      */    
  279.     public function delete($key) {    
  280.         if(emptyempty($key)) {    
  281.             throw new CacheException("Missing argument 1 for Cache_File::delete()");    
  282.         }    
  283.             
  284.         $cacheFile = self::_getCacheFile($key);    
  285.         if(!@unlink($cacheFile)) {    
  286.             throw new CacheException("Cache file could not be deleted");    
  287.         }    
  288.     
  289.         return $this;    
  290.     }    
  291.     
  292.     /**   
  293.      * 緩存單元是否已經鎖定   
  294.      *   
  295.      * @param string $key   
  296.      * @return bool   
  297.      */    
  298.     public function isLocked($key) {    
  299.         $cacheFile = self::_getCacheFile($key);    
  300.         clearstatcache();    
  301.         return file_exists($cacheFile . '.lock');    
  302.     }    
  303.     
  304.     /**   
  305.      * 鎖定   
  306.      *   
  307.      * @param string $key   
  308.      * @return Cache_File   
  309.      */    
  310.     public function lock($key) {    
  311.     //開源代碼Vevb.com 
  312.         $cacheFile = self::_getCacheFile($key);    
  313.         $cacheDir  = dirname($cacheFile);    
  314.         if(!is_dir($cacheDir)) {    
  315.             if(!@mkdir($cacheDir, 0755, true)) {    
  316.                 if(!is_dir($cacheDir)) {    
  317.                     throw new CacheException("Could not make cache directory");    
  318.                 }    
  319.             }    
  320.         }    
  321.     
  322.         // 設定緩存鎖文件的訪問和修改時間    
  323.         @touch($cacheFile . '.lock');    
  324.         return $this;    
  325.     }    
  326.       
  327.     /**   
  328.      * 解鎖   
  329.      *   
  330.      * @param string $key   
  331.      * @return Cache_File   
  332.      */    
  333.     public function unlock($key) {    
  334.         $cacheFile = self::_getCacheFile($key);    
  335.         @unlink($cacheFile . '.lock');    
  336.         return 
  337. ?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 陈巴尔虎旗| 九江市| 大石桥市| 彰武县| 东乌珠穆沁旗| 乌鲁木齐市| 神农架林区| 嘉善县| 竹北市| 延津县| 社会| 连山| 本溪市| 浮山县| 巴彦县| 新建县| 抚远县| 曲靖市| 腾冲县| 夏津县| 甘谷县| 乐都县| 龙江县| 离岛区| 丰城市| 合水县| 岱山县| 隆安县| 峨边| 游戏| 苏尼特左旗| 鲁甸县| 武城县| 仲巴县| 泾阳县| 漳浦县| 津南区| 响水县| 永川市| 景宁| 永州市|