前言
本文主要給大家介紹了關(guān)于Redis在Laravel項(xiàng)目中的應(yīng)用實(shí)例,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹:
在初步了解Redis在Laravel中的應(yīng)用 那么我們?cè)囅脒@樣的一個(gè)應(yīng)用場(chǎng)景 一個(gè)文章或者帖子的瀏覽次數(shù)的統(tǒng)計(jì) 如果只是每次增加一個(gè)瀏覽量
就到數(shù)據(jù)庫(kù)新增一個(gè)數(shù)據(jù) 如果請(qǐng)求來(lái)那個(gè)太大這對(duì)數(shù)據(jù)庫(kù)的消耗也就不言而喻了吧 那我們是不是可以有其他的解決方案
這里的解決方案就是 即使你的網(wǎng)站的請(qǐng)求量很大 那么每次增加一個(gè)訪問(wèn)量就在緩存中去進(jìn)行更改 至于刷新Mysql數(shù)據(jù)庫(kù)可以自定義為
多少分鐘進(jìn)行刷新一次或者訪問(wèn)量達(dá)到一定數(shù)量再去刷新數(shù)據(jù)庫(kù) 這樣數(shù)據(jù)也是準(zhǔn)確的 效率也比直接每次刷新數(shù)據(jù)庫(kù)要高出許多了
既然給出了相應(yīng)的解決方案 我們就開(kāi)始實(shí)施
我們以一篇帖子的瀏覽為例 我們先去創(chuàng)建對(duì)應(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(); });
還有就是我們測(cè)試的數(shù)據(jù)的Seeder填充數(shù)據(jù)
$factory->define(App\Post::class, function (Faker\Generator $faker) { return [ 'title' => $faker->sentence, 'content' => $faker->paragraph, 'view_count' => 0 ]; });
定義帖子的訪問(wèn)路由
Route::get('/post/{id}', 'PostController@showPost');
當(dāng)然我們還是需要去寫我們?cè)L問(wèn)也就是瀏覽事件的(在app/providers/EventServiceProvider中定義)
protected $listen = [ 'App\Events\PostViewEvent' => [ // 'App\Listeners\EventListener', 'App\Listeners\PostEventListener', ], ];
執(zhí)行事件生成監(jiān)聽(tīng)
$ php artisan event:generate
之前定義了相關(guān)的路由方法 現(xiàn)在去實(shí)現(xiàn)一下:
public function showPost(Request $request,$id) { //Redis緩存中沒(méi)有該post,則從數(shù)據(jù)庫(kù)中取值,并存入Redis中,該鍵值key='post:cache'.$id生命時(shí)間5分鐘 $post = Cache::remember('post:cache:'.$id, $this->cacheExpires, function () use ($id) { return Post::whereId($id)->first(); }); //獲取客戶端請(qǐng)求的IP $ip = $request->ip(); //觸發(fā)瀏覽次數(shù)統(tǒng)計(jì)時(shí)間 event(new PostViewEvent($post, $ip)); return view('posts.show', compact('post')); }
這里看的出來(lái)就是以Redis作為緩存驅(qū)動(dòng) 同樣的 會(huì)獲取獲取的ip目的是防止同一個(gè)ip多次刷新來(lái)增加瀏覽量
同樣的每次瀏覽會(huì)觸發(fā)我們之前定義的事件 傳入我們的post和id參數(shù)
Redis的key的命名以:分割 這樣可以理解為一個(gè)層級(jí)目錄 在可視化工具里就可以看的很明顯了
接下來(lái)就是給出我們的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>