> For the complete documentation index, see [llms.txt](https://docs.cmsfullform.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cmsfullform.com/documents/libraries/caching.md).

# Caching

PHPFast includes a powerful caching system that helps store and retrieve data efficiently. The framework provides a RedisCache class, offering essential methods like `set()`, `get()`, and `delete()` for interacting with a Redis server. This caching mechanism significantly improves performance, especially when handling data that doesn't change frequently.

## Caching in Controllers

To enable caching in your controllers, use the **RedisCache** class or other (see more at [Cache](/documents/drivers/cache.md)). Cache settings are configured in `application/Config/Config.php`

Here's how you can implement caching in `HomeController`. Simply uncomment the relevant caching lines to activate the functionality:

```php
<?php

namespace App\Controllers;

use System\Core\BaseController;
use System\Libraries\Render;
use System\Drivers\Cache\RedisCache;

class HomeController extends BaseController
{
    protected $usersModel;
    protected $cache;

    public function __construct()
    {
        // Load cache configuration and initialize Redis cache
        $config = config('cache');
        $this->cache = new RedisCache($config);
    }


    public function index()
    {
        $cacheKey = 'home_page';

        // Check if cached content is available
        if ($cachedContent = $this->cache->get($cacheKey)) {
            echo $cachedContent;
            echo 'Loaded from cache.<br />';
            return;
        }

        // Generate the full page content
        $content = $this->render('themes', 'home/home');

        // Store the rendered page in cache for 10 minutes
        $this->cache->set($cacheKey, $content, 600);

        // Display the generated content
        echo $content;
    }
}
```

## Configure Cache Settings

**Modify** `application/Config/Config.php` **to enable caching:**

```php
'cache' => [
    'cache_driver'  => 'redis',     // Use Redis as the caching system
    'cache_host'    => '127.0.0.1', // Redis server address
    'cache_port'    => 6379,        // Redis default port
    'cache_username'=> '',          // Leave blank if authentication is not required
    'cache_password'=> '',          // Set password if needed
    'cache_database'=> 0,           // Default Redis database
],
```

## Using Cache

### Setup in Controller

```php
use System\Drivers\Cache\RedisCache;

class HomeController extends BaseController {
    protected $cache;

    public function __construct() {
        $config = config('cache'); // Load cache configuration
        $this->cache = new RedisCache($config); // Initialize Redis caching
    }
}
```

### Store and Retrieve Cached Data

```php
// Store data
$this->cache->set('home_page_data', $data, 600); // Cache for 10 minutes (600 seconds)

// Retrieve data
$cachedData = $this->cache->get('home_page_data');
```

### Clear and Delete Cached Data

```php
// Delete a Specific Cache Entry
$this->cache->delete('home_page_data');

// Clear All Cache
$this->cache->clear();
```

{% hint style="info" %}
Note

* **Best for Static Content** – Use caching for data that doesn’t change often, like homepage content or user lists.
* **Set Expiration Time** – Always define a cache expiration time to avoid showing outdated information.
* **Customize as Needed** – Adjust caching settings based on your application’s needs for better performance and efficiency.
  {% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.cmsfullform.com/documents/libraries/caching.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
