Laravel使用Caching緩存數(shù)據(jù)減輕數(shù)據(jù)庫查詢壓力的方法
本文實例講述了Laravel使用Caching緩存數(shù)據(jù)減輕數(shù)據(jù)庫查詢壓力的方法。分享給大家供大家參考,具體如下:
昨天想把自己博客的首頁做一下緩存,達到類似于生成靜態(tài)頁緩存的效果,在群里問了大家怎么做緩存,都挺忙的沒多少回復,我就自己去看了看文檔,發(fā)現(xiàn)了Caching這個部分,其實之前也有印象,但是沒具體接觸過,顧名思義,就是緩存了,那肯定和我的需求有點聯(lián)系,我就認真看了看,發(fā)現(xiàn)的確是太強大了,經過很簡單的幾個步驟,我就改裝好了首頁,用firebug測試了一下,提高了幾十毫秒解析時間,當然了有人會笑這有必要嗎,豈不是閑的慌?其實我想這是有必要的,只是在我這里一來訪問人少(其實根本沒人還,嘿嘿....),二來我在首頁里做的查詢目前還挺少,就一次,就是取得所有博文,如果一個頁面里面有個七八次乃至十多次查詢,我想這個效果應該就很明顯了吧!(當然了,Raymond哥還有提到用更高級的專用緩存去做(memcached之類吧貌似),這是要自己能取得服務器控制權,能自由安裝軟件或者服務器本來就有這些緩存機制的情況下才能實現(xiàn)的,我需求比較簡單,也沒有這個環(huán)境去做,所以這里就不考慮了)
閑話少說,開始吧,先說說我的具體需求:
一. 實現(xiàn)首頁的數(shù)據(jù)緩存,如果有沒過期的緩存,就不查數(shù)據(jù)庫,這樣基本模擬出靜態(tài)頁的效果(當然了,其實還是要經過php處理的)
二. 實現(xiàn)刷新指定緩存的功能(這里只有首頁,就單指刷新首頁緩存了,這個功能,我做到了admin模塊下
具體實現(xiàn):
一. 查閱文檔,找到能幫我實現(xiàn)需求的模塊
我查了一下文檔,發(fā)現(xiàn)了有Caching這樣一個模塊,顧名思義,就是緩存了,那它能否幫到我呢,看看先:
1. http://laravel.com/docs/cache/config 這里是laravel的Caching模塊的實現(xiàn)
2. 文檔中有如下描述:
The Basics Imagine your application displays the ten most popular songs as voted on by your users. Do you really need to look up these ten songs every time someone visits your site? What if you could store them for 10 minutes, or even an hour, allowing you to dramatically speed up your application? Laravel's caching makes it simple.
我簡單理解為:
假設你的應用展示了用戶投票最多的10首流行歌曲,你真的需要在每個人訪問你的網站的時候都去查一遍這10首歌嗎?如果你想按10分鐘或者是一小時的頻率來緩存查詢結果來加速你的應用,Laravel 的 caching緩存模塊能將使工作變得異常簡單.
嗯,從這段話,我已經了解到這完全符合我現(xiàn)在的需求了,接下來我只需要找到對應的使用方法和API,一步一步來就行了.
二. 學習相應API等
1. 還是上面文檔,里面接著向下看,有如下描述:
By default, Laravel is configured to use the file system cache driver. It's ready to go out of the box with no configuration. The file system driver stores cached items as files in the cache directory. If you're satisfied with this driver, no other configuration is required. You're ready to start using it.
我簡單理解為:
默認情況下,Laravel使用文件系統(tǒng)作為緩存的驅動, 這是不需配置就可使用的, 文件系統(tǒng)驅動會將緩存的數(shù)據(jù)存入緩存目錄下的文件里面去, 如果你覺得合適的話不需要做任何其他的配置直接開始用就行了.
當然了, 這也是符合我的想法的, 其實我就是想把頁面緩存成靜態(tài)頁文件, 用戶再次訪問時直接輸出緩存的靜態(tài)頁就ok了, 如果需要更高級的需求, 還可以使用其他的驅動,有數(shù)據(jù)庫驅動, memcached, redis驅動等, 很好很強大!
2. 接下來查看用例,找到使用方法
用例文檔在這: http://laravel.com/docs/cache/usage
可以看出, 里面有 get, put, forever, remember, has, forget 等方法,這些方法使用也是基本上能 "望文生義" 就能搞定的,呵呵
具體使用方法文檔里面已經說的夠詳細, 使用方法一目了然我就不細說了, 只在代碼里面說吧
三. 具體實現(xiàn)
1. 我首頁之前的代碼
class Home_Controller extends Base_Controller { public function get_index() { $posts = Post::with('user') ->join('users', 'users.id', '=', 'posts.post_author') -> order_by('posts.created_at', 'desc') ->get(array('posts.id', 'posts.support', 'posts.against', 'users.username', 'posts.post_author', 'posts.post_title', 'posts.post_body')); $data = array(); foreach($posts as $p){ $data[] = array( 'id' => $p -> id, 'support' => $p -> support, 'against' => $p -> against, 'username'=> $p -> username, 'post_author' => $p -> post_author, 'post_title' => $p -> post_title, 'post_body' => $p -> post_body ); } return View::make('home.index') -> with('posts', $data); } }
這是我首頁的controller,作用只有一個, 就是從博文表里面取得所有博文, 然后輸出, 每次有人訪問, 都要查表, 如果沒有發(fā)表新的博文, 也要查表, 的確有很多不必要的開銷
2. 下面是我改裝之后的代碼:
class Home_Controller extends Base_Controller { public function get_index() { // 添加靜態(tài)緩存支持 // 如果不存在靜態(tài)頁緩存就立即緩存 if ( !Cache::has('staticPageCache_home') ) { $data = array(); $posts = Post::with('user') ->join('users', 'users.id', '=', 'posts.post_author') -> order_by('posts.created_at', 'desc') ->get(array('posts.id', 'posts.support', 'posts.against', 'users.username', 'posts.post_author', 'posts.post_title', 'posts.post_body')); foreach($posts as $p){ $data[] = array( 'id' => $p -> id, 'support' => $p -> support, 'against' => $p -> against, 'username'=> $p -> username, 'post_author' => $p -> post_author, 'post_title' => $p -> post_title, 'post_body' => $p -> post_body ); } $res = View::make('home.index') -> with('posts', $data); Cache::forever('staticPageCache_home', $res); } // 返回緩存的數(shù)據(jù) return Cache::get('staticPageCache_home'); } }
這里我用到了三個api
1). Cache::has ,這個判斷是說如果當前不存在 staticPageCache_home 這個名字的緩存, 就立即去取數(shù)據(jù)
2). Cache::forever, 這個從用例文檔里面可知是"永久緩存"的意思, 因為我一般都是很勤勞的,如果發(fā)表了博文,自己再去后臺立即刷新一下緩存就好了, 所以不需要設置過期啊失效時間之類的, 當然這個是要按各自的具體需求來的
3). Cache::get , 這句是從緩存里面取出 staticPageCache_home 這個名字的緩存, 然后作為響應內容返回
嗯, 就這么簡單, 呵呵, 一個基本的緩存功能就完成了, laravel的確是不錯地!
3. 為后臺添加刷新緩存功能
還是貼代碼吧, 不過也很簡單:
// 刷新首頁緩存(暫時只支持首頁) public function get_refreshcache() { /* @var $GID admin組id */ $GID = 1; if ( Auth::user() -> gid === 1 ) { $data = array(); $posts = Post::with('user') ->join('users', 'users.id', '=', 'posts.post_author') -> order_by('posts.created_at', 'desc') ->get(array('posts.id', 'posts.support', 'posts.against', 'users.username', 'posts.post_author', 'posts.post_title', 'posts.post_body')); foreach($posts as $p){ $data[] = array( 'id' => $p -> id, 'support' => $p -> support, 'against' => $p -> against, 'username'=> $p -> username, 'post_author' => $p -> post_author, 'post_title' => $p -> post_title, 'post_body' => $p -> post_body ); } $res = View::make('home.index') -> with('posts', $data); Cache::forever('staticPageCache_home', $res); return '刷新首頁緩存成功!'; } return '對不起,只有管理員組才可進行此操作!'; }
我給后臺添加了一個項目, 對應這個方法, 方法內容和首頁的大同小異, 取數(shù)據(jù), 然后Cache::forever 刷新一下緩存,就這么簡單,當然了,上面的Auth::user() 判斷是個簡單的判斷,只有管理員組才能進行刷新操作,呵呵
嗯, 全部內容就這么多, 很簡單, 歡迎童鞋們拍磚指正!
更多關于Laravel相關內容感興趣的讀者可查看本站專題:《Laravel框架入門與進階教程》、《php優(yōu)秀開發(fā)框架總結》、《smarty模板入門基礎教程》、《php日期與時間用法總結》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Laravel框架的PHP程序設計有所幫助。
相關文章
UPUPW 更新 64 位 Apache 系列 PHP 7.0 正式版
這篇文章主要介紹了UPUPW 更新 64 位 Apache 系列 PHP 7.0 正式版的相關資料,需要的朋友可以參考下2015-12-12ThinkPHP模板范圍判斷輸出In標簽與Range標簽用法詳解
這篇文章主要介紹了ThinkPHP模板范圍判斷輸出In標簽與Range標簽用法,需要的朋友可以參考下2014-06-06PHP+Redis事務解決高并發(fā)下商品超賣問題(推薦)
這篇文章主要介紹了PHP+Redis事務解決高并發(fā)下商品超賣問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08Laravel中創(chuàng)建Zip壓縮文件并提供下載
在Laravel中,創(chuàng)建Zip壓縮文件并提供下載是一個常見的需求,首先,我們需要確保服務器上安裝了Zip擴展,然后使用ZipArchive類創(chuàng)建 Zip文件并添加需要壓縮的文件和目錄,最后提供直接下載或預覽后下載的方式供用戶下載,本文將介紹并提供詳細的步驟和代碼示例2024-03-03