Guide Usage Cache
The Cache.php file in the system/Drivers/Cache/ directory of the PHPFast framework is an abstract class that provides the basic methods for managing cache. This class defines abstract methods that must be implemented by its subclasses, creating a consistent interface for storing and retrieving cached data.
Cache Initialization:
The constructor method accepts cache configuration and calls the method to connect to the specific cache driver.
Connecting to Cache Driver:
The abstract method
connect()is used to connect to specific cache drivers such as Redis, File-based, etc.
Storing and Retrieving Cached Data:
The Cache.php class defines the following abstract methods, which are implemented in subclasses to handle cache operations efficiently:
set($key, $value, $ttl)– Stores data in the cache with a specified time-to-live (TTL).get($key)– Retrieves cached data using a unique key.delete($key)– Removes a specific item from the cache.has($key)– Checks if a key exists in the cache.clear()– Clears all cached data.
These methods ensure a consistent and structured approach to cache management across different caching backends.
Configuration Cache
See more at Configurations
Example Usage of Cache.php
Cache.phpStoring Data in Cache
<?php
// Create a cache object
$cache = new RedisCache($config);
$phpFast = 'Welcome to PHPFast Framework";
// Store data in cache with a TTL of 10 minutes (600 seconds)
$cache->set('php_fast', $phpFast, 600);Retrieving Data from Cache
<?php
// Retrieve data from cache
$phpFast = $cache->get('php_fast');
if ($phpFast !== null) {
// Data found in cache
echo "Data loaded from cache.";
} else {
// Data not found in cache, need to load from another source
echo "Data not found in cache.";
}Deleting Data from Cache
<?php
// Delete data from cache
$cache->delete('php_fast');Subclasses of Cache.php
Cache.phpThe Cache.php class acts as a parent class for multiple subclasses, each implementing its own caching mechanism while adhering to the standard interface defined in Cache.php. Key subclasses include:
UriCache.php– Optimizes request handling by caching URI-based data.RedisCache.php– Provides high-performance caching using Redis.FilesCache.php– Implements file-based caching by storing data in structured files.
Each subclass implements the abstract methods defined in Cache.php, ensuring a modular and extensible caching system within the PHPFast framework.
Last updated
Was this helpful?