PHPackages                             mk4u/cache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. mk4u/cache

ActiveLibrary[Caching](/categories/caching)

mk4u/cache
==========

A simple cache library.

v0.4(7mo ago)1381MITPHP

Since Aug 21Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/al3x5dev/cache)[ Packagist](https://packagist.org/packages/mk4u/cache)[ RSS](/packages/mk4u-cache/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (2)Versions (13)Used By (1)

Mk4U\\Cache
===========

[](#mk4ucache)

[![GitHub Release](https://camo.githubusercontent.com/8cb747badcfb5b94bf7366acce79d157b99c0b6cfec2b8985ebde5d22c6912d5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f616c657873616e64726f7631362f63616368653f696e636c7564655f70726572656c6561736573267374796c653d666c61742d73717561726526636f6c6f723d626c7565)](https://camo.githubusercontent.com/8cb747badcfb5b94bf7366acce79d157b99c0b6cfec2b8985ebde5d22c6912d5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f616c657873616e64726f7631362f63616368653f696e636c7564655f70726572656c6561736573267374796c653d666c61742d73717561726526636f6c6f723d626c7565)[![GitHub code size in bytes](https://camo.githubusercontent.com/9aef321b3a3d9bad65299293d1a8f5239df33e13761462baca04c268425eaa5a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f616c657873616e64726f7631362f63616368653f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/9aef321b3a3d9bad65299293d1a8f5239df33e13761462baca04c268425eaa5a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f616c657873616e64726f7631362f63616368653f7374796c653d666c61742d737175617265)[![GitHub License](https://camo.githubusercontent.com/bd506f89f1f20a6814ee36c75c09be35b811f030346755f5fae21274a6c9996e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f616c657873616e64726f7631362f63616368653f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/bd506f89f1f20a6814ee36c75c09be35b811f030346755f5fae21274a6c9996e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f616c657873616e64726f7631362f63616368653f7374796c653d666c61742d737175617265)

A simple and flexible cache system for PHP.

Features
--------

[](#features)

- 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.

Requirements
------------

[](#requirements)

- 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)

Installation
------------

[](#installation)

```
composer require mk4u/cache
```

Usage.
------

[](#usage)

### Configuration

[](#configuration)

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
]
```

#### Example of use with the `Mk4U\Cache\Stores\File`

[](#example-of-use-with-the-mk4ucachestoresfile)

```
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.

#### Example of use with `Mk4U\Cache\Stores\Apcu`

[](#example-of-use-with-mk4ucachestoresapcu)

```
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).

#### Example of use with `Mk4U\Cache\Stores\Database`

[](#example-of-use-with-mk4ucachestoresdatabase)

```
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);
```

### Driver Comparison Table

[](#driver-comparison-table)

FeatureFileAPCuSQLiteMySQL/MariaDBBest Use CaseSmall datasets, simple applicationsHigh performance, volatile dataMedium datasets, persistent storage with good performanceDistributed applications, existing database infrastructurePersistenceYes (Files)No (Memory)Yes (Database file)Yes (Database server)PerformanceSlow (Disk I/O)Very Fast (Memory)GoodGood (Network dependent)ScalabilityLowLow (Single server)MediumHighNote

For SQLite and MySQL/MariaDB drivers, the cache table is created dynamically

### Available methods

[](#available-methods)

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`

#### Example of use of the methods

[](#example-of-use-of-the-methods)

##### Storing a value in the cache

[](#storing-a-value-in-the-cache)

```
// Store the value in the cache
$cache->set('my_key', 'Hello, World!');
```

##### Retrieve a value from the cache

[](#retrieve-a-value-from-the-cache)

```
// Retrieve cache value
$cachedValue = $cache->get('my_key', 'Default value');

echo $cachedValue; // Print: Hello, World!
```

##### Remove a value from the cache

[](#remove-a-value-from-the-cache)

```
// Delete the value from the cache
$cache->delete('my_key');
```

##### Checks if a value exists in the cache by its key

[](#checks-if-a-value-exists-in-the-cache-by-its-key)

```
// checks if a value exists
return $cache->has('my_key'); //false
```

##### Clear the entire cache

[](#clear-the-entire-cache)

```
// Clear the entire cache
$cache->clear();
```

###### Handling multiple values

[](#handling-multiple-values)

You can store, retrieve and delete multiple values from the cache using the setMultiple, getMultiple and deleteMultiple methods.

###### Storing multiple values

[](#storing-multiple-values)

```
$values = [
    'key1' => 'Value 1',
    'key2' => 'Value 2'
];

$cache->setMultiple($values);
```

###### Retrieve multiple values

[](#retrieve-multiple-values)

```
$keys = ['key1', 'key2'];
$cachedValues = $cache->getMultiple($keys, 'Default value');

print_r($cachedValues); // Print stored values
```

###### Delete multiple values

[](#delete-multiple-values)

```
$keysToDelete = ['key1', 'key2'];
$cache->deleteMultiple($keysToDelete);
```

### Exceptions

[](#exceptions)

The library throws the following exceptions:

- `Mk4U\Cache\Exceptions\CacheException`: for cache related errors.
- `Mk4U\Cache\Exceptions\InvalidArgumentException`: For invalid arguments.

Contributions
-------------

[](#contributions)

Contributions are welcome. If you wish to contribute, please open an issue or a pull request in the repository.

License
-------

[](#license)

This project is licensed under the [MIT License](https://github.com/al3x5dev/cache?tab=MIT-1-ov-file).

Contact
-------

[](#contact)

If you have any questions or comments, feel free to contact me at [Telegram](http://t.me/al3x5dev).

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance63

Regular maintenance activity

Popularity10

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~41 days

Recently: every ~78 days

Total

11

Last Release

224d ago

Major Versions

v0.3.2 → v1.x-dev2025-10-07

### Community

Maintainers

![](https://www.gravatar.com/avatar/ee7388f72cf6dfb30d963374d1faf32cdc320751d19c0dc0ce752a55179d1eb3?d=identicon)[Al3x5](/maintainers/Al3x5)

---

Top Contributors

[![al3x5dev](https://avatars.githubusercontent.com/u/139165436?v=4)](https://github.com/al3x5dev "al3x5dev (57 commits)")

---

Tags

apcucachecache-filemk4uphpphp-cache

### Embed Badge

![Health badge](/badges/mk4u-cache/health.svg)

```
[![Health](https://phpackages.com/badges/mk4u-cache/health.svg)](https://phpackages.com/packages/mk4u-cache)
```

###  Alternatives

[laminas/laminas-cache

Caching implementation with a variety of storage options, as well as codified caching strategies for callbacks, classes, and output

1076.9M130](/packages/laminas-laminas-cache)[cache/adapter-common

Common classes for PSR-6 adapters

11124.4M38](/packages/cache-adapter-common)[cache/filesystem-adapter

A PSR-6 cache implementation using filesystem. This implementation supports tags

705.8M82](/packages/cache-filesystem-adapter)[cache/array-adapter

A PSR-6 cache implementation using a php array. This implementation supports tags

548.3M151](/packages/cache-array-adapter)[cache/redis-adapter

A PSR-6 cache implementation using Redis (PhpRedis). This implementation supports tags

523.9M27](/packages/cache-redis-adapter)[cache/simple-cache-bridge

A PSR-6 bridge to PSR-16. This will make any PSR-6 cache compatible with SimpleCache.

423.1M27](/packages/cache-simple-cache-bridge)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
