Laravel使用memcached緩存對文章增刪改查進(jìn)行優(yōu)化的方法
本文實例講述了Laravel使用memcached緩存對文章增刪改查進(jìn)行優(yōu)化的方法。分享給大家供大家參考,具體如下:
這里我們將以文章的增刪改查作為實例系統(tǒng)講述緩存的使用,這個實例是對之前創(chuàng)建RESTFul風(fēng)格控制器實現(xiàn)文章增刪改查這篇教程的改造和升級,我們將在其基礎(chǔ)上融合進(jìn)Eloquent ORM和模型事件,將應(yīng)用的場景直接拉到生成環(huán)境。
1、準(zhǔn)備工作
路由及控制器
路由的定義和控制器的創(chuàng)建保持和創(chuàng)建RESTFul風(fēng)格控制器實現(xiàn)文章增刪改查中一樣。
創(chuàng)建數(shù)據(jù)表
關(guān)于文章對應(yīng)數(shù)據(jù)表我們在數(shù)據(jù)庫部分使用查詢構(gòu)建器實現(xiàn)對數(shù)據(jù)庫的高級查詢已有提及,這里我們使用之前創(chuàng)建的數(shù)據(jù)表即可。
創(chuàng)建文章模型
關(guān)于文章模型Post的創(chuàng)建也和之前Eloquent ORM部分講ORM概述、模型定義及基本查詢中創(chuàng)建的一致。
2、修改控制器
在之前我們是通過緩存實現(xiàn)對文章的增刪改查操作,這里我們將其修改為通過數(shù)據(jù)庫實現(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ù)量大采用分頁獲取
$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)建的文章存儲到存儲器
*
* @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;
}
/**
* 在存儲器中更新指定文章
*
* @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]);
}
/**
* 從存儲器中移除指定文章
*
* @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ù),緩存中不存在才會去數(shù)據(jù)庫取,同時將數(shù)據(jù)回寫到緩存中,由于對數(shù)據(jù)庫的操作大部分都是讀操作,所以這一點小小的改進(jìn)對性能卻有很大提升,尤其是在海量數(shù)據(jù)時。此外我們還將訪問量持久化到緩存中以提升性能。
3、在模型事件中使用緩存
我們還可以通過模型事件在文章進(jìn)行增刪改的時候觸發(fā)相應(yīng)事件將修改保存到緩存中,這里我們簡單講模型事件注冊到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ù)據(jù)保存到緩存,而刪除文章時也會從緩存中移除數(shù)據(jù),從而保證被刪除后的文章查看詳情時也不能瀏覽。
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《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)文章
PHP網(wǎng)頁游戲?qū)W習(xí)之Xnova(ogame)源碼解讀(十四)
這篇文章主要介紹了PHP網(wǎng)頁游戲Xnova(ogame)源碼解讀的資源更新頁面部分,需要的朋友可以參考下2014-06-06

