初始化我们的事件就是接收一下这些参数即可
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']);
}
}
}
最后可以通过我们的工具查看具体效果

总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
相关链接
Redis 命令
Laravel中unique和exists验证规则的优化详解这篇文章主要给大家介绍了关于Laravel中unique和exists验证规则的优化的相关资料,文中通过示例代码介绍的非常详细,
Laravel中使用Queue的最基本操作教程Laravel队列服务为各种不同的后台队列提供统一的API,下面这篇文章主要给大家介绍了关于Laravel中使用Queue的最基本操
Laravel中数据迁移与数据填充的详细步骤这篇文章主要给大家介绍了关于Laravel中数据迁移与数据填充的相关资料,文中通过示例代码给大家详细介绍了实现的
Laravel中服务提供者和门面模式的入门介绍这篇文章主要给大家介绍了关于Laravel中服务提供者和门面模式使用的相关资料,文中通过详细的示例代码给大家介绍
Laravel中encrypt和decrypt的实现方法这篇文章主要给大家介绍了关于Laravel中encrypt和decrypt的实现方法,文中通过示例代码介绍的非常详细,对大家的学习
Laravel中获取路由参数Route Parameters的五种方法示例这篇文章主要给大家介绍了关于Laravel中获取路由参数Route Parameters的五种方法,文中通过示例代码介绍的非常详细,对