Tuesday, June 28, 2016

How to count Page Views using Redis? | Max View Counter using Redis


page view count with redis



In this short tutorial I'm gonna talking about how you can count No of page views or Article Views in your Blog using Redis. You can ask why using Redis? Because Redis will keep the data in session so that no need of storing the data in Database or View Counts.

So Let's start the session:

1. Let''s define the Route first:

Routes.php
Route::get('/article/{id}', [
   'uses'=>'UserController@pageView',
   'as'=>'page'
]);
2. Let's define the UserController@PageView

UserController.php


public function pageView(Request $request,$id)
        {
            $redis = \Redis::connection();
            $views=$redis->incr('article'.$id.'view');
            return 'This is article with id: '.$id.' And It has '.$views. ' Views';
        }

That's it. Now if you access the route, you'll see the No of page views Like following:


page view counter demo


Ok, So that's it for today, Hope you enjoyed the Post. Stay connected with my Blog.
Thank you!




Related Articles

0 comments:

Post a Comment