有些信息比方經常不變的,但是還是能變的信息放在緩存中以加快顯示速度,這是很有價值的,所謂的緩存,通俗的理解就是一些保存在服務器端的共用信息.它是于服務器同生死的,我們在保存緩存的時候可以指定下次更新的時間的判斷,比方要在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代碼如下:
- <?php
- class CacheException extends Exception {}
- /**
- * 緩存抽象類
- */
- abstract class Cache_Abstract {
- /**
- * 讀緩存變量
- *
- * @param string $key 緩存下標
- * @return mixed
- */
- abstract public function fetch($key);
- /**
- * 緩存變量
- *
- * @param string $key 緩存變量下標
- * @param string $value 緩存變量的值
- * @return bool
- */
- abstract public function store($key, $value);
- /**
- * 刪除緩存變量
- *
- * @param string $key 緩存下標
- * @return Cache_Abstract
- */
- abstract public function delete($key);
- /**
- * 清(刪)除所有緩存
- *
- * @return Cache_Abstract
- */
- abstract public function clear();
- /**
- * 鎖定緩存變量
- *
- * @param string $key 緩存下標
- * @return Cache_Abstract
- */
- abstract public function lock($key);
- /**
- * 緩存變量解鎖
- *
- * @param string $key 緩存下標
- * @return Cache_Abstract
- */
- abstract public function unlock($key);
- /**
- * 取得緩存變量是否被鎖定
- *
- * @param string $key 緩存下標
- * @return bool
- */
- abstract public function isLocked($key);
- /**
- * 確保不是鎖定狀態
- * 最多做$tries次睡眠等待解鎖,超時則跳過并解鎖
- *
- * @param string $key 緩存下標
- */
- public function checkLock($key) {
- if (!$this->isLocked($key)) {
- return $this;
- }
- $tries = 10;
- $count = 0;
- do {
- usleep(200);
- $count ++;
- } while ($count <= $tries && $this->isLocked($key)); // 最多做十次睡眠等待解鎖,超時則跳過并解鎖
- $this->isLocked($key) && $this->unlock($key);
- return $this;
- }
- }
- /**
- * APC擴展緩存實現
- *
- *
- * @category Mjie
- * @package Cache
- * @author 流水孟春
- * @copyright Copyright (c) 2008- <cmpan(at)qq.com>
- * @license New BSD License
- * @version $Id: Cache/Apc.php 版本號 2010-04-18 23:02 cmpan $
- */
- class Cache_Apc extends Cache_Abstract {
- protected $_prefix = 'cache.mjie.net';
- public function __construct() {
- if (!function_exists('apc_cache_info')) {
- throw new CacheException('apc extension didn't installed');
- }
- }
- /**
- * 保存緩存變量
- *
- * @param string $key
- * @param mixed $value
- * @return bool
- */
- public function store($key, $value) {
- return apc_store($this->_storageKey($key), $value);
- }
- /**
- * 讀取緩存
- *
- * @param string $key
- * @return mixed
- */
- public function fetch($key) {
- return apc_fetch($this->_storageKey($key));
- }
- /**
- * 清除緩存
- *
- * @return Cache_Apc
- */
- public function clear() {
- apc_clear_cache();
- return $this;
- }
- /**
- * 刪除緩存單元
- *
- * @return Cache_Apc
- */
- public function delete($key) {
- apc_delete($this->_storageKey($key));
- return $this;
- }
- /**
- * 緩存單元是否被鎖定
- *
- * @param string $key
- * @return bool
- */
- public function isLocked($key) {
- if ((apc_fetch($this->_storageKey($key) . '.lock')) === false) {
- return false;
- }
- return true;
- }
- /**
- * 鎖定緩存單元
- *
- * @param string $key
- * @return Cache_Apc
- */
- public function lock($key) {
- apc_store($this->_storageKey($key) . '.lock', '', 5);
- return $this;
- }
- /**
- * 緩存單元解鎖
- *
- * @param string $key
- * @return Cache_Apc
- */
- public function unlock($key) {
- apc_delete($this->_storageKey($key) . '.lock');
- return $this;
- }
- /**
- * 完整緩存名
- *
- * @param string $key
- * @return string
- */
- private function _storageKey($key) {
- return $this->_prefix . '_' . $key;
- }
- }
- /**
- * 文件緩存實現
- *
- *
- * @category Mjie
- * @package Cache
- * @author 流水孟春
- * @copyright Copyright (c) 2008- <cmpan(at)qq.com>
- * @license New BSD License
- * @version $Id: Cache/File.php 版本號 2010-04-18 16:46 cmpan $
- */
- class Cache_File extends Cache_Abstract {
- public $useSubdir = false;
- protected $_cachesDir = 'cache';
- public function __construct() {
- if (defined('DATA_DIR')) {
- $this->_setCacheDir(DATA_DIR . '/cache');
- }
- }
- /**
- * 獲取緩存文件
- *
- * @param string $key
- * @return string
- */
- protected function _getCacheFile($key) {
- $subdir = $this->useSubdir ? substr($key, 0, 2) . '/' : '';
- return $this->_cachesDir . '/' . $subdir . $key . '.php';
- }
- /**
- * 讀取緩存變量
- * 為防止信息泄露,緩存文件格式為php文件,并以"<?php exit;?>"開頭
- *
- * @param string $key 緩存下標
- * @return mixed
- */
- public function fetch($key) {
- $cacheFile = self::_getCacheFile($key);
- if (file_exists($cacheFile) && is_readable($cacheFile)) {
- // include 方式
- //return include $cacheFile;
- // 系列化方式
- return unserialize(@file_get_contents($cacheFile, false, NULL, 13));
- }
- return false;
- }
- /**
- * 緩存變量
- * 為防止信息泄露,緩存文件格式為php文件,并以"<?php exit;?>"開頭
- *
- * @param string $key 緩存變量下標
- * @param string $value 緩存變量的值
- * @return bool
- */
- public function store($key, $value) {
- $cacheFile = self::_getCacheFile($key);
- $cacheDir = dirname($cacheFile);
- if(!is_dir($cacheDir)) {
- if(!@mkdir($cacheDir, 0755, true)) {
- throw new CacheException("Could not make cache directory");
- }
- }
- // 用include方式
- //return @file_put_contents($cacheFile, '<?php return ' . var_export($value, true). ';');
- return @file_put_contents($cacheFile, '<?php exit;?>' . serialize($value));
- }
- /**
- * 刪除緩存變量
- *
- * @param string $key 緩存下標
- * @return Cache_File
- */
- public function delete($key) {
- if(emptyempty($key)) {
- throw new CacheException("Missing argument 1 for Cache_File::delete()");
- }
- $cacheFile = self::_getCacheFile($key);
- if(!@unlink($cacheFile)) {
- throw new CacheException("Cache file could not be deleted");
- }
- return $this;
- }
- /**
- * 緩存單元是否已經鎖定
- *
- * @param string $key
- * @return bool
- */
- public function isLocked($key) {
- $cacheFile = self::_getCacheFile($key);
- clearstatcache();
- return file_exists($cacheFile . '.lock');
- }
- /**
- * 鎖定
- *
- * @param string $key
- * @return Cache_File
- */
- public function lock($key) {
- //開源代碼Vevb.com
- $cacheFile = self::_getCacheFile($key);
- $cacheDir = dirname($cacheFile);
- if(!is_dir($cacheDir)) {
- if(!@mkdir($cacheDir, 0755, true)) {
- if(!is_dir($cacheDir)) {
- throw new CacheException("Could not make cache directory");
- }
- }
- }
- // 設定緩存鎖文件的訪問和修改時間
- @touch($cacheFile . '.lock');
- return $this;
- }
- /**
- * 解鎖
- *
- * @param string $key
- * @return Cache_File
- */
- public function unlock($key) {
- $cacheFile = self::_getCacheFile($key);
- @unlink($cacheFile . '.lock');
- return
- ?>
新聞熱點
疑難解答