本文实例讲述了laravel使用memcached缓存对文章增删改查进行优化的方法。分享给大家供大家参考,具体如下:
这里我们将以文章的增删改查作为实例系统讲述缓存的使用,这个实例是对之前创建restful风格控制器实现文章增删改查这篇教程的改造和升级,我们将在其基础上融合进eloquent orm和模型事件,将应用的场景直接拉到生成环境。
1、准备工作
路由及控制器
路由的定义和控制器的创建保持和创建restful风格控制器实现文章增删改查中一样。
创建数据表
关于文章对应数据表我们在数据库部分使用查询构建器实现对数据库的高级查询已有提及,这里我们使用之前创建的数据表即可。
创建文章模型
关于文章模型post的创建也和之前eloquent orm部分讲orm概述、模型定义及基本查询中创建的一致。
2、修改控制器
在之前我们是通过缓存实现对文章的增删改查操作,这里我们将其修改为通过数据库实现增删改查操作:
<?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获取所有数据,如果数据量大采用分页获取
$posts = post::all();
if(!$posts)
exit('还没有发布任何文章!');
$html = '<ul>';
foreach ($posts as $post) {
$html .= '<li><a href='.route('post.show',['post'=>$post]).'>'.$post->title.'</li>';
}
$html .= '</ul>';
return $html;
}
/**
* 创建新文章表单页面
*
* @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;
}
/**
* 将新创建的文章存储到存储器
*
* @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方法中,我们首先从缓存中取文章数据,缓存中不存在才会去数据库取,同时将数据回写到缓存中,由于对数据库的操作大部分都是读操作,所以这一点小小的改进对性能却有很大提升,尤其是在海量数据时。此外我们还将访问量持久化到缓存中以提升性能。
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框架的php程序设计有所帮助。
更多laravel使用memcached缓存对文章增删改查进行优化的方法。