Redis在Laravel項目中的應(yīng)用實例詳解
前言
本文主要給大家介紹了關(guān)于Redis在Laravel項目中的應(yīng)用實例,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹:
在初步了解Redis在Laravel中的應(yīng)用 那么我們試想這樣的一個應(yīng)用場景 一個文章或者帖子的瀏覽次數(shù)的統(tǒng)計 如果只是每次增加一個瀏覽量
就到數(shù)據(jù)庫新增一個數(shù)據(jù) 如果請求來那個太大這對數(shù)據(jù)庫的消耗也就不言而喻了吧 那我們是不是可以有其他的解決方案
這里的解決方案就是 即使你的網(wǎng)站的請求量很大 那么每次增加一個訪問量就在緩存中去進(jìn)行更改 至于刷新Mysql數(shù)據(jù)庫可以自定義為
多少分鐘進(jìn)行刷新一次或者訪問量達(dá)到一定數(shù)量再去刷新數(shù)據(jù)庫 這樣數(shù)據(jù)也是準(zhǔn)確的 效率也比直接每次刷新數(shù)據(jù)庫要高出許多了
既然給出了相應(yīng)的解決方案 我們就開始實施
我們以一篇帖子的瀏覽為例 我們先去創(chuàng)建對應(yīng)的控制器
$ php artisan make:controller PostController
再去生成需要用到的 Model
$ php artisan make:model Post -m
填寫posts的遷移表的字段內(nèi)容
Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string("title"); $table->string("content"); $table->integer('view_count')->unsigned(); $table->timestamps(); });
還有就是我們測試的數(shù)據(jù)的Seeder填充數(shù)據(jù)
$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');
當(dāng)然我們還是需要去寫我們訪問也就是瀏覽事件的(在app/providers/EventServiceProvider中定義)
protected $listen = [ 'App\Events\PostViewEvent' => [ // 'App\Listeners\EventListener', 'App\Listeners\PostEventListener', ], ];
執(zhí)行事件生成監(jiān)聽
$ php artisan event:generate
之前定義了相關(guān)的路由方法 現(xiàn)在去實現(xiàn)一下:
public function showPost(Request $request,$id) { //Redis緩存中沒有該post,則從數(shù)據(jù)庫中取值,并存入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(); //觸發(fā)瀏覽次數(shù)統(tǒng)計時間 event(new PostViewEvent($post, $ip)); return view('posts.show', compact('post')); }
這里看的出來就是以Redis作為緩存驅(qū)動 同樣的 會獲取獲取的ip目的是防止同一個ip多次刷新來增加瀏覽量
同樣的每次瀏覽會觸發(fā)我們之前定義的事件 傳入我們的post和id參數(shù)
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" 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="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script> <!-- 最新的 Bootstrap 核心 JavaScript 文件 --> <script src="http://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <script> </script> </body> </html>
初始化我們的事件就是接收一下這些參數(shù)即可
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'); } }
最主要的還是編寫我們的監(jiān)聽事件:
class PostEventListener { /** * 一個帖子的最大訪問數(shù) */ 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秒時間內(nèi),同一IP訪問多次,僅僅作為1次訪問量 if($this->ipViewLimit($id, $ip)){ //一個IP在300秒時間內(nèi)訪問第一次時,刷新下該篇post的瀏覽量 $this->updateCacheViewCount($id, $ip); } } /** * 限制同一IP一段時間內(nèi)得訪問,防止增加無效瀏覽次數(shù) * @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]); //如果集合中不存在這個建 那么新建一個并設(shè)置過期時間 if(!$existsInRedisSet){ //SADD,集合類型指令,向ipPostViewKey鍵中加一個值ip Redis::command('SADD', [$ipPostViewKey, $ip]); //并給該鍵設(shè)置生命時間,這里設(shè)置300秒,300秒后同一IP訪問就當(dāng)做是新的瀏覽量了 Redis::command('EXPIRE', [$ipPostViewKey, self::ipExpireSec]); return true; } return false; } /** * 達(dá)到要求更新數(shù)據(jù)庫的瀏覽量 * @param $id * @param $count */ public function updateModelViewCount($id, $count) { //訪問量達(dá)到300,再進(jìn)行一次SQL更新 $post = Post::find($id); $post->view_count += $count; $post->save(); } /** * 不同用戶訪問,更新緩存中瀏覽次數(shù) * @param $id * @param $ip */ public function updateCacheViewCount($id, $ip) { $cacheKey = 'post:view:'.$id; //這里以Redis哈希類型存儲鍵,就和數(shù)組類似,$cacheKey就類似數(shù)組名 如果這個key存在 if(Redis::command('HEXISTS', [$cacheKey, $ip])){ //哈希類型指令HINCRBY,就是給$cacheKey[$ip]加上一個值,這里一次訪問就是1 $save_count = Redis::command('HINCRBY', [$cacheKey, $ip, 1]); //redis中這個存儲瀏覽量的值達(dá)到30后,就去刷新一次數(shù)據(jù)庫 if($save_count == self::postViewLimit){ $this->updateModelViewCount($id, $save_count); //本篇post,redis中瀏覽量刷進(jìn)MySQL后,就把該篇post的瀏覽量清空,重新開始計數(shù) Redis::command('HDEL', [$cacheKey, $ip]); Redis::command('DEL', ['laravel:post:cache:'.$id]); } }else{ //哈希類型指令HSET,和數(shù)組類似,就像$cacheKey[$ip] = 1; Redis::command('HSET', [$cacheKey, $ip, '1']); } } }
最后可以通過我們的工具查看具體效果
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)鏈接
- PHP的Laravel框架結(jié)合MySQL與Redis數(shù)據(jù)庫的使用部署
- Laravel框架使用Redis的方法詳解
- laravel使用Redis實現(xiàn)網(wǎng)站緩存讀取的方法詳解
- 關(guān)于 Laravel Redis 多個進(jìn)程同時取隊列問題詳解
- Laravel框架實現(xiàn)redis集群的方法分析
- Laravel如何使用Redis共享Session
- laravel配置Redis多個庫的實現(xiàn)方法
- Laravel的Auth驗證Token驗證使用自定義Redis的例子
- laravel項目利用twemproxy部署redis集群的完整步驟
- laravel中Redis隊列監(jiān)聽中斷的分析
相關(guān)文章
php+redis實現(xiàn)多臺服務(wù)器內(nèi)網(wǎng)存儲session并讀取示例
這篇文章主要介紹了php+redis實現(xiàn)多臺服務(wù)器內(nèi)網(wǎng)存儲session并讀取示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01laravel5.4利用163郵箱發(fā)送郵件的步驟詳解
發(fā)送郵件是我們?nèi)粘T陂_發(fā)中必不可少會遇到的一個需求,下面這篇文章主要給大家介紹了關(guān)于laravel5.4利用163郵箱發(fā)送郵件的步驟,文中通過示例代碼和圖片介紹的非常詳細(xì),需要的朋友可以參考下。2017-09-09Laravel實現(xiàn)構(gòu)造函數(shù)自動依賴注入的方法
這篇文章主要介紹了Laravel實現(xiàn)構(gòu)造函數(shù)自動依賴注入的方法,涉及Laravel構(gòu)造函數(shù)自動初始化的相關(guān)技巧,需要的朋友可以參考下2016-03-03PHP傳值到不同頁面的三種常見方式及php和html之間傳值問題
這篇文章主要介紹了PHP傳值到不同頁面的三種常見方式及php和html之間傳值問題的相關(guān)資料,需要的朋友可以參考下2015-11-11Bootstrap+PHP實現(xiàn)多圖上傳功能實例詳解
這篇文章主要介紹了Bootstrap+PHP實現(xiàn)多圖上傳功能實例詳解,本文圖片加實例相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-04-04