栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

Laravel使用memcached缓存对文章增删改查进行优化的方法

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Laravel使用memcached缓存对文章增删改查进行优化的方法

本文实例讲述了Laravel使用memcached缓存对文章增删改查进行优化的方法。分享给大家供大家参考,具体如下:

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

1、准备工作

路由及控制器

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

创建数据表

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

创建文章模型

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

2、修改控制器

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

';
      foreach ($posts as $post) {
 $html .= '
  • $post]).'>'.$post->title.'
  • '; } $html .= ''; return $html; } public function create() { $postUrl = route('post.store'); $csrf_field = csrf_field(); $html = <<input('title'); $content = $request->input('content'); $post = new Post; $post->title = $title; $post->content = $content; $post->save(); return redirect()->route('post.show',['post'=>$post]); } 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 = <<title}

    {$post->content}

    已有{$views}人阅读

    编辑

    POST; return $html; } public function edit($id) { $post = Post::find($id); if(!$post) exit('指定文章不存在!'); $postUrl = route('post.update',['post'=>$post]); $csrf_field = csrf_field(); $html = <<input('title'); $content = $request->input('content'); $post->title = $title; $post->content = $content; $post->save(); return redirect()->route('post.show',['post'=>$post]); } public function destroy($id) { $post = Post::find($id); if(!$post) exit('指定被删除文章不存在!'); if($post->delete()){ redirect()->route('post.index'); }else{ exit('删除文章失败!'); } } }

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

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

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

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

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

    更多关于Laravel相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

    希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

    转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/42405.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号