Cache Class
The Cache class provides page-level output caching to improve website performance. it supports both file-based and Redis-backed caching strategies.
Configuration
Caching is configured via the system settings in the admin dashboard:
cache_enabled: Set to 'on' to enable the cache.
cache_type: 'file' (default) or 'redis'.
cache_path: Directory for file-based cache.
cache_timeout: TTL for cached items in seconds (default: 3600).
redis_*: Connection details for Redis (host, port, pass, db).
Methods
Start Method
Usage: Cache::start();
Call this method at the very beginning of the page execution (before any output). It:
- Checks if a valid cached version of the current URL exists.
- If found, it outputs the cache content and terminates the script immediately.
- If not found, it starts output buffering (
ob_start).
End Method
Usage: Cache::end();
Call this method at the very end of the page execution. It:
- Captures the buffered output content.
- Stores the content in the cache (file or Redis) with the configured timeout.
- Flushes the buffer to the browser.
Logic
The cache key is uniquely generated based on the current URL (host + request URI + query string) using an MD5 hash. This ensures that different pages and different query parameters result in separate cache entries.
Requirements
- File Cache: Requires write permissions to the configured
cache_path.
- Redis Cache: Requires the
php-redis extension to be installed and enabled on the server.