- Overview
- Features
- Requirements
- Installation
- Basic Usage
- Advanced Usage
- API Reference
- Security Considerations
- Examples
The Cache class provides a simple yet robust file-based caching system for PHP applications. It supports subfolder organization, cache expiration, and automatic compression of cached content.
- File-based caching with SHA1 hashing
- Support for subfolder organization
- Automatic content compression using gzip
- Cache expiration management
- Maximum cache file limit with automatic cleanup
- Security features (.htaccess protection)
- Directory structure auto-creation
- PHP 7.4 or higher
- Write permissions on the cache directory
- zlib extension (for compression)
- Copy the
Cache.phpfile to your project - Include the class in your PHP script:
require_once 'path/to/Cache.php';// Create a cache instance with default settings
$cache = new Cache('/path/to/cache/directory');
// Or with custom max files limit
$cache = new Cache('/path/to/cache/directory', 5000);// Store data in cache
$cache->write('unique-key', 'Data to be cached');// Basic read
$data = $cache->read('unique-key');
// Read with expiration (86400 seconds = 1 day)
$data = $cache->read('unique-key', 86400);// Delete specific cache entry
$cache->delete('unique-key');
// Clear all cache
$cache->clearAll();// Set a subfolder for organizing cache files
$cache->setSubFolder('api-responses');
$cache->write('api-key', $apiResponse);// Write cache
$cache->write('temp-data', $data);
// Read with 1-hour expiration
$data = $cache->read('temp-data', 3600);
// Check if cache exists and is valid
if ($cache->checkCache('temp-data', 3600)) {
// Cache is valid
}// Clear cache files older than 1 day
$cache->clear(86400);
// Clear all cache files
$cache->clear(0);public function __construct(string $cacheDirectory, int $maxCacheFiles = 10000)public function write(string $cacheName, string $content): voidpublic function read(string $cacheName, int $maxAge = 0, bool $deleteExpired = TRUE): ?stringpublic function delete(string $cacheName): voidpublic function clear(int $maxAge = 0): voidpublic function clearAll(): boolpublic function setSubFolder(string $subFolder): voidpublic function checkCache(string $cacheName, int $maxAge = 0, bool $deleteExpired = TRUE): bool- The class automatically creates a
.htaccessfile to prevent direct access to cache files - Cache directory should be outside the web root
- Proper permissions should be set on the cache directory
- Cache keys should be validated before use
$cache = new Cache('/path/to/cache');
$cache->setSubFolder('api');
$apiUrl = 'https://api.example.com/data';
$cacheKey = 'api-' . md5($apiUrl);
// Try to get from cache first
if ($data = $cache->read($cacheKey, 3600)) {
return json_decode($data);
}
// If not in cache, fetch and store
$response = file_get_contents($apiUrl);
$cache->write($cacheKey, $response);
return json_decode($response);$cache = new Cache('/path/to/cache');
$cache->setSubFolder('db-queries');
$queryKey = 'user-list-' . md5($sql);
// Try cache first
if ($data = $cache->read($queryKey, 1800)) {
return unserialize($data);
}
// If not in cache, query and store
$result = $db->query($sql)->fetchAll();
$cache->write($queryKey, serialize($result));
return $result;$cache = new Cache('/path/to/cache');
// API responses - 1 hour cache
$cache->setSubFolder('api');
$apiData = $cache->read('api-key', 3600);
// User sessions - 24 hour cache
$cache->setSubFolder('sessions');
$sessionData = $cache->read('session-key', 86400);
// Template fragments - 1 week cache
$cache->setSubFolder('templates');
$templateData = $cache->read('template-key', 604800);