PHPackages                             damimpr/cache-multi-layer - 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. damimpr/cache-multi-layer

ActiveLibrary[Caching](/categories/caching)

damimpr/cache-multi-layer
=========================

PHP library used for fast management of a single cache system, or management of multiple systems with priority levels.v

2.1.1(4mo ago)3383↓50%Apache-2.0PHPPHP &gt;=8.2CI failing

Since Oct 7Pushed 1mo agoCompare

[ Source](https://github.com/DamImpr/cache-multi-layer)[ Packagist](https://packagist.org/packages/damimpr/cache-multi-layer)[ Docs](https://damianoimprota.dev)[ RSS](/packages/damimpr-cache-multi-layer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (5)Versions (12)Used By (0)

Cache multi layer
=================

[](#cache-multi-layer)

PHP library used for fast management of a single cache system, or management of multiple systems with priority levels.

The management of a single system works like a mask and allows transparent use of the cache by abstracting which one you want to use.

Multi-level management is conceived as a vertical hierarchy, where the highest priority is at the top, and lower priorities are gradually lower down. Write operations are performed on all levels. Read operations are performed by reading from the cache system with the highest priority, and if the data is not found, the next system is read. Whenever a read from a system is successful, the cache systems with higher priority that did not return any value are updated.

The currently implemented caches are:

- Apcu
- Redis
- Memcache

All cache systems support all primitive types (int, float, string) and instances of classes that implement the [Cacheable](src/Interface/Cacheable.php) interface. Arrays are also supported, provided that the elements they contain are as described above.

Install
-------

[](#install)

```
composer require damimpr/cache-multi-layer
```

Usage examples
--------------

[](#usage-examples)

### Single cache

[](#single-cache)

example with redis cache

```
use CacheMultiLayer\Enum\CacheEnum;
use CacheMultiLayer\Service\Cache;

/*
 * creates the redis instance located on the host “redis-server”
 * with a default TTL of 60 seconds
 */
$cache = Cache::factory(CacheEnum::REDIS, 60, ['server_address' => 'redis-server']);

// set new value
$x = 8;
$key = 'test_key';
$res = $cache->set($key, $x);

// get
$y = $cache->get($key); // the value of $y is 8

//test with object

$foo = new Foo(); //Foo implements Cacheable
$foo->x = 5;
$keyFoo = 'key_foo';
$resFoo = $cache->set($keyFoo, $foo);
$fooCache = $cache->get($keyFoo);

$testEqual = $keyFoo === $foo; //$testEquals could be false
$testEqualX = $keyFoo->x === $foo->x; //testEqualX is true
```

### Multi cache level

[](#multi-cache-level)

Example with apcu cache used with maximum priority and redis cache used with lower priority

```
use CacheMultiLayer\Enum\CacheEnum;
use CacheMultiLayer\Service\Cache;
use CacheMultiLayer\Service\CacheConfiguration;
use CacheMultiLayer\Service\CacheManager;

/*
 * creates the apcu instance as the highest priority cache and then
 * creates the redis instance located on the host ‘redis-server’ with a default TTL of 60 seconds with lower priority
 */
$cacheManager = CacheManager::factory();
$cacheManager->appendCache(Cache::factory(CacheEnum::APCU, 10));
$cacheManager->appendCache(Cache::factory(CacheEnum::REDIS, 65, ['server_address' => 'redis-server']));

// it's the same using cache configuration

$cacheConfiguration = new CacheConfiguration();
$cacheConfiguration->appendCacheLevel(CacheEnum::APCU, 10);
$cacheConfiguration->appendCacheLevel(CacheEnum::REDIS, 65, ['server_address' => 'redis-server']);
$cacheManager = CacheManager::factory($cacheConfiguration);

// set new value
$x = 8;
$key = 'test_key';
$res = $cacheManager->set($key, $x);

//wait 15 seconds
sleep(15);

$y = $cacheManager->get($key);
// the value of $y is 8 read from redis, and apcu, which had expired, has been refreshed
```

Contribuiting
-------------

[](#contribuiting)

If you would like to contribute to this library, there is a [docker](https://docs.docker.com/engine/install/) you can use in development.

The docker image has been developed to execute commands, which are executable through the [commands](commands) file, and are listed below:

- ```
    bash commands test-sw # Running the phpunit test suite
    ```
- ```
    bash commands update-vendor # Update composer packages
    ```
- ```
    bash commands php-cs-fixer # Format the code according to the PSR-12 standard using php-cs-fixer
    ```
- ```
    bash commands rector # Run powerful php tool useful for refactoring
    ```
- ```
    bash commands phpstan # Run powerful php tool useful finding bugs
    ```
- ```
    bash commands sh # Run the sh shell in the container
    ```

Credits
-------

[](#credits)

Powerful tools used:

- [php-cs-fixer](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer) for standard PSR-12 code formatting
- [rector](https://github.com/rectorphp/rector) for code refactoring
- [phpstan](https://phpstan.org/) for finding bugs

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance84

Actively maintained with recent releases

Popularity21

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 89.5% 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 ~12 days

Recently: every ~1 days

Total

9

Last Release

122d ago

Major Versions

1.3 → 2.02026-01-12

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/35658871?v=4)[drizella](/maintainers/drizella)[@drizella](https://github.com/drizella)

---

Top Contributors

[![DamImpr](https://avatars.githubusercontent.com/u/26581459?v=4)](https://github.com/DamImpr "DamImpr (187 commits)")[![actions-user](https://avatars.githubusercontent.com/u/65916846?v=4)](https://github.com/actions-user "actions-user (22 commits)")

---

Tags

cachecache-layercache-multi-layer

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/damimpr-cache-multi-layer/health.svg)

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

###  Alternatives

[cache/predis-adapter

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

272.6M13](/packages/cache-predis-adapter)[sobstel/metaphore

PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates or stampending herd).

100179.4k1](/packages/sobstel-metaphore)[contributte/redis

Redis client integration into Nette framework

181.6M2](/packages/contributte-redis)[pstaender/silverstripe-redis-cache

Enables Redis cache for SilverStripe

1199.1k](/packages/pstaender-silverstripe-redis-cache)[abouvier/slim-redis-cache

Redis cache middleware for Slim framework

172.0k](/packages/abouvier-slim-redis-cache)[quick/cache

This is a cache system that uses Redis for rapid caching.

122.7k](/packages/quick-cache)

PHPackages © 2026

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