PHPackages                             infocyph/cachelayer - 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. [Database &amp; ORM](/categories/database)
4. /
5. infocyph/cachelayer

ActiveLibrary[Database &amp; ORM](/categories/database)

infocyph/cachelayer
===================

PSR-6/PSR-16 cache layer with local, distributed, and cloud adapters.

1.2.2(1mo ago)01.2k↓61.5%3MITPHPPHP &gt;=8.3CI passing

Since Apr 12Pushed 1w agoCompare

[ Source](https://github.com/infocyph/CacheLayer)[ Packagist](https://packagist.org/packages/infocyph/cachelayer)[ RSS](/packages/infocyph-cachelayer/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (3)Dependencies (10)Versions (7)Used By (3)

CacheLayer
==========

[](#cachelayer)

[![Security & Standards](https://github.com/infocyph/CacheLayer/actions/workflows/security-standards.yml/badge.svg)](https://github.com/infocyph/CacheLayer/actions/workflows/security-standards.yml)[![Packagist Downloads](https://camo.githubusercontent.com/73d9afd6e343d6d4672a717092423212e1643d9fbebebc0db23dd8727a07e9cc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f696e666f637970682f43616368654c617965723f636f6c6f723d677265656e266c696e6b3d68747470732533412532462532467061636b61676973742e6f72672532467061636b61676573253246696e666f6379706825324643616368654c61796572)](https://camo.githubusercontent.com/73d9afd6e343d6d4672a717092423212e1643d9fbebebc0db23dd8727a07e9cc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f696e666f637970682f43616368654c617965723f636f6c6f723d677265656e266c696e6b3d68747470732533412532462532467061636b61676973742e6f72672532467061636b61676573253246696e666f6379706825324643616368654c61796572)[![License: MIT](https://camo.githubusercontent.com/784362b26e4b3546254f1893e778ba64616e362bd6ac791991d2c9e880a3a64e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d677265656e2e737667)](https://opensource.org/licenses/MIT)[![Packagist Version](https://camo.githubusercontent.com/4639e76f366e2456903a41c99d03e87dac090b47f67156d421f1d6137c3d7983/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696e666f637970682f43616368654c61796572)](https://camo.githubusercontent.com/4639e76f366e2456903a41c99d03e87dac090b47f67156d421f1d6137c3d7983/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696e666f637970682f43616368654c61796572)[![Packagist PHP Version](https://camo.githubusercontent.com/54a83aeceedabb95d6366e5b3644c7a6b06249a7ba96ba186e3e4418dd09fdf9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f696e666f637970682f43616368654c617965722f706870)](https://camo.githubusercontent.com/54a83aeceedabb95d6366e5b3644c7a6b06249a7ba96ba186e3e4418dd09fdf9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f696e666f637970682f43616368654c617965722f706870)[![GitHub Code Size](https://camo.githubusercontent.com/8bdb99ec23667dc6848af51ec501046581cd44f977d65a655de42605df31153d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f696e666f637970682f43616368654c61796572)](https://camo.githubusercontent.com/8bdb99ec23667dc6848af51ec501046581cd44f977d65a655de42605df31153d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f696e666f637970682f43616368654c61796572)[![Documentation](https://camo.githubusercontent.com/58099d137cff8c87a1e2e61cafb7d6d3c48485db538d280faca78dfd6b176432/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f446f63756d656e746174696f6e2d43616368654c617965722d626c75653f6c6f676f3d72656164746865646f6373266c6f676f436f6c6f723d7768697465)](https://docs.infocyph.com/projects/CacheLayer/)

CacheLayer is a standalone cache toolkit for modern PHP applications. It provides a unified API over PSR-6 and PSR-16 with local, distributed, and cloud adapters.

Project Background
------------------

[](#project-background)

CacheLayer was separated from the existing Intermix project to improve package visibility, maintenance focus, and faster feature enrichment for caching.

Features
--------

[](#features)

- Unified `Cache` facade implementing PSR-6, PSR-16, `ArrayAccess`, and `Countable`
- Adapter support for APCu, File, PHP Files, Memcached, Redis, Valkey, Redis Cluster, PDO (SQLite default), Shared Memory, MongoDB, and ScyllaDB
- Tiered cache composition via `Cache::tiered()` (L1/L2/... descriptors or pool instances)
- Node-local APCu L1 + SQLite L2 cache via `NodeCache`
- Durable multi-node invalidation coordination via `ClusterCache`
- Tagged invalidation with versioned tags: `setTagged()`, `invalidateTag()`, `invalidateTags()`
- Stampede-safe `remember()` with pluggable lock providers
- Per-adapter metrics counters and export hooks
- Payload compression controls
- Value serializer helpers for closures/resources
- Memoization helpers: `memoize()`, `remember()`, `once()`

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

[](#requirements)

- PHP 8.3+
- Composer

Optional extensions/packages depend on adapter choice:

- `ext-apcu`
- `ext-redis`
- `ext-memcached`
- `ext-pdo` + driver (`pdo_sqlite`, `pdo_pgsql`, `pdo_mysql`, ...)
- `ext-sysvshm`
- `mongodb/mongodb`
- `ext-cassandra`

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

[](#installation)

```
composer require infocyph/cachelayer
```

Usage
-----

[](#usage)

```
use Infocyph\CacheLayer\Cache\Cache;

$cache = Cache::pdo('app'); // defaults to sqlite file under sys temp cachelayer/pdo

$cache->setTagged('user:1', ['name' => 'Ada'], ['users'], 300);

$user = $cache->remember('user:1', function ($item) {
    $item->expiresAfter(300);
    return ['name' => 'Ada'];
}, tags: ['users']);

$cache->invalidateTag('users');

$metrics = $cache->exportMetrics();
```

Tiered Flow (L1 -&gt; L2 -&gt; DB)
----------------------------------

[](#tiered-flow-l1---l2---db)

```
use Infocyph\CacheLayer\Cache\Cache;

$cache = Cache::tiered([
    ['driver' => 'apcu', 'namespace' => 'app'], // L1
    ['driver' => 'valkey', 'namespace' => 'app', 'dsn' => 'valkey://127.0.0.1:6379'], // L2
], writeToL1: false); // optional L1 write-through

$value = $cache->remember('user:42', function ($item) use ($pdo) {
    $item->expiresAfter(300);

    $stmt = $pdo->prepare('SELECT payload FROM users_cache_source WHERE id = ?');
    $stmt->execute([42]);

    return $stmt->fetchColumn();
});
```

Request flow:

- check APCu (L1)
- check Redis/Valkey (L2)
- query DB on miss
- write L2
- optionally write L1 (controlled by `writeToL1`)

Node-local cache (APCu -&gt; SQLite)
------------------------------------

[](#node-local-cache-apcu---sqlite)

```
use Infocyph\CacheLayer\Node\NodeCache;
use Infocyph\CacheLayer\Node\NodeCacheConfig;

$cache = NodeCache::create(new NodeCacheConfig(
    namespace: 'app',
    sqliteFile: '/var/cache/my-app/cache.sqlite',
));
```

This topology keeps an independent, disposable cache on each application server: APCu provides hot in-memory reads when available, while SQLite keeps a larger local cache across PHP-FPM restarts. It does not synchronize entries or invalidation across servers.

Cluster invalidation
--------------------

[](#cluster-invalidation)

```
use Infocyph\CacheLayer\Cluster\ClusterCache;
use Infocyph\CacheLayer\Cluster\ClusterCacheConfig;

$cluster = ClusterCache::create(
    node: new NodeCacheConfig(sqliteFile: '/var/cache/my-app/cache.sqlite'),
    cluster: new ClusterCacheConfig('production', gethostname()),
    transport: $durableInvalidationTransport,
);

$cluster->invalidateKey('product.42');
$cluster->consume();
```

Cluster Cache distributes only durable invalidation events. It never replicates cached values, APCu state, or SQLite files; ordinary reads and writes remain local to each application node.

Security Hardening
------------------

[](#security-hardening)

CacheLayer includes optional payload/serialization hardening controls:

```
$cache
    ->configurePayloadSecurity(
        integrityKey: 'replace-with-strong-secret',
        maxPayloadBytes: 8_388_608,
    )
    ->configureSerializationSecurity(
        allowClosurePayloads: false,
        allowObjectPayloads: false,
    );
```

You can also set:

- `CACHELAYER_PAYLOAD_INTEGRITY_KEY`
- `CACHELAYER_MAX_PAYLOAD_BYTES`

Testing
-------

[](#testing)

```
composer test:code
```

Or run the full test pipeline:

```
composer test:all
```

Security
--------

[](#security)

Protected by [PHPForge](https://github.com/infocyph/PHPForge) — an automated quality and security gate for PHP projects.

---

 **Made with ❤️ for the PHP community**
 [MIT Licensed](LICENSE)
 [Documentation](https://docs.infocyph.com/projects/CacheLayer) • [Security](SECURITY.md) • [Code of Conduct](CODE_OF_CONDUCT.md) • [Contributing](CONTRIBUTING.md) • [Report | Request | Suggest](https://github.com/infocyph/CacheLayer/issues)

###  Health Score

48

—

FairBetter than 94% of packages

Maintenance96

Actively maintained with recent releases

Popularity20

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 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 ~17 days

Total

5

Last Release

32d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/33b750b84ab22f2038ef9b90ee48f2fcdaab6b5698e9f956a89a152466200b63?d=identicon)[abmmhasan](/maintainers/abmmhasan)

---

Top Contributors

[![abmmhasan](https://avatars.githubusercontent.com/u/7711128?v=4)](https://github.com/abmmhasan "abmmhasan (54 commits)")

---

Tags

mysqlsqlitepostgresmariadbpdorediscachepsr-16psr-6memcachedapcumongodbmemoizationweakmapvalkeymemoizescylladbchain-cache

### Embed Badge

![Health badge](/badges/infocyph-cachelayer/health.svg)

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

###  Alternatives

[apix/cache

A thin PSR-6 cache wrapper with a generic interface to various caching backends emphasising cache taggging and indexing to Redis, Memcached, PDO/SQL, APC and other adapters.

117548.2k6](/packages/apix-cache)[doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.

9.7k605.0M7.0k](/packages/doctrine-dbal)[matthiasmullie/scrapbook

Scrapbook is a PHP cache library, with adapters for e.g. Memcached, Redis, Couchbase, APCu, SQL and additional capabilities (e.g. transactions, stampede protection) built on top.

3232.8M46](/packages/matthiasmullie-scrapbook)[phpfastcache/phpfastcache

PHP Abstract Cache Class - Reduce your database call using cache system. Phpfastcache handles a lot of drivers such as Apc(u), Cassandra, CouchBase, Couchdb, Dynamodb, Firestore, Mongodb, Files, (P)redis, Leveldb, Memcache(d), Ravendb, Ssdb, Sqlite, Wincache, Xcache, Zend Data Cache.

2.5k5.3M141](/packages/phpfastcache-phpfastcache)[desarrolla2/cache

Provides an cache interface for several adapters Apc, Apcu, File, Mongo, Memcache, Memcached, Mysql, Mongo, Redis is supported.

1342.5M48](/packages/desarrolla2-cache)[rybakit/phive-queue

$queue-&gt;push('I can be popped off after', '10 minutes');

16441.9k1](/packages/rybakit-phive-queue)

PHPackages © 2026

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