资讯专栏INFORMATION COLUMN

Redis在项目中的应用实例

libin19890520 / 3180人阅读

摘要:在初步了解在中的应用那么我们试想这样的一个应用场景一个文章或者帖子的浏览次数的统计如果只是每次增加一个浏览量就到数据库新增一个数据如果请求来那个太大这对数据库的消耗也就不言而喻了吧那我们是不是可以有其他的解决方案这里的解决方案就是即使你的网

在初步了解RedisLaravel中的应用 那么我们试想这样的一个应用场景 一个文章或者帖子的浏览次数的统计 如果只是每次增加一个浏览量

就到数据库新增一个数据 如果请求来那个太大这对数据库的消耗也就不言而喻了吧 那我们是不是可以有其他的解决方案

这里的解决方案就是 即使你的网站的请求量很大 那么每次增加一个访问量就在缓存中去进行更改 至于刷新Mysql数据库可以自定义为

多少分钟进行刷新一次或者访问量达到一定数量再去刷新数据库 这样数据也是准确的 效率也比直接每次刷新数据库要高出许多了

既然给出了相应的解决方案 我们就开始实施

我们以一篇帖子的浏览为例 我们先去创建对应的控制器

$ php artisan make:controller PostController

再去生成需要用到的 Model

$ php artisan make:model Post -m

填写posts的迁移表的字段内容

Schema::create("posts", function (Blueprint $table) {
    $table->increments("id");
    $table->string("title");
    $table->string("content");
    $table->integer("view_count")->unsigned();
    $table->timestamps();
});

还有就是我们测试的数据的Seeder填充数据

$factory->define(AppPost::class, function (FakerGenerator $faker) {
    return [
        "title" => $faker->sentence,
        "content" => $faker->paragraph,
        "view_count" => 0
    ];
});

定义帖子的访问路由

Route::get("/post/{id}", "PostController@showPost");

当然我们还是需要去写我们访问也就是浏览事件的(在app/providers/EventServiceProvider中定义)

protected $listen = [
        "AppEventsPostViewEvent" => [
//            "AppListenersEventListener",
            "AppListenersPostEventListener",
        ],
    ];

执行事件生成监听

$ 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"));
}

这里看的出来就是以Redis作为缓存驱动 同样的 会获取获取的ip目的是防止同一个ip多次刷新来增加浏览量

同样的每次浏览会触发我们之前定义的事件 传入我们的postid参数

Redis的key的命名以:分割 这样可以理解为一个层级目录 在可视化工具里就可以看的很明显了

接下来就是给出我们的posts.show的视图文件



    
    
    
    Bootstrap Template
    
    
    


Title:{{$post->title}}

Content:{{$post->content}}

初始化我们的事件就是接收一下这些参数即可

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 命令

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/25604.html

相关文章

  • Redis的KEYS命令引起宕机事件

    摘要:最近的互联网线上事故发生比较频繁,年月号顺丰发生了一起线上删库事件,在这里就不介绍了。最后的最后,线上操作的任何一条命令,再小心也不为过,因为由于你的一个符号而引起的事故可能是你所承担不起的。 摘要: 使用 Redis 的开发者必看,吸取教训啊! 原文:Redis 的 KEYS 命令引起 RDS 数据库雪崩,RDS 发生两次宕机,造成几百万的资金损失 作者:陈浩翔 Fundebu...

    zoomdong 评论0 收藏0
  • Redis的KEYS命令引起宕机事件

    摘要:最近的互联网线上事故发生比较频繁,年月号顺丰发生了一起线上删库事件,在这里就不介绍了。最后的最后,线上操作的任何一条命令,再小心也不为过,因为由于你的一个符号而引起的事故可能是你所承担不起的。 摘要: 使用 Redis 的开发者必看,吸取教训啊! 原文:Redis 的 KEYS 命令引起 RDS 数据库雪崩,RDS 发生两次宕机,造成几百万的资金损失 作者:陈浩翔 Fundebu...

    ixlei 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<