litespeedtech / lscache-laravel

LSCache for Laravel framework
GNU General Public License v3.0
51 stars 14 forks source link

Lscache Problem with middlewares #13

Closed mrmmg closed 4 years ago

mrmmg commented 4 years ago

Hello Thanks for the good package you have prepared for Laravel, I used this package in my project but there is a strange problem with it. First, read the following scenario: I have written a custom middleware to track my site visits, please see the following snippet:

routes (web.php)

Route::group(['middleware' => 'tracker'], function (){

    Route::get('/', 'HomeController@home')->name('root');

    //Cache for 1 week
    Route::group(['middleware' => 'lscache:max-age=604800;public;esi=on', 'lstags:1wcache'], function (){
        Route::get('/experience','HomeController@showExperience');
        Route::get('/contact','ContactController@show')->name('contact');
        Route::get('/donate','PaymentController@show')->name('donate-page');
    });

    //Cache for 1 day
    Route::group(['middleware' => 'lscache:max-age=86400;public;esi=on', 'lstags:1dcache'], function (){
        Route::get('/portfolio','HomeController@showPortfolio');
    });

    //Cache for 12 hrs
    Route::group(['middleware' => 'lscache:max-age=3600;public;esi=on', 'lstags:12hcache'], function (){
        Route::get('/blog','HomeController@blog');
        Route::get('/blog/{post}','HomeController@showPost');
        Route::get('/p/{id}','HomeController@shortLink');
    });
});

my middleware:

<?php

namespace App\Http\Middleware;

use App\Jobs\ProcessUserTracker;
use Closure;
use Jenssegers\Agent\Agent;

use Illuminate\Support\Facades\Log;

class UserTracker
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next){

        //get user ip
        $ip = geoip()->getClientIP();
        $userTrack = geoip($ip);
        $agent = new Agent();

        //use jobs to make application faster
        ProcessUserTracker::dispatch($userTrack, $agent);

        return $next($request);
    }
}

And problem statement:

When the site is visited for the first time, nothing is cached and my custom middleware is executed, but for the next time, the same middleware will not be executed at all.

I have disabled the entire cache process and the problem is solved!!!

Can you help me solve this problem?

lucasRolff commented 4 years ago

You would not be able to track it using a middleware, since the middleware (and the PHP code in general) won't be executed on a cache-hit - since the content is delivered directly by the webserver, we'll never hit PHP.

The only way you can really do it, is to either execute an ajax call (which doesn't use any form of caching), or track the visits using tools like Matomo or similar (Which technically does the same with a PHP file that gets information from the request).

mrmmg commented 4 years ago

@lucasRolff Thank you, I changed my method to an ajax call and it's worked.