alexdodonov / mezon-router

Small and fast router
https://twitter.com/mezonphp
267 stars 18 forks source link

How are routes and regex cached and compiled inside mezon router.?? #21

Closed kingx619 closed 3 years ago

kingx619 commented 3 years ago

I can see that the methods dumpOnDisk(); and loadFromDisk inside the routeSet trait are not linked and used anywhere inside the code. I just want to understand how mezon router caching is done for regex and the routes...?? I can also see warmRoutes(); method not being used anywhere in the main Router code. .Can you explain it Alex

alexdodonov commented 3 years ago

Hi! You are right - you should look at the method warmRoutes. You will find caching details in it.

But using this method or not should be descided by the client code. Mezon Router just provides such functionality.

The main idea - is to provide ability to start using Mezon Router with minimum amount of lines of code. And then step by step improve performance with caching and dumping on disk.

And by the way - in some cases warming the cache is not necessary and will simply slow down your code.

kingx619 commented 3 years ago

I cannot see the warmRoutes(); method being used anywhere insie the code. You have provided it as the option. But I cannot see it being used any where to cache the contents. Please let me know how this router is caching the contents and precompiling the regexps inside caches

alexdodonov commented 3 years ago

Here is the example https://github.com/alexdodonov/mezon-router-benchmark/blob/main/Benchmarks/Mezon/MezonReactParamBench.php

kingx619 commented 3 years ago

Thanks for the info

alexdodonov commented 3 years ago

Can I close the issue now?

simp-lee commented 2 years ago

Here is the example https://github.com/alexdodonov/mezon-router-benchmark/blob/main/Benchmarks/Mezon/MezonReactParamBench.php

This link can not be visited. Where else can I find this example? dumpOnDisk(); and loadFromDisk(), How do the two methods improve performance? According to $router->addRoute() method, the routes have been written in the code. Why do we need to read them from the cache file again?

alexdodonov commented 2 years ago

Hi! Check this please:

// prepare cache
$router = new Router();

// add routes
$router->addRoute(/* route and controller */);
$router->addRoute(/* route and controller */);
$router->addRoute(/* route and controller */);
...

// dumping routes on disk
$router->dumpOnDisk('cache.static.php');

// reading routes from cache
$router->loadFromDisk('cache.static.php');

Hope it will be usefull )

simp-lee commented 2 years ago

Thanks for your quick reply.

About the cache, each time the code is executed, the cache will be written and read through dumpOnDisk() and loadFromDisk() methods.

How can the cache improve performance?
I don't understand how it works. :-)

alexdodonov commented 2 years ago

You need to split this code.

The first part:

// somewhere in your CI/CD pipeline - warming cache
$router = new Router();

// add routes
$router->addRoute(/* route and controller */);
$router->addRoute(/* route and controller */);
$router->addRoute(/* route and controller */);
...

// dumping routes on disk
$router->dumpOnDisk('cache.static.php');

The second part:

// somewhere in your application code - using this cache
$router->loadFromDisk('cache.static.php');
simp-lee commented 2 years ago

I tested the caching function in my local environment, and an error was returned:

// create router
$router = new  \Mezon\Router\Router();

// additing routes
$router->addRoute('/article/[i:id]', [new \App\Admin\Controllers\ArticleController(), 'mod'], 'GET');

// dumping routes on disk
$router->dumpOnDisk(\Core\App::Config()->get('route_path'));

// reading routes from cache
$router->loadFromDisk(\Core\App::Config()->get('route_path'));

// calling route
$router->callRoute($_SERVER['REQUEST_URI']);

return Fatal error:

Fatal error: Uncaught Error: Call to undefined method App\Admin\Controllers\ArticleController::__set_state() in E:\web\lee-php\runtime\route.php:37 Stack trace: #0 E:\web\lee-php\vendor\mezon\router\Mezon\Router\RoutesSet.php(204): require() #1 E:\web\lee-php\public\mezon.php(24): Mezon\Router\Router->loadFromDisk() #2 {main} thrown in E:\web\lee-php\runtime\route.php on line 37

cache php:

...
1 => 
  array (
    'GET' => 
    array (
      0 => 
      array (
        'bunch' => 
        array (
          2 => 
          array (
            'pattern' => 'article/[i:id]',
            'callback' => 
            array (
              0 => 
              App\Admin\Controllers\ArticleController::__set_state(array(
              )),
              1 => 'mod',
            ),
          ),
        ),
        'regexp' => '~^(?|article/([-+]{0,1}[0-9]+))$~',
      ),
    ),
    'POST' => 
    array (
    ),
    'PUT' => 
    array (
    ),
    'DELETE' => 
    array (
    ),
    'OPTION' => 
    array (
    ),
    'PATCH' => 
    array (
    ),
  ),
...
alexdodonov commented 2 years ago

You need to implement magic method __set_state in your \App\Admin\Controllers\ArticleController class.

Like it is described here: https://www.php.net/manual/en/language.oop5.magic.php#object.set-state