PHPSlim FrameworkPerformanceCachingOptimization

Implementasi Route Caching di Slim Framework: Boost Performance 65%

By Your Name•

Implementasi Route Caching di Slim Framework: Boost Performance 65%

Ketika aplikasi PHP kamu mulai berkembang dengan ratusan routes, routing menjadi salah satu bottleneck terbesar. Di artikel ini, aku akan share cara mengimplementasikan route caching di Slim Framework yang berhasil meningkatkan performa hingga 65%.

Problem: Slow Routing pada Production

Di salah satu project PHP legacy yang aku handle, aplikasi memiliki 200+ routes yang didefinisikan secara dinamis. Setiap request harus:

  1. Parse semua file route definitions
  2. Register routes ke router
  3. Match request ke route yang tepat

Proses ini menghabiskan ~150ms per request di production. Tidak efisien!

Solution: Route Caching

Route caching bekerja dengan cara meng-cache hasil routing ke file, sehingga aplikasi tidak perlu re-parse route definitions di setiap request.

Step 1: Setup Route Cache Directory

Pertama, buat directory untuk menyimpan cache:

mkdir -p var/cache/routes
chmod 775 var/cache/routes

Step 2: Implementasi Route Cache Handler

Buat file

src/Middleware/RouteCacheMiddleware.php
:

<?php
namespace App\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Routing\RouteCollector;

class RouteCacheMiddleware implements MiddlewareInterface
{
    private string $cacheFile;
    private RouteCollector $routeCollector;

    public function __construct(RouteCollector $routeCollector, string $cacheDir)
    {
        $this->routeCollector = $routeCollector;
        $this->cacheFile = $cacheDir . '/routes.cache.php';
    }

    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler
    ): ResponseInterface {
        // Load cached routes if available
        if (file_exists($this->cacheFile)) {
            $cachedRoutes = require $this->cacheFile;
            $this->routeCollector->setCachedRoutes($cachedRoutes);
        }

        return $handler->handle($request);
    }
}

Step 3: Generate Cache File

Buat command untuk generate route cache:

<?php
// bin/cache-routes.php

require __DIR__ . '/../vendor/autoload.php';

$app = require __DIR__ . '/../bootstrap/app.php';

// Get all routes
$routeCollector = $app->getRouteCollector();
$routes = $routeCollector->getRoutes();

// Serialize and save to cache file
$cacheDir = __DIR__ . '/../var/cache/routes';
$cacheFile = $cacheDir . '/routes.cache.php';

$cacheContent = '<?php return ' . var_export($routes, true) . ';';
file_put_contents($cacheFile, $cacheContent);

echo "Route cache generated successfully!\n";

Step 4: Integrate ke Application

Di

bootstrap/app.php
, tambahkan middleware:

<?php
use App\Middleware\RouteCacheMiddleware;

$app->add(new RouteCacheMiddleware(
    $app->getRouteCollector(),
    __DIR__ . '/../var/cache/routes'
));

Benchmark Results

Setelah implementasi route caching, berikut hasil benchmarknya menggunakan Apache Bench:

Before Caching:

Requests per second:    66.78 [#/sec]
Time per request:       149.73 [ms]

After Caching:

Requests per second:    189.45 [#/sec]
Time per request:       52.79 [ms]

Improvement: 65% faster! šŸš€

Best Practices

  1. Clear cache setelah update routes: Jalankan
    php bin/cache-routes.php
    setiap deploy
  2. Disable di development: Gunakan environment variable untuk disable caching di dev
  3. Add to CI/CD: Integrate cache generation ke deployment pipeline
if (getenv('APP_ENV') === 'production') {
    $app->add(new RouteCacheMiddleware(...));
}

Kesimpulan

Route caching adalah low-hanging fruit untuk optimasi performance aplikasi Slim Framework. Dengan implementasi sederhana ini, kamu bisa mendapat peningkatan performa signifikan tanpa harus refactor besar-besaran.

Key Takeaways:

  • Route parsing adalah bottleneck di aplikasi dengan banyak routes
  • Caching mengurangi overhead hingga 65%
  • Implementasi mudah dan tidak invasif
  • Must-have untuk production apps

Ada pertanyaan atau masukan? Drop komentar atau reach out via email!

Enjoyed this article? Share it with others!