Redis在項目中的應用實例

在初步瞭解RedisLaravel中的應用 那麼咱們試想這樣的一個應用場景 一個文章或者帖子的瀏覽次數的統計 若是隻是每次增長一個瀏覽量php

就到數據庫新增一個數據 若是請求來那個太大這對數據庫的消耗也就不言而喻了吧 那咱們是否是能夠有其餘的解決方案css

這裏的解決方案就是 即便你的網站的請求量很大 那麼每次增長一個訪問量就在緩存中去進行更改 至於刷新Mysql數據庫能夠自定義爲html

多少分鐘進行刷新一次或者訪問量達到必定數量再去刷新數據庫 這樣數據也是準確的 效率也比直接每次刷新數據庫要高出許多了jquery

既然給出了相應的解決方案 咱們就開始實施laravel

咱們以一篇帖子的瀏覽爲例 咱們先去建立對應的控制器redis

$ php artisan make:controller PostController

再去生成須要用到的 Modelsql

$ php artisan make:model Post -m

填寫posts的遷移表的字段內容shell

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(App\Post::class, function (Faker\Generator $faker) {
    return [
        'title' => $faker->sentence,
        'content' => $faker->paragraph,
        'view_count' => 0
    ];
});

定義帖子的訪問路由bootstrap

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

固然咱們仍是須要去寫咱們訪問也就是瀏覽事件的(在app/providers/EventServiceProvider中定義)

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'));
}

這裏看的出來就是以Redis做爲緩存驅動 一樣的 會獲取獲取的ip目的是防止同一個ip屢次刷新來增長瀏覽量

一樣的每次瀏覽會觸發咱們以前定義的事件 傳入咱們的postid參數

Redis的key的命名以:分割 這樣能夠理解爲一個層級目錄 在可視化工具裏就能夠看的很明顯了

接下來就是給出咱們的posts.show的視圖文件

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

最後能夠經過咱們的工具查看具體效果
圖片描述

相關連接

相關文章
相關標籤/搜索