国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

Laravel memcached緩存對(duì)文章增刪改查進(jìn)行優(yōu)化例子

2024-05-04 21:50:39
字體:
供稿:網(wǎng)友

本節(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)增刪改查操作:

  1.     namespace App/Http/Controllers; 
  2.  
  3.     use Illuminate/Http/Request; 
  4.  
  5.     use Cache; 
  6.     use App/Models/Post; 
  7.  
  8.     use App/Http/Requests; 
  9.     use App/Http/Controllers/Controller; 
  10.  
  11.     class PostController extends Controller 
  12.     { 
  13.         /** 
  14.          * 顯示文章列表. 
  15.          * 
  16.          * @return Response 
  17.          */ 
  18.         public function index() 
  19.         { 
  20.             //使用all獲取所有數(shù)據(jù),如果數(shù)據(jù)量大采用分頁獲取 
  21.             $posts = Post::all(); 
  22.             if(!$posts
  23.                 exit('還沒有發(fā)布任何文章!'); 
  24.  
  25.             $html = '<ul>'
  26.  
  27.             foreach ($posts as $post) { 
  28.                 $html .= '<li><a href='.route('post.show',['post'=>$post]).'>'.$post->title.'</li>'
  29.             } 
  30.  
  31.             $html .= '</ul>'
  32.  
  33.             return $html
  34.         } 
  35.  
  36.         /** 
  37.          * 創(chuàng)建新文章表單頁面 
  38.          * 
  39.          * @return Response 
  40.          */ 
  41.         public function create() 
  42.         { 
  43.             $postUrl = route('post.store'); 
  44.             $csrf_field = csrf_field(); 
  45.             $html = <<<CREATE 
  46.                 <form action="$postUrl" method="POST"
  47.                     $csrf_field 
  48.                     <input type="text" name="title"><br/><br/> 
  49.                     <textarea name="content" cols="50" rows="5"></textarea><br/><br/> 
  50.                     <input type="submit" value="提交"/> 
  51.                 </form> 
  52. CREATE; 
  53.             return $html
  54.  
  55.         /** 
  56.          * 將新創(chuàng)建的文章存儲(chǔ)到存儲(chǔ)器 
  57.          * 
  58.          * @param Request $request 
  59.          * @return Response 
  60.          */ 
  61.         public function store(Request $request
  62.         { 
  63.             $title = $request->input('title'); 
  64.             $content = $request->input('content'); 
  65.  
  66.             $post = new Post; 
  67.             $post->title = $title
  68.             $post->content = $content
  69.             $post->save(); 
  70.  
  71.             return redirect()->route('post.show',['post'=>$post]); 
  72.         } 
  73.  
  74.         /** 
  75.          * 顯示指定文章 
  76.          * 
  77.          * @param int $id 
  78.          * @return Response 
  79.          */ 
  80.         public function show($id
  81.         { 
  82.  
  83.             $post = Cache::get('post_'.$id); 
  84.             if(!$post){ 
  85.                 $post = Post::find($id); 
  86.                 if(!$post
  87.                     exit('指定文章不存在!'); 
  88.                 Cache::put('post_'.$id,$post,60*24*7); 
  89.             } 
  90.  
  91.             if(!Cache::get('post_views_'.$id)) 
  92.                 Cache::forever('post_views_'.$id,0); 
  93.             $views = Cache::increment('post_views_'.$id); 
  94.             Cache::forever('post_views_'.$id,$views); 
  95.  
  96.             $editUrl = route('post.edit',['post'=>$post]); 
  97.             $deleteUrl = route('post.destroy',['post'=>$post]); 
  98.             $html = <<<POST 
  99.                 <h3>{$post->title}</h3> 
  100.                 <p>{$post->content}</p> 
  101.                 <i>已有{$views}人閱讀</i> 
  102.                 <p> 
  103.                     <a href="{$editUrl}">編輯</a> 
  104.                 </p> 
  105. POST; 
  106.  
  107.             return $html
  108.         } 
  109.  
  110.         /** 
  111.          * 顯示編輯指定文章的表單頁面 
  112.          * 
  113.          * @param int $id 
  114.          * @return Response 
  115.          */ 
  116.         public function edit($id
  117.         { 
  118.             $post = Post::find($id); 
  119.  
  120.             if(!$post
  121.                 exit('指定文章不存在!'); 
  122.  
  123.             $postUrl = route('post.update',['post'=>$post]); 
  124.             $csrf_field = csrf_field(); 
  125.             $html = <<<CREATE 
  126.                 <form action="$postUrl" method="POST"
  127.                     $csrf_field 
  128.                     <input type="hidden" name="_method" value="PUT"/> 
  129.                     <input type="text" name="title" value="{$post->title}"><br/><br/> 
  130.                     <textarea name="content" cols="50" rows="5">{$post->content}</textarea><br/><br/> 
  131.                     <input type="submit" value="提交"/> 
  132.                 </form> 
  133. CREATE; 
  134.             return $html
  135.  
  136.         } 
  137.  
  138.         /** 
  139.          * 在存儲(chǔ)器中更新指定文章 
  140.          * 
  141.          * @param Request $request 
  142.          * @param int $id 
  143.          * @return Response 
  144.          */ 
  145.         public function update(Request $request$id
  146.         { 
  147.             $post = Post::find($id); 
  148.             if(!$post
  149.                 exit('指定文章不存在!'); 
  150.  
  151.             $title = $request->input('title'); 
  152.             $content = $request->input('content'); 
  153.  
  154.             $post->title = $title
  155.             $post->content = $content
  156.  
  157.             $post->save(); 
  158.  
  159.             return redirect()->route('post.show',['post'=>$post]); 
  160.         } 
  161.  
  162.         /** 
  163.          * 從存儲(chǔ)器中移除指定文章 
  164.          * 
  165.          * @param int $id 
  166.          * @return Response 
  167.          */ 
  168.         public function destroy($id
  169.         { 
  170.             $post = Post::find($id); 
  171.             if(!$post
  172.                 exit('指定被刪除文章不存在!'); 
  173. //Vevb.com 
  174.             if($post->delete()){ 
  175.                 redirect()->route('post.index'); 
  176.             }else
  177.                 exit('刪除文章失敗!'); 
  178.             } 
  179.         } 
  180.     } 

需要注意的是在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方法中:

  1. //保存之后更新緩存數(shù)據(jù) 
  2. Post::saved(function($post){ 
  3.     $cacheKey = 'post_'.$post->id; 
  4.     $cacheData = Cache::get($cacheKey); 
  5.     if(!$cacheData){ 
  6.         Cache::add($cacheKey,$post,60*24*7); 
  7.     }else
  8.         Cache::put($cacheKey,$post,60*24*7); 
  9.     } 
  10. }); 
  11.  
  12. //刪除之后清除緩存數(shù)據(jù) 
  13. Post::deleted(function($post){ 
  14.     $cacheKey = 'post_'.$post->id; 
  15.     $cacheData = Cache::get($cacheKey); 
  16.     if($cacheData){ 
  17.         Cache::forget($cacheKey); 
  18.     } 
  19.     if(Cache::get('post_views_'.$post->id)) 
  20.         Cache::forget('post_views_'.$post->id); 
  21. }); 

我們將緩存有效期設(shè)置為一周。這樣在文章創(chuàng)建或更新時(shí)會(huì)將數(shù)據(jù)保存到緩存,而刪除文章時(shí)也會(huì)從緩存中移除數(shù)據(jù),從而保證被刪除后的文章查看詳情時(shí)也不能瀏覽。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 东乡族自治县| 依安县| 郁南县| 彰化市| 兴山县| 平阳县| 双辽市| 乡城县| 南涧| 宁明县| 措美县| 上杭县| 尉犁县| 蓝山县| 班玛县| 库伦旗| 宜兴市| 敖汉旗| 南郑县| 策勒县| 泊头市| 河池市| 张家界市| 元阳县| 丹江口市| 南澳县| 崇义县| 沧州市| 南平市| 花莲县| 芜湖县| 壤塘县| 余姚市| 眉山市| 陇西县| 彭阳县| 全州县| 磐安县| 新和县| 西丰县| 来凤县|