本文實(shí)例講述了Laravel使用Caching緩存數(shù)據(jù)減輕數(shù)據(jù)庫查詢壓力的方法。分享給大家供大家參考,具體如下:
昨天想把自己博客的首頁做一下緩存,達(dá)到類似于生成靜態(tài)頁緩存的效果,在群里問了大家怎么做緩存,都挺忙的沒多少回復(fù),我就自己去看了看文檔,發(fā)現(xiàn)了Caching這個(gè)部分,其實(shí)之前也有印象,但是沒具體接觸過,顧名思義,就是緩存了,那肯定和我的需求有點(diǎn)聯(lián)系,我就認(rèn)真看了看,發(fā)現(xiàn)的確是太強(qiáng)大了,經(jīng)過很簡單的幾個(gè)步驟,我就改裝好了首頁,用firebug測(cè)試了一下,提高了幾十毫秒解析時(shí)間,當(dāng)然了有人會(huì)笑這有必要嗎,豈不是閑的蛋疼?其實(shí)我想這是有必要的,只是在我這里一來訪問人少(其實(shí)根本沒人還,嘿嘿....),二來我在首頁里做的查詢目前還挺少,就一次,就是取得所有博文,如果一個(gè)頁面里面有個(gè)七八次乃至十多次查詢,我想這個(gè)效果應(yīng)該就很明顯了吧!(當(dāng)然了,Raymond哥還有提到用更高級(jí)的專用緩存去做(memcached之類吧貌似),這是要自己能取得服務(wù)器控制權(quán),能自由安裝軟件或者服務(wù)器本來就有這些緩存機(jī)制的情況下才能實(shí)現(xiàn)的,我需求比較簡單,也沒有這個(gè)環(huán)境去做,所以這里就不考慮了)
閑話少說,開始吧,先說說我的具體需求:
一. 實(shí)現(xiàn)首頁的數(shù)據(jù)緩存,如果有沒過期的緩存,就不查數(shù)據(jù)庫,這樣基本模擬出靜態(tài)頁的效果(當(dāng)然了,其實(shí)還是要經(jīng)過php處理的)
二. 實(shí)現(xiàn)刷新指定緩存的功能(這里只有首頁,就單指刷新首頁緩存了,這個(gè)功能,我做到了admin模塊下
具體實(shí)現(xiàn):
一. 查閱文檔,找到能幫我實(shí)現(xiàn)需求的模塊
我查了一下文檔,發(fā)現(xiàn)了有Caching這樣一個(gè)模塊,顧名思義,就是緩存了,那它能否幫到我呢,看看先:
1. http://laravel.com/docs/cache/config 這里是laravel的Caching模塊的實(shí)現(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首流行歌曲,你真的需要在每個(gè)人訪問你的網(wǎng)站的時(shí)候都去查一遍這10首歌嗎?如果你想按10分鐘或者是一小時(shí)的頻率來緩存查詢結(jié)果來加速你的應(yīng)用,Laravel 的 caching緩存模塊能將使工作變得異常簡單.
嗯,從這段話,我已經(jīng)了解到這完全符合我現(xiàn)在的需求了,接下來我只需要找到對(duì)應(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.
我簡單理解為:
默認(rèn)情況下,Laravel使用文件系統(tǒng)作為緩存的驅(qū)動(dòng), 這是不需配置就可使用的, 文件系統(tǒng)驅(qū)動(dòng)會(huì)將緩存的數(shù)據(jù)存入緩存目錄下的文件里面去, 如果你覺得合適的話不需要做任何其他的配置直接開始用就行了.
當(dāng)然了, 這也是符合我的想法的, 其實(shí)我就是想把頁面緩存成靜態(tài)頁文件, 用戶再次訪問時(shí)直接輸出緩存的靜態(tài)頁就ok了, 如果需要更高級(jí)的需求, 還可以使用其他的驅(qū)動(dòng),有數(shù)據(jù)庫驅(qū)動(dòng), memcached, redis驅(qū)動(dòng)等, 很好很強(qiáng)大!
2. 接下來查看用例,找到使用方法
用例文檔在這: http://laravel.com/docs/cache/usage
可以看出, 里面有 get, put, forever, remember, has, forget 等方法,這些方法使用也是基本上能 "望文生義" 就能搞定的,呵呵
具體使用方法文檔里面已經(jīng)說的夠詳細(xì), 使用方法一目了然我就不細(xì)說了, 只在代碼里面說吧
三. 具體實(shí)現(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,作用只有一個(gè), 就是從博文表里面取得所有博文, 然后輸出, 每次有人訪問, 都要查表, 如果沒有發(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'); }} 這里我用到了三個(gè)api
1). Cache::has ,這個(gè)判斷是說如果當(dāng)前不存在 staticPageCache_home 這個(gè)名字的緩存, 就立即去取數(shù)據(jù)
2). Cache::forever, 這個(gè)從用例文檔里面可知是"永久緩存"的意思, 因?yàn)槲乙话愣际呛芮趧诘?如果發(fā)表了博文,自己再去后臺(tái)立即刷新一下緩存就好了, 所以不需要設(shè)置過期啊失效時(shí)間之類的, 當(dāng)然這個(gè)是要按各自的具體需求來的
3). Cache::get , 這句是從緩存里面取出 staticPageCache_home 這個(gè)名字的緩存, 然后作為響應(yīng)內(nèi)容返回
嗯, 就這么簡單, 呵呵, 一個(gè)基本的緩存功能就完成了, laravel的確是不錯(cuò)地!
3. 為后臺(tái)添加刷新緩存功能
還是貼代碼吧, 不過也很簡單:
// 刷新首頁緩存(暫時(shí)只支持首頁)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 '對(duì)不起,只有管理員組才可進(jìn)行此操作!';} 我給后臺(tái)添加了一個(gè)項(xiàng)目, 對(duì)應(yīng)這個(gè)方法, 方法內(nèi)容和首頁的大同小異, 取數(shù)據(jù), 然后Cache::forever 刷新一下緩存,就這么簡單,當(dāng)然了,上面的Auth::user() 判斷是個(gè)簡單的判斷,只有管理員組才能進(jìn)行刷新操作,呵呵
嗯, 全部內(nèi)容就這么多, 很簡單, 歡迎童鞋們拍磚指正!

















