当前位置:首页 > PHP教程 > php高级应用 > 列表

Laravel memcached缓存对文章增删改查进行优化例子

发布:smiling 来源: PHP粉丝网  添加日期:2018-10-30 10:28:30 浏览: 评论:0 

本节我们将以文章的增删改查作为实例系统讲述缓存的使用,这个实例是对之前创建RESTFul风格控制器实现文章增删改查这篇教程的改造和升级,我们将在其基础上融合进Eloquent ORM和模型事件,将应用的场景直接拉到生成环境。

1、准备工作

路由及控制器

路由的定义和控制器的创建保持和创建RESTFul风格控制器实现文章增删改查中一样。

创建数据表

关于文章对应数据表我们在数据库部分使用查询构建器实现对数据库的高级查询已有提及,这里我们使用之前创建的数据表即可。

创建文章模型

关于文章模型Post的创建也和之前Eloquent ORM部分讲ORM概述、模型定义及基本查询中创建的一致。

2、修改控制器

在之前我们是通过缓存实现对文章的增删改查操作,这里我们将其修改为通过数据库实现增删改查操作:

  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获取所有数据,如果数据量大采用分页获取 
  21.             $posts = Post::all(); 
  22.             if(!$posts) 
  23.                 exit('还没有发布任何文章!'); 
  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.          * 创建新文章表单页面 
  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.         /** 
  57.          * 将新创建的文章存储到存储器 
  58.          * 
  59.          * @param Request $request 
  60.          * @return Response 
  61.          */ 
  62.         public function store(Request $request) 
  63.         { 
  64.             $title = $request->input('title'); 
  65.             $content = $request->input('content'); 
  66.  
  67.             $post = new Post; 
  68.             $post->title = $title; 
  69.             $post->content = $content; 
  70.             $post->save(); 
  71.  
  72.             return redirect()->route('post.show',['post'=>$post]); 
  73.         } 
  74.  
  75.         /** 
  76.          * 显示指定文章 
  77.          * 
  78.          * @param int $id 
  79.          * @return Response 
  80.          */ 
  81.         public function show($id) 
  82.         { 
  83.  
  84.             $post = Cache::get('post_'.$id); 
  85.             if(!$post){ 
  86.                 $post = Post::find($id); 
  87.                 if(!$post) 
  88.                     exit('指定文章不存在!'); 
  89.                 Cache::put('post_'.$id,$post,60*24*7); 
  90.             } 
  91.  
  92.             if(!Cache::get('post_views_'.$id)) 
  93.                 Cache::forever('post_views_'.$id,0); 
  94.             $views = Cache::increment('post_views_'.$id); 
  95.             Cache::forever('post_views_'.$id,$views); 
  96.  
  97.             $editUrl = route('post.edit',['post'=>$post]); 
  98.             $deleteUrl = route('post.destroy',['post'=>$post]); 
  99.             $html = <<<POST 
  100.                 <h3>{$post->title}</h3> 
  101.                 <p>{$post->content}</p> 
  102.                 <i>已有{$views}人阅读</i> 
  103.                 <p> 
  104.                     <a href="{$editUrl}">编辑</a> 
  105.                 </p> 
  106. POST; 
  107.  
  108.             return $html; 
  109.         } 
  110.  
  111.         /** 
  112.          * 显示编辑指定文章的表单页面 
  113.          * 
  114.          * @param int $id 
  115.          * @return Response 
  116.          */ 
  117.         public function edit($id) 
  118.         { 
  119.             $post = Post::find($id); 
  120.  
  121.             if(!$post) 
  122.                 exit('指定文章不存在!'); 
  123.  
  124.             $postUrl = route('post.update',['post'=>$post]); 
  125.             $csrf_field = csrf_field(); 
  126.             $html = <<<CREATE 
  127.                 <form action="$postUrl" method="POST"> 
  128.                     $csrf_field 
  129.                     <input type="hidden" name="_method" value="PUT"/> 
  130.                     <input type="text" name="title" value="{$post->title}"><br/><br/> 
  131.                     <textarea name="content" cols="50" rows="5">{$post->content}</textarea><br/><br/> 
  132.                     <input type="submit" value="提交"/> 
  133.                 </form> 
  134. CREATE; 
  135.             return $html; 
  136.  
  137.         } 
  138.  
  139.         /** 
  140.          * 在存储器中更新指定文章 
  141.          * 
  142.          * @param Request $request 
  143.          * @param int $id 
  144.          * @return Response 
  145.          */ 
  146.         public function update(Request $request, $id) 
  147.         { 
  148.             $post = Post::find($id); 
  149.             if(!$post) 
  150.                 exit('指定文章不存在!'); 
  151.  
  152.             $title = $request->input('title'); 
  153.             $content = $request->input('content'); 
  154.  
  155.             $post->title = $title; 
  156.             $post->content = $content; 
  157.  
  158.             $post->save(); 
  159.  
  160.             return redirect()->route('post.show',['post'=>$post]); 
  161.         } 
  162.  
  163.         /** 
  164.          * 从存储器中移除指定文章 
  165.          * 
  166.          * @param int $id 
  167.          * @return Response 
  168.          */ 
  169.         public function destroy($id) 
  170.         { 
  171.             $post = Post::find($id); 
  172.             if(!$post) 
  173.                 exit('指定被删除文章不存在!'); 
  174. //phpfensi.com 
  175.             if($post->delete()){ 
  176.                 redirect()->route('post.index'); 
  177.             }else{ 
  178.                 exit('删除文章失败!'); 
  179.             } 
  180.         } 
  181.     } 

需要注意的是在show方法中,我们首先从缓存中取文章数据,缓存中不存在才会去数据库取,同时将数据回写到缓存中,由于对数据库的操作大部分都是读操作,所以这一点小小的改进对性能却有很大提升,尤其是在海量数据时。此外我们还将访问量持久化到缓存中以提升性能。

3、在模型事件中使用缓存

我们还可以通过模型事件在文章进行增删改的时候触发相应事件将修改保存到缓存中,这里我们简单讲模型事件注册到AppServiceProvider的boot方法中:

  1. //保存之后更新缓存数据 
  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. //删除之后清除缓存数据 
  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. }); 

我们将缓存有效期设置为一周。这样在文章创建或更新时会将数据保存到缓存,而删除文章时也会从缓存中移除数据,从而保证被删除后的文章查看详情时也不能浏览。

Tags: Laravel memcached

分享到: