Caching
Caching in Controllers
<?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
Using Cache
Setup in Controller
Store and Retrieve Cached Data
Clear and Delete Cached Data
Last updated
Was this helpful?