A simple and efficient PHP library for cache management.
- Easy to use
- Supports multiple data types: strings, arrays, objects, and booleans
- Allows setting cache expiration time
- Option to bypass cache
Use Composer to install this library:
composer require mrmanchot/simple-cache
require 'vendor/autoload.php';
$cache = new SimpleCache('/path/to/cache/directory/');
or
use Mrmanchot\SimpleCache\SimpleCache;
$cache = new SimpleCache('/path/to/cache/directory/');
// Set cache
$cache->set('key', 'value');
// Get cache
$value = $cache->get('key');
You can specify an expiration time in minutes using the $delayMinutes
parameter.
// Set cache with a 10-minute expiration time
$cache->set('key', 'value');
// Get cache, valid for 10 minutes
$value = $cache->get('key', 'string', 10);
You can also store arrays, objects, and booleans.
// Storing an array
$cache->set('array_key', ['a' => 1, 'b' => 2]);
// Retrieving an array
$array = $cache->get('array_key', 'array');
// Storing an object
$object = new stdClass();
$object->property = 'value';
$cache->set('object_key', $object);
// Retrieving an object
$object = $cache->get('object_key', 'object');
// Storing a boolean
$cache->set('boolean_key', true);
// Retrieving a boolean
$boolean = $cache->get('boolean_key', 'bool');
You can use subdirectories in keys for better organization.
// Set cache in a subdirectory
$cache->set('user/1', 'value');
// Get cache from a subdirectory
$value = $cache->get('user/1');
The clear method allows you to remove cached items based on a pattern. This is useful for batch invalidation of cache items.
// Clear a specific cache item
$cache->clear('key');
// Clear all cache items in a subdirectory
$cache->clear('user/*');
// Clear all cache items
$cache->clear('*');