PHPackages                             jarir-ahmed/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. jarir-ahmed/cache

ActiveLibrary[Caching](/categories/caching)

jarir-ahmed/cache
=================

Framework-agnostic caching toolkit for PHP: PSR-16 + PSR-6, Redis/Memcached/APCu/File/PDO/tiered stores, tags, atomic locks, stampede protection, and HTTP caching (ETag/304, full-page).

00PHPCI passing

Since Jun 10Pushed todayCompare

[ Source](https://github.com/jarir2020/jarir-ahmed-cache)[ Packagist](https://packagist.org/packages/jarir-ahmed/cache)[ RSS](/packages/jarir-ahmed-cache/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

jarir-ahmed/cache
=================

[](#jarir-ahmedcache)

A **framework-agnostic caching toolkit** for PHP — every common cache type behind one clean API: in-memory, file, APCu, **Redis**, **Memcached**, PDO (database), and **tiered** (multi-layer). Plus **PSR-16 + PSR-6** for drop-in use in any framework, **tags**, **atomic locks**, **stampede protection**, and **HTTP caching** (ETag/304, full-page, PSR-15 middleware).

[![PHP](https://camo.githubusercontent.com/83dd395020c37276225039739320f6c8e7e99963ab21ee3d09282cb48dad2a60/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e312532422d626c7565)](https://php.net) [![PSR-16](https://camo.githubusercontent.com/07e552d088d8844b56139aaf6d419065dae0d1f59ab278d63ad8498fd6200b37/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5053522d2d31362d636f6d706c69616e742d677265656e)](https://www.php-fig.org/psr/psr-16/) [![PSR-6](https://camo.githubusercontent.com/a068f436de16fddaf2e10916d9d1c4ba9080f0e6c1fecc6119c92e5e77c74dbe/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5053522d2d362d636f6d706c69616e742d677265656e)](https://www.php-fig.org/psr/psr-6/) [![License](https://camo.githubusercontent.com/f8df3091bbe1149f398a5369b2c39e896766f9f6efba3477c63e9b4aa940ef14/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e)](LICENSE)

Zero required dependencies beyond the PSR cache interfaces. Redis and Memcached work **with no PHP extension** (built-in raw-socket clients) and automatically use `ext-redis` / `ext-memcached` when present.

Install
-------

[](#install)

```
composer require jarir-ahmed/cache
```

Why
---

[](#why)

"Framework-level caching for any framework" = implement the standards. This package is a PSR-16 **and** PSR-6 implementation, so it drops straight into Laravel, Symfony, Slim, Mezzio, Doctrine, or raw PHP — while also giving you a richer native API and an HTTP caching layer most libraries skip.

Quick start
-----------

[](#quick-start)

```
use JarirAhmed\Cache\Cache;
use JarirAhmed\Cache\Store\ArrayStore;

$cache = Cache::repository(new ArrayStore);

$cache->set('key', 'value', 60);                       // ttl seconds (null = forever)
$cache->get('key', 'default');
$cache->remember('user:1', 300, fn () => loadUser(1)); // compute once, then cache
$cache->increment('hits');
$cache->tags(['users'])->flush();
```

### Config-driven, named stores

[](#config-driven-named-stores)

```
$cache = Cache::create([
    'default'    => 'redis',
    'prefix'     => 'app:',
    'serializer' => 'php',            // php | json | igbinary
    'stores' => [
        'redis'     => ['driver' => 'redis', 'host' => '127.0.0.1', 'port' => 6379],
        'memcached' => ['driver' => 'memcached', 'host' => '127.0.0.1', 'port' => 11211],
        'files'     => ['driver' => 'file', 'path' => '/var/cache/app'],
        'db'        => ['driver' => 'pdo', 'pdo' => $pdo, 'table' => 'cache'],
        'fast'      => ['driver' => 'tiered', 'layers' => ['array', 'redis']],
    ],
]);

$cache->store('redis')->set('k', $value, 3600);
$cache->store('fast')->remember('hot', 60, fn () => compute());
```

Stores
------

[](#stores)

DriverClassNotes`array``ArrayStore`per-request memory`file``FileStore`sharded dirs, atomic writes`apcu``ApcuStore`shared memory (ext-apcu)`redis``RedisStore`**raw-socket RESP** or ext-redis`memcached``MemcachedStore`**raw-socket text proto** or ext-memcached`pdo``PdoStore`sqlite/mysql/pgsql, self-creating table`null``NullStore`disables caching`tiered``TieredStore`L1+L2, read-through back-fillRedis without any extension:

```
use JarirAhmed\Cache\Store\RedisStore;

$redis = Cache::repository(RedisStore::connect('127.0.0.1', 6379)); // uses ext-redis if loaded, else raw socket
```

PSR-16 and PSR-6 (use it in any framework)
------------------------------------------

[](#psr-16-and-psr-6-use-it-in-any-framework)

```
$psr16 = Cache::psr16(new RedisStore(...));   // Psr\SimpleCache\CacheInterface
$psr16->set('k', 'v', 3600);

$pool  = Cache::psr6(new FileStore('/tmp'));   // Psr\Cache\CacheItemPoolInterface
$item  = $pool->getItem('k');
if (! $item->isHit()) { $item->set(compute())->expiresAfter(600); $pool->save($item); }
```

- **Symfony**: pass the PSR-6 pool anywhere a `CacheItemPoolInterface` is expected.
- **Doctrine**: same — it consumes PSR-6.
- **Laravel**: use `Bridge\LaravelStore` to register a custom driver (`Cache::extend(...)`).

Tags
----

[](#tags)

```
$cache->tags(['users', 'profiles'])->put('u:1', $data, 3600);
$cache->tags(['users'])->flush();              // invalidate every 'users' entry at once
```

Atomic locks &amp; stampede protection
--------------------------------------

[](#atomic-locks--stampede-protection)

```
// distributed lock
$cache->lock('import', 30)->get(function () {
    runImportOnce();                           // only one process at a time
});

// single-flight: under load, ONE caller computes, the rest wait for the result
$report = $cache->rememberLocked('daily-report', 600, fn () => buildExpensiveReport());
```

HTTP caching
------------

[](#http-caching)

```
use JarirAhmed\Cache\Http\HttpCache;
use JarirAhmed\Cache\Http\CacheControl;

echo HttpCache::serve($body, [
    'etag'          => true,
    'cache_control' => CacheControl::make()->public()->maxAge(300)->staleWhileRevalidate(60),
]);   // emits ETag + Cache-Control, replies 304 automatically on If-None-Match
```

Full-page cache:

```
use JarirAhmed\Cache\Http\PageCache;

$page = new PageCache($cache->store(), ttl: 120);

if (($html = $page->start('home')) !== null) { echo $html; return; } // cache hit
// ... render your page ...
echo $page->end('home');                                             // capture + store
```

PSR-15 middleware (Slim/Mezzio/Laminas):

```
use JarirAhmed\Cache\Http\CacheMiddleware;

$app->add(new CacheMiddleware($cache->store(), $psr17ResponseFactory, ttl: 60));
```

Serializers
-----------

[](#serializers)

`php` (default, any value), `json` (portable, scalars/arrays), `igbinary` (compact binary, needs ext-igbinary). Set per store via the `serializer` config key or pass a `Serializer` instance.

Architecture
------------

[](#architecture)

```
Store (contract) ─ Array | File | Apcu | Redis | Memcached | Pdo | Null | Tiered
Repository ─ rich API (remember/add/pull/increment/tags/lock/rememberLocked) over a Store
Psr16Cache / Psr6Pool ─ standards adapters
Lock + StoreLock ─ atomic mutex (single-flight)
Tags: TagSet + TaggedCache ─ group invalidation
Http: CacheControl, HttpCache (ETag/304), PageCache, CacheMiddleware (PSR-15)
Cache / CacheManager ─ factory + named stores

```

Testing
-------

[](#testing)

```
composer test
```

The suite runs one shared **contract** against every store. Redis/Memcached tests run when those servers are reachable and skip otherwise; raw-socket and extension paths are both covered.

License
-------

[](#license)

MIT.

###  Health Score

20

—

LowBetter than 13% of packages

Maintenance65

Regular maintenance activity

Popularity0

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity11

Early-stage or recently created project

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/20700f34ff813055154e843bbdbe04d33320c1e6a81bbb3301cad67cb8350fd3?d=identicon)[jarircse16](/maintainers/jarircse16)

### Embed Badge

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

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

###  Alternatives

[barryvdh/laravel-httpcache

HttpCache for Laravel

513404.4k10](/packages/barryvdh-laravel-httpcache)

PHPackages © 2026

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