Laravel使用memcached緩存對(duì)文章增刪改查進(jìn)行優(yōu)化的方法
本文實(shí)例講述了Laravel使用memcached緩存對(duì)文章增刪改查進(jìn)行優(yōu)化的方法。分享給大家供大家參考,具體如下:
這里我們將以文章的增刪改查作為實(shí)例系統(tǒng)講述緩存的使用,這個(gè)實(shí)例是對(duì)之前創(chuàng)建RESTFul風(fēng)格控制器實(shí)現(xiàn)文章增刪改查這篇教程的改造和升級(jí),我們將在其基礎(chǔ)上融合進(jìn)Eloquent ORM和模型事件,將應(yīng)用的場(chǎng)景直接拉到生成環(huán)境。
1、準(zhǔn)備工作
路由及控制器
路由的定義和控制器的創(chuàng)建保持和創(chuàng)建RESTFul風(fēng)格控制器實(shí)現(xiàn)文章增刪改查中一樣。
創(chuàng)建數(shù)據(jù)表
關(guān)于文章對(duì)應(yīng)數(shù)據(jù)表我們?cè)跀?shù)據(jù)庫(kù)部分使用查詢(xún)構(gòu)建器實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的高級(jí)查詢(xún)已有提及,這里我們使用之前創(chuàng)建的數(shù)據(jù)表即可。
創(chuàng)建文章模型
關(guān)于文章模型Post的創(chuàng)建也和之前Eloquent ORM部分講ORM概述、模型定義及基本查詢(xún)中創(chuàng)建的一致。
2、修改控制器
在之前我們是通過(guò)緩存實(shí)現(xiàn)對(duì)文章的增刪改查操作,這里我們將其修改為通過(guò)數(shù)據(jù)庫(kù)實(shí)現(xiàn)增刪改查操作:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Cache; use App\Models\Post; use App\Http\Requests; use App\Http\Controllers\Controller; class PostController extends Controller { /** * 顯示文章列表. * * @return Response */ public function index() { //使用all獲取所有數(shù)據(jù),如果數(shù)據(jù)量大采用分頁(yè)獲取 $posts = Post::all(); if(!$posts) exit('還沒(méi)有發(fā)布任何文章!'); $html = '<ul>'; foreach ($posts as $post) { $html .= '<li><a href='.route('post.show',['post'=>$post]).'>'.$post->title.'</li>'; } $html .= '</ul>'; return $html; } /** * 創(chuàng)建新文章表單頁(yè)面 * * @return Response */ public function create() { $postUrl = route('post.store'); $csrf_field = csrf_field(); $html = <<<CREATE <form action="$postUrl" method="POST"> $csrf_field <input type="text" name="title"><br/><br/> <textarea name="content" cols="50" rows="5"></textarea><br/><br/> <input type="submit" value="提交"/> </form> CREATE; return $html; } /** * 將新創(chuàng)建的文章存儲(chǔ)到存儲(chǔ)器 * * @param Request $request * @return Response */ public function store(Request $request) { $title = $request->input('title'); $content = $request->input('content'); $post = new Post; $post->title = $title; $post->content = $content; $post->save(); return redirect()->route('post.show',['post'=>$post]); } /** * 顯示指定文章 * * @param int $id * @return Response */ public function show($id) { $post = Cache::get('post_'.$id); if(!$post){ $post = Post::find($id); if(!$post) exit('指定文章不存在!'); Cache::put('post_'.$id,$post,60*24*7); } if(!Cache::get('post_views_'.$id)) Cache::forever('post_views_'.$id,0); $views = Cache::increment('post_views_'.$id); Cache::forever('post_views_'.$id,$views); $editUrl = route('post.edit',['post'=>$post]); $deleteUrl = route('post.destroy',['post'=>$post]); $html = <<<POST <h3>{$post->title}</h3> <p>{$post->content}</p> <i>已有{$views}人閱讀</i> <p> <a href="{$editUrl}">編輯</a> </p> POST; return $html; } /** * 顯示編輯指定文章的表單頁(yè)面 * * @param int $id * @return Response */ public function edit($id) { $post = Post::find($id); if(!$post) exit('指定文章不存在!'); $postUrl = route('post.update',['post'=>$post]); $csrf_field = csrf_field(); $html = <<<CREATE <form action="$postUrl" method="POST"> $csrf_field <input type="hidden" name="_method" value="PUT"/> <input type="text" name="title" value="{$post->title}"><br/><br/> <textarea name="content" cols="50" rows="5">{$post->content}</textarea><br/><br/> <input type="submit" value="提交"/> </form> CREATE; return $html; } /** * 在存儲(chǔ)器中更新指定文章 * * @param Request $request * @param int $id * @return Response */ public function update(Request $request, $id) { $post = Post::find($id); if(!$post) exit('指定文章不存在!'); $title = $request->input('title'); $content = $request->input('content'); $post->title = $title; $post->content = $content; $post->save(); return redirect()->route('post.show',['post'=>$post]); } /** * 從存儲(chǔ)器中移除指定文章 * * @param int $id * @return Response */ public function destroy($id) { $post = Post::find($id); if(!$post) exit('指定被刪除文章不存在!'); if($post->delete()){ redirect()->route('post.index'); }else{ exit('刪除文章失敗!'); } } }
需要注意的是在show方法中,我們首先從緩存中取文章數(shù)據(jù),緩存中不存在才會(huì)去數(shù)據(jù)庫(kù)取,同時(shí)將數(shù)據(jù)回寫(xiě)到緩存中,由于對(duì)數(shù)據(jù)庫(kù)的操作大部分都是讀操作,所以這一點(diǎn)小小的改進(jìn)對(duì)性能卻有很大提升,尤其是在海量數(shù)據(jù)時(shí)。此外我們還將訪(fǎng)問(wèn)量持久化到緩存中以提升性能。
3、在模型事件中使用緩存
我們還可以通過(guò)模型事件在文章進(jìn)行增刪改的時(shí)候觸發(fā)相應(yīng)事件將修改保存到緩存中,這里我們簡(jiǎn)單講模型事件注冊(cè)到AppServiceProvider的boot方法中:
//保存之后更新緩存數(shù)據(jù) Post::saved(function($post){ $cacheKey = 'post_'.$post->id; $cacheData = Cache::get($cacheKey); if(!$cacheData){ Cache::add($cacheKey,$post,60*24*7); }else{ Cache::put($cacheKey,$post,60*24*7); } }); //刪除之后清除緩存數(shù)據(jù) Post::deleted(function($post){ $cacheKey = 'post_'.$post->id; $cacheData = Cache::get($cacheKey); if($cacheData){ Cache::forget($cacheKey); } if(Cache::get('post_views_'.$post->id)) Cache::forget('post_views_'.$post->id); });
我們將緩存有效期設(shè)置為一周。這樣在文章創(chuàng)建或更新時(shí)會(huì)將數(shù)據(jù)保存到緩存,而刪除文章時(shí)也會(huì)從緩存中移除數(shù)據(jù),從而保證被刪除后的文章查看詳情時(shí)也不能瀏覽。
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Laravel框架入門(mén)與進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《smarty模板入門(mén)基礎(chǔ)教程》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
- Laravel使用Caching緩存數(shù)據(jù)減輕數(shù)據(jù)庫(kù)查詢(xún)壓力的方法
- Laravel中擴(kuò)展Memcached緩存驅(qū)動(dòng)實(shí)現(xiàn)使用阿里云OCS緩存
- laravel使用Redis實(shí)現(xiàn)網(wǎng)站緩存讀取的方法詳解
- Laravel ORM對(duì)Model::find方法進(jìn)行緩存示例詳解
- Laravel Memcached緩存驅(qū)動(dòng)的配置與應(yīng)用方法分析
- Laravel框架中實(shí)現(xiàn)使用阿里云ACE緩存服務(wù)
- Laravel框架中緩存的使用方法分析
相關(guān)文章
PHP框架Laravel學(xué)習(xí)心得體會(huì)
Laravel是一套簡(jiǎn)潔、優(yōu)雅的PHP Web開(kāi)發(fā)框架 (PHP Web Framework) 。在世界(不含中國(guó))PHP框架的占有率超過(guò)40%。下面通過(guò)本文給大家分享我的PHP框架Laravel學(xué)習(xí)心得體會(huì),歡迎大家給我留言2015-10-10Laravel中服務(wù)提供者和門(mén)面模式的入門(mén)介紹
這篇文章主要給大家介紹了關(guān)于Laravel中服務(wù)提供者和門(mén)面模式使用的相關(guān)資料,文中通過(guò)詳細(xì)的示例代碼給大家介紹了Laravel中的服務(wù)提供者和門(mén)面模式,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11php生成N個(gè)不重復(fù)的隨機(jī)數(shù)實(shí)例
使用php生成N個(gè)不重復(fù)的隨機(jī)數(shù)的實(shí)例方法2013-11-11PHP網(wǎng)頁(yè)游戲?qū)W習(xí)之Xnova(ogame)源碼解讀(十四)
這篇文章主要介紹了PHP網(wǎng)頁(yè)游戲Xnova(ogame)源碼解讀的資源更新頁(yè)面部分,需要的朋友可以參考下2014-06-06