Laravel使用Caching緩存數(shù)據(jù)減輕數(shù)據(jù)庫查詢壓力的方法
本文實例講述了Laravel使用Caching緩存數(shù)據(jù)減輕數(shù)據(jù)庫查詢壓力的方法。分享給大家供大家參考,具體如下:
昨天想把自己博客的首頁做一下緩存,達到類似于生成靜態(tài)頁緩存的效果,在群里問了大家怎么做緩存,都挺忙的沒多少回復(fù),我就自己去看了看文檔,發(fā)現(xiàn)了Caching這個部分,其實之前也有印象,但是沒具體接觸過,顧名思義,就是緩存了,那肯定和我的需求有點聯(lián)系,我就認真看了看,發(fā)現(xiàn)的確是太強大了,經(jīng)過很簡單的幾個步驟,我就改裝好了首頁,用firebug測試了一下,提高了幾十毫秒解析時間,當然了有人會笑這有必要嗎,豈不是閑的慌?其實我想這是有必要的,只是在我這里一來訪問人少(其實根本沒人還,嘿嘿....),二來我在首頁里做的查詢目前還挺少,就一次,就是取得所有博文,如果一個頁面里面有個七八次乃至十多次查詢,我想這個效果應(yīng)該就很明顯了吧!(當然了,Raymond哥還有提到用更高級的專用緩存去做(memcached之類吧貌似),這是要自己能取得服務(wù)器控制權(quán),能自由安裝軟件或者服務(wù)器本來就有這些緩存機制的情況下才能實現(xiàn)的,我需求比較簡單,也沒有這個環(huán)境去做,所以這里就不考慮了)
閑話少說,開始吧,先說說我的具體需求:
一. 實現(xiàn)首頁的數(shù)據(jù)緩存,如果有沒過期的緩存,就不查數(shù)據(jù)庫,這樣基本模擬出靜態(tài)頁的效果(當然了,其實還是要經(jīng)過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.
我簡單理解為:
假設(shè)你的應(yīng)用展示了用戶投票最多的10首流行歌曲,你真的需要在每個人訪問你的網(wǎng)站的時候都去查一遍這10首歌嗎?如果你想按10分鐘或者是一小時的頻率來緩存查詢結(jié)果來加速你的應(yīng)用,Laravel 的 caching緩存模塊能將使工作變得異常簡單.
嗯,從這段話,我已經(jīng)了解到這完全符合我現(xiàn)在的需求了,接下來我只需要找到對應(yīng)的使用方法和API,一步一步來就行了.
二. 學(xué)習(xí)相應(yīng)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)作為緩存的驅(qū)動, 這是不需配置就可使用的, 文件系統(tǒng)驅(qū)動會將緩存的數(shù)據(jù)存入緩存目錄下的文件里面去, 如果你覺得合適的話不需要做任何其他的配置直接開始用就行了.
當然了, 這也是符合我的想法的, 其實我就是想把頁面緩存成靜態(tài)頁文件, 用戶再次訪問時直接輸出緩存的靜態(tài)頁就ok了, 如果需要更高級的需求, 還可以使用其他的驅(qū)動,有數(shù)據(jù)庫驅(qū)動, memcached, redis驅(qū)動等, 很好很強大!
2. 接下來查看用例,找到使用方法
用例文檔在這: http://laravel.com/docs/cache/usage
可以看出, 里面有 get, put, forever, remember, has, forget 等方法,這些方法使用也是基本上能 "望文生義" 就能搞定的,呵呵
具體使用方法文檔里面已經(jīng)說的夠詳細, 使用方法一目了然我就不細說了, 只在代碼里面說吧
三. 具體實現(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ā)表了博文,自己再去后臺立即刷新一下緩存就好了, 所以不需要設(shè)置過期啊失效時間之類的, 當然這個是要按各自的具體需求來的
3). Cache::get , 這句是從緩存里面取出 staticPageCache_home 這個名字的緩存, 然后作為響應(yīng)內(nèi)容返回
嗯, 就這么簡單, 呵呵, 一個基本的緩存功能就完成了, 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 '對不起,只有管理員組才可進行此操作!';
}
我給后臺添加了一個項目, 對應(yīng)這個方法, 方法內(nèi)容和首頁的大同小異, 取數(shù)據(jù), 然后Cache::forever 刷新一下緩存,就這么簡單,當然了,上面的Auth::user() 判斷是個簡單的判斷,只有管理員組才能進行刷新操作,呵呵
嗯, 全部內(nèi)容就這么多, 很簡單, 歡迎童鞋們拍磚指正!
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php日期與時間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Laravel框架的PHP程序設(shè)計有所幫助。
相關(guān)文章
解析CI即CodeIgniter框架在Nginx下的重寫規(guī)則
本篇文章是對CI即CodeIgniter框架在Nginx下的重寫規(guī)則進行了詳細的分析介紹,需要的朋友參考下2013-06-06
用php實現(xiàn)百度網(wǎng)盤圖片直鏈的代碼分享
華為網(wǎng)盤有個直鏈功能,不過需要錢買。我有百度網(wǎng)盤,不過百度的網(wǎng)盤外鏈不能在網(wǎng)頁里直接使用圖片 華為的直鏈功能可以做到。百度哪天也能有這功能就好了。2012-11-11
php 將json格式數(shù)據(jù)轉(zhuǎn)換成數(shù)組的方法
今天小編就為大家分享一篇php 將json格式數(shù)據(jù)轉(zhuǎn)換成數(shù)組的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
thinkPHP框架可添加js事件的分頁類customPage.class.php完整實例
這篇文章主要介紹了thinkPHP框架可添加js事件的分頁類customPage.class.php,以完整實例形式給出了分頁類customPage.class.php的實現(xiàn)代碼并分析了ajax動態(tài)加載數(shù)據(jù),設(shè)置分頁鏈接等功能,需要的朋友可以參考下2017-03-03

