A simple and flexible cache system for PHP.
- Support for multiple cache drivers (APCu, files, SQLite and MySQL/MariaDB)
- Implementation of the
Psr\SimpleCache\CacheInterface
interface. - Efficient storage and retrieval of data.
Important
The file driver Mk4U\Cache\Stores\File
is suitable for storing small amounts of data in cache. However, as the number of records increases, performance may be affected due to the latency of disk I/O operations.
For applications that require storing large volumes of data or high performance, it is recommended to consider using in-memory cache drivers, such as APCu, or database drivers such as SQLite and MySQL/MariaDB which are designed to handle large amounts of data more efficiently.
- PHP 8.2 or higher
- APCu extension (optional)
- PDO Extension
- SQLite Extension (for the SQLite driver)
- MySQLi or PDO_MySQL Extension (for the MySQL/MariaDB driver)
composer require mk4u/cache
To use the library, you must first create an instance of the cache driver you want to use. The library includes a Mk4U\Cache\CacheFactory
that makes it easy to create instances of the cache drivers.
Tip
If no parameters are passed to the Mk4U\Cache\CacheFactory::create()
, an object of type Mk4U\Cache\Stores\File
will be created by default.
Note
By default the Mk4U\Cache\Stores\File
object sets the following configuration parameters:
[
// extension of cache files
'ext' =>'cache',
// directory where the cache will be stored, if it does not exist create it.
'dir' => '/cache',
// cache lifetime in seconds (default 5 minutes.)
'ttl' => 300
]
require 'vendor/autoload.php';
$cache = Mk4U\Cache\CacheFactory::create();
// Default Configuration Parameters
// .cache file extension
// Directory where the cache will be stored (/var/www/html/webapp/cache).
// Cache lifetime in seconds (default: 5 minutes).
// or
// Cache driver configuration
$config = [
'ext' => 'txt', // Extension of cache files.
'dir' => '/cache', // Directory where the cache will be stored
'ttl' => 3600 // Cache lifetime in seconds.
];
// Create an instance of the file cache driver.
$cache = Mk4U\Cache\CacheFactory::create('file', $config);
Important
Make sure you set the necessary permissions for the creation of directories and cache files.
require 'vendor/autoload.php';
// Cache driver configuration
$config = [
'ttl' => 3600 // cache lifetime in seconds (default 5 minutes.)
];
// Create an instance of the APCu cache driver.
$cache = Mk4U\Cache\CacheFactory::create('apcu', $config);
Mk4U\Cache\Stores\Apcu
has only one configurable parameter and it is ttl
, by default its value is 300 seconds (5 minutes).
require 'vendor/autoload.php';
// SQLite
$sqlite = [
// 'connection'=>'sqlite',
'database' => '/path/to/cache.sqlite',
// 'ttl' => 300
];
// Create an instance of the APCu cache driver.
$cache = Mk4U\Cache\CacheFactory::create('database', $sqlite);
// MySQL|MariaDB
$mysql = [
'connection'=>'mysql', // default sqlite
'host' => 'localhost', // default localhost
'port' => 3306, // default 3306
'database' => 'my_db',
'user' => 'root', // default root
'password' => '', // default empty
'ttl' => 3600 // default 5 minutes.
];
$cache = Mk4U\Cache\CacheFactory::create('database', $mysql);
Feature | File | APCu | SQLite | MySQL/MariaDB |
---|---|---|---|---|
Best Use Case | Small datasets, simple applications | High performance, volatile data | Medium datasets, persistent storage with good performance | Distributed applications, existing database infrastructure |
Persistence | Yes (Files) | No (Memory) | Yes (Database file) | Yes (Database server) |
Performance | Slow (Disk I/O) | Very Fast (Memory) | Good | Good (Network dependent) |
Scalability | Low | Low (Single server) | Medium | High |
Note
For SQLite and MySQL/MariaDB drivers, the cache table is created dynamically
The cache class implements the following methods of the CacheInterface interface:
get(string $key, mixed $default = null): mixed
set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
delete(string $key): bool
clear(): bool
getMultiple(iterable $keys, mixed $default = null): iterable
setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
deleteMultiple(iterable $keys): bool
has(string $key): bool
// Store the value in the cache
$cache->set('my_key', 'Hello, World!');
// Retrieve cache value
$cachedValue = $cache->get('my_key', 'Default value');
echo $cachedValue; // Print: Hello, World!
// Delete the value from the cache
$cache->delete('my_key');
// checks if a value exists
return $cache->has('my_key'); //false
// Clear the entire cache
$cache->clear();
You can store, retrieve and delete multiple values from the cache using the setMultiple, getMultiple and deleteMultiple methods.
$values = [
'key1' => 'Value 1',
'key2' => 'Value 2'
];
$cache->setMultiple($values);
$keys = ['key1', 'key2'];
$cachedValues = $cache->getMultiple($keys, 'Default value');
print_r($cachedValues); // Print stored values
$keysToDelete = ['key1', 'key2'];
$cache->deleteMultiple($keysToDelete);
The library throws the following exceptions:
Mk4U\Cache\Exceptions\CacheException
: for cache related errors.Mk4U\Cache\Exceptions\InvalidArgumentException
: For invalid arguments.
Contributions are welcome. If you wish to contribute, please open an issue or a pull request in the repository.
This project is licensed under the MIT License.
If you have any questions or comments, feel free to contact me at Telegram.