$ php artisan make:controller PostController
$ php artisan make:model Post -m
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string("title");
$table->string("content");
$table->integer('view_count')->unsigned();
$table->timestamps();
});
$factory->define(App\Post::class, function (Faker\Generator $faker) {
return [
'title' => $faker->sentence,
'content' => $faker->paragraph,
'view_count' => 0
];
});
Route::get('/post/{id}', 'PostController@showPost');
protected $listen = [ 'App\Events\PostViewEvent' => [ // 'App\Listeners\EventListener', 'App\Listeners\PostEventListener', ], ];
$ php artisan event:generate
public function showPost(Request $request,$id)
{
//Redis缓存中没有该post,则从数据库中取值,并存入Redis中,该键值key='post:cache'.$id生命时间5分钟
$post = Cache::remember('post:cache:'.$id, $this->cacheExpires, function () use ($id) {
return Post::whereId($id)->first();
});
//获取客户端请求的IP
$ip = $request->ip();
//触发浏览次数统计时间
event(new PostViewEvent($post, $ip));
return view('posts.show', compact('post'));
}
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap Template</title>
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="external nofollow" >
<style>
html,body{
width: 100%;
height: 100%;
}
*{
margin: 0;
border: 0;
}
.jumbotron{
margin-top: 10%;
}
.jumbotron>span{
margin: 10px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-xs-12 col-md-12">
<div class="jumbotron">
<h1>Title:{{$post->title}}</h1>
<span class="glyphicon glyphicon-eye-open" aria-hidden="true"> {{$post->view_count}} views</span>
<p>Content:{{$post->content}}</p>
</div>
</div>
</div>
</div>
<!-- jQuery文件-->
<script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script>
</script>
</body>
</html>
class PostViewEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $ip;
public $post;
/**
* PostViewEvent constructor.
* @param Post $post
* @param $ip
*/
public function __construct(Post $post, $ip)
{
$this->post = $post;
$this->ip = $ip;
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
class PostEventListener
{
/**
* 一个帖子的最大访问数
*/
const postViewLimit = 20;
/**
* 同一用户浏览同一个帖子的过期时间
*/
const ipExpireSec = 200;
/**
* Create the event listener.
*
*/
public function __construct()
{
}
/**
* @param PostViewEvent $event
*/
public function handle(PostViewEvent $event)
{
$post = $event->post;
$ip = $event->ip;
$id = $post->id;
//首先判断下ipExpireSec = 200秒时间内,同一IP访问多次,仅仅作为1次访问量
if($this->ipViewLimit($id, $ip)){
//一个IP在300秒时间内访问第一次时,刷新下该篇post的浏览量
$this->updateCacheViewCount($id, $ip);
}
}
/**
* 限制同一IP一段时间内得访问,防止增加无效浏览次数
* @param $id
* @param $ip
* @return bool
*/
public function ipViewLimit($id, $ip)
{
$ipPostViewKey = 'post:ip:limit:'.$id;
//Redis命令SISMEMBER检查集合类型Set中有没有该键,Set集合类型中值都是唯一
$existsInRedisSet = Redis::command('SISMEMBER', [$ipPostViewKey, $ip]);
//如果集合中不存在这个建 那么新建一个并设置过期时间
if(!$existsInRedisSet){
//SADD,集合类型指令,向ipPostViewKey键中加一个值ip
Redis::command('SADD', [$ipPostViewKey, $ip]);
//并给该键设置生命时间,这里设置300秒,300秒后同一IP访问就当做是新的浏览量了
Redis::command('EXPIRE', [$ipPostViewKey, self::ipExpireSec]);
return true;
}
return false;
}
/**
* 达到要求更新数据库的浏览量
* @param $id
* @param $count
*/
public function updateModelViewCount($id, $count)
{
//访问量达到300,再进行一次SQL更新
$post = Post::find($id);
$post->view_count += $count;
$post->save();
}
/**
* 不同用户访问,更新缓存中浏览次数
* @param $id
* @param $ip
*/
public function updateCacheViewCount($id, $ip)
{
$cacheKey = 'post:view:'.$id;
//这里以Redis哈希类型存储键,就和数组类似,$cacheKey就类似数组名 如果这个key存在
if(Redis::command('HEXISTS', [$cacheKey, $ip])){
//哈希类型指令HINCRBY,就是给$cacheKey[$ip]加上一个值,这里一次访问就是1
$save_count = Redis::command('HINCRBY', [$cacheKey, $ip, 1]);
//redis中这个存储浏览量的值达到30后,就去刷新一次数据库
if($save_count == self::postViewLimit){
$this->updateModelViewCount($id, $save_count);
//本篇post,redis中浏览量刷进MySQL后,就把该篇post的浏览量清空,重新开始计数
Redis::command('HDEL', [$cacheKey, $ip]);
Redis::command('DEL', ['laravel:post:cache:'.$id]);
}
}else{
//哈希类型指令HSET,和数组类似,就像$cacheKey[$ip] = 1;
Redis::command('HSET', [$cacheKey, $ip, '1']);
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有