Performance & Caching

Rhapsody includes several caching layers designed to significantly improve your application's speed and responsiveness, especially in a production environment. Understanding and utilizing these features is key to building a high-performance application. Caching is primarily managed through the Rhapsody Console.

Cache Drivers

The framework supports multiple cache backends, or "drivers." You can switch between them with a single configuration change in your .env file.


# Use the local filesystem for caching
CACHE_DRIVER=file

# Use a Redis server for caching
CACHE_DRIVER=redis

Using the Cache

The cache is managed by the Rhapsody\Core\Cache class, which is available via the service container. You should inject it into your controller's constructor. The most common way to interact with the cache is the remember() method.

The `remember()` Method

This method simplifies the process of getting and setting cache items:

  1. It checks if a value exists in the cache for a given key.
  2. If it exists and is not expired, it returns the cached value.
  3. If it does not exist, it executes the provided function, saves its return value to the cache for a specified number of minutes, and then returns that value.

use Rhapsody\Core\Cache;
use App\Models\Post;

class PostController extends BaseController
{
    // 1. Inject the Cache service in the constructor
    public function __construct(
        protected Post $postModel,
        protected Cache $cache,
        //... other dependencies
    ) {
        //...
    }
    
    public function index()
    {
        // 2. Use the injected instance to call remember()
        $posts = $this->cache->remember('posts.all', 60, function () {
            // This expensive query only runs when the cache is empty or expired
            return $this->postModel->findAll();
        });

        return $this->view('posts/index.twig', ['posts' => $posts]);
    }
}

This method works seamlessly regardless of which cache driver is active.

Other Cache Methods

Once injected, you can use other methods on your $this->cache instance:

  • $this->cache->get('key'): Retrieve an item from the cache.
  • $this->cache->put('key', $value, $minutes): Store an item in the cache.
  • $this->cache->has('key'): Check if an item exists in the cache.
  • $this->cache->forget('key'): Remove an item from the cache.

Twig Template Caching

Twig compiles templates into plain PHP files for faster rendering. In production, you should pre‑compile all templates to avoid the overhead of compilation on the fly. Rhapsody provides a console command to do this:

php rhapsody app:cache-warm

This command scans all your theme and core view directories, compiles every .twig template, and stores the compiled PHP files in the cache directory (usually storage/cache/views).

Run this command after deploying your application or after making changes to your templates. It ensures that your users experience the fastest possible response times.

You can also clear the compiled template cache with:

php rhapsody cache:clear

This removes all compiled templates, forcing Twig to recompile them on the next request (useful during development).


Route Caching

For high‑traffic applications, you can cache your route definitions to speed up routing. Generate a route cache with:

php rhapsody route:cache

This compiles all your routes into a PHP file that the router loads directly, bypassing the need to parse your route files on every request. Clear the route cache with:

php rhapsody route:clear
Tip: In production, run cache:clear and route:cache as part of your deployment script to ensure optimal performance.