本節(jié)我們將以文章的增刪改查作為實(shí)例系統(tǒng)講述緩存的使用,這個(gè)實(shí)例是對(duì)之前創(chuàng)建RESTFul風(fēng)格控制器實(shí)現(xiàn)文章增刪改查這篇教程的改造和升級(jí),我們將在其基礎(chǔ)上融合進(jìn)Eloquent ORM和模型事件,將應(yī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ù)庫部分使用查詢構(gòu)建器實(shí)現(xiàn)對(duì)數(shù)據(jù)庫的高級(jí)查詢已有提及,這里我們使用之前創(chuàng)建的數(shù)據(jù)表即可。
創(chuàng)建文章模型
關(guān)于文章模型Post的創(chuàng)建也和之前Eloquent ORM部分講ORM概述、模型定義及基本查詢中創(chuàng)建的一致。
2、修改控制器
在之前我們是通過緩存實(shí)現(xiàn)對(duì)文章的增刪改查操作,這里我們將其修改為通過數(shù)據(jù)庫實(shí)現(xiàn)增刪改查操作:
- 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ù)量大采用分頁獲取
- $posts = Post::all();
- if(!$posts)
- exit('還沒有發(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)建新文章表單頁面
- *
- * @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;
- }
- /**
- * 顯示編輯指定文章的表單頁面
- *
- * @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('指定被刪除文章不存在!');
- //Vevb.com
- if($post->delete()){
- redirect()->route('post.index');
- }else{
- exit('刪除文章失敗!');
- }
- }
- }
需要注意的是在show方法中,我們首先從緩存中取文章數(shù)據(jù),緩存中不存在才會(huì)去數(shù)據(jù)庫取,同時(shí)將數(shù)據(jù)回寫到緩存中,由于對(duì)數(shù)據(jù)庫的操作大部分都是讀操作,所以這一點(diǎn)小小的改進(jìn)對(duì)性能卻有很大提升,尤其是在海量數(shù)據(jù)時(shí)。此外我們還將訪問量持久化到緩存中以提升性能。
3、在模型事件中使用緩存
我們還可以通過模型事件在文章進(jìn)行增刪改的時(shí)候觸發(fā)相應(yīng)事件將修改保存到緩存中,這里我們簡單講模型事件注冊(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í)也不能瀏覽。
新聞熱點(diǎn)
疑難解答