PHPackages                             decodelabs/stash - 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. decodelabs/stash

ActiveLibrary[Caching](/categories/caching)

decodelabs/stash
================

PSR6 / PSR16 cache handler

v0.8.0(9mo ago)06.3k4MITPHPPHP ^8.4CI passing

Since Oct 18Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/decodelabs/stash)[ Packagist](https://packagist.org/packages/decodelabs/stash)[ RSS](/packages/decodelabs-stash/feed)WikiDiscussions develop Synced today

READMEChangelog (10)Dependencies (14)Versions (33)Used By (4)

Stash
=====

[](#stash)

[![PHP from Packagist](https://camo.githubusercontent.com/d281a6632e3b1e1f1168f3ba0996b4b2dd8f8bc910620c80c567663cdc83994f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6465636f64656c6162732f73746173683f7374796c653d666c6174)](https://packagist.org/packages/decodelabs/stash)[![Latest Version](https://camo.githubusercontent.com/ba7dba138d68f12121e88ed26efd1ba5678bd815a2661fc719d145a6eead871b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6465636f64656c6162732f73746173682e7376673f7374796c653d666c6174)](https://packagist.org/packages/decodelabs/stash)[![Total Downloads](https://camo.githubusercontent.com/7a52073ad15b25052ae67e2d908f11b498127fe3ae6a5ae824e2459f9b0cf00f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6465636f64656c6162732f73746173682e7376673f7374796c653d666c6174)](https://packagist.org/packages/decodelabs/stash)[![GitHub Workflow Status](https://camo.githubusercontent.com/250ef3f39227121b034e65d33864ecaf189a8fe689f1bc66ba2df5ce65d12f7b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6465636f64656c6162732f73746173682f696e746567726174652e796d6c3f6272616e63683d646576656c6f70)](https://github.com/decodelabs/stash/actions/workflows/integrate.yml)[![PHPStan](https://camo.githubusercontent.com/e25c14ce011edabdd0fbd2e10415b41cc5d66ed11ef3e5b7edd074c5bdd35a2d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d656e61626c65642d3434434331312e7376673f6c6f6e6743616368653d74727565267374796c653d666c6174)](https://github.com/phpstan/phpstan)[![License](https://camo.githubusercontent.com/86ee8e9e5c4a319012db110806d0519faf09199c52b4ff40afbb6be5ed4c6b4c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6465636f64656c6162732f73746173683f7374796c653d666c6174)](https://packagist.org/packages/decodelabs/stash)

### Cache storage system

[](#cache-storage-system)

Stash provides a PSR6 / PSR16 compatible cache system for PHP.

---

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

[](#installation)

This package requires PHP 8.4 or higher.

Install via Composer:

```
composer require decodelabs/stash
```

Usage
-----

[](#usage)

Store and access data in a standardised volatile cache either via the PSR6 or PSR16 interface mechanisms. Caches are namespaced to allow for clean separation of data between usage domains.

```
use DecodeLabs\Stash;

$stash = new Stash();
$myCache = $stash->load('MyCache');

if(!$myCache->has('myValue')) {
    $myCache->set('myValue', [1, 2, 3]);
}

$total = 0;

foreach($myCache->get('myValue', []) as $number) {
    $total += $number;
}

$myCache->delete('myValue');
```

### Fetch

[](#fetch)

Use the fetch method to ensure a cache value is in place in one call:

```
$myValue = $myCache->fetch('myValue', function() {
    return [1, 2, 3]; // Only called if key not found in cache
});
```

### Array Access

[](#array-access)

Array access methods provide quick offset access to cache data:

```
if(!isset($myCache['myValue'])) {
    $myCache['myValue'] = 'Hello world';
}

echo $myCache['myValue'];
unset($MyCache['myValue']);
```

### Object access

[](#object-access)

Object access works the same way as ArrayAccess, but with the PSR6 Cache Item object as the return:

```
$item = $myCache->myValue;

if(!$item->isHit()) {
    $item->set('Hello world');
}

echo $item->get();
$item->delete();
```

Drivers
-------

[](#drivers)

The following drivers are available out of the box:

- Memcache
- Redis
- Predis (native PHP redis client)
- APCu
- File (serialized data)
- PhpFile (var\_export data)
- PhpArray (in memory)
- Blackhole (nothing stored)

Stash uses [Archetype](https://github.com/decodelabs/archetype) to load driver classes so additional drivers may be provided by implementing your own `Resolver`.

If no configuration is provided, Stash will attempt to use the best-fit driver for your environment, starting with Memcache, through Redis and APCu, and falling back on the File store.

Configuration
-------------

[](#configuration)

Stash can be configured by passing a `DriverManager` to the constructor (or in your DI container):

```
use DecodeLabs\Stash\DriverManager;

$stash = new Stash(new DriverManager(
    new RedisConfig('localRedis'),
    new RedisConfig('anotherRedis', host: '123.456.789.000', port: 6379),
    new MemcacheConfig('remoteMemcache', host: '123.456.789.000', port: 11211),
    new NamespaceConfig('MyStore', driver: 'remoteMemcache'),
    new FileStoreConfig('specialFileStore', path: '/tmp/stash/fileStore'),
));
```

You may pass any number of `DriverConfig`, `NamespaceConfig` and `FileStoreConfig` objects to the `DriverManager` to configure the drivers and stores - the `DriverManager` will use the most relevant driver for each namespace.

If you're using `Kingdom` to manage your application, it is advisable to provide a `DriverManager` to your container and specify the drivers you want to use during initialization.

```
use DecodeLabs\Fabric\Kingdom as FabricKingdom;
use DecodeLabs\Kingdom\ContainerAdapter;
use DecodeLabs\Stash\DriverManager;
use DecodeLabs\Stash\DriverConfig\Redis as RedisConfig;

class Kingdom extends FabricKingdom
{
    public function initialize(): void
    {
        parent::initialize();

        $this->container->setFactory(
            DriverManager::class,
            fn () => new DriverManager(
                new RedisConfig('localRedis')
            )
        );
    }
}
```

Custom Store methods
--------------------

[](#custom-store-methods)

By default, newly loaded caches use a generic Store implementation, however if you require custom methods for domain-oriented data access, you can implement your own Store classes using a custom [Archetype](https://github.com/decodelabs/archetype) `Resolver`.

```
namespace MyApp;

use DecodeLabs\Stash\Store;
use DecodeLabs\Stash\Store\Generic;

class MyCache extends Generic
{

    public function getMyData(): string
    {
        return $this->fetch('myData', function() {
            return 'Hello world';
        });
    }
}

$archetype->map(Store::class, namespace::class);

$myCache = $stash->load('MyCache'); // Will now use MyApp\MyCache
```

Licensing
---------

[](#licensing)

Stash is licensed under the MIT License. See [LICENSE](./LICENSE) for the full license text.

###  Health Score

47

—

FairBetter than 93% of packages

Maintenance75

Regular maintenance activity

Popularity21

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity65

Established project with proven stability

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

Recently: every ~15 days

Total

31

Last Release

289d ago

PHP version history (3 changes)v0.1.0PHP ^8.0

v0.2.0PHP ^8.1

v0.6.0PHP ^8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/8a241d64d12b3b5ee94197862ec1ec30b82ed2efa34a0cd7f4c3565a021daddd?d=identicon)[betterthanclay](/maintainers/betterthanclay)

---

Top Contributors

[![betterthanclay](https://avatars.githubusercontent.com/u/1273586?v=4)](https://github.com/betterthanclay "betterthanclay (166 commits)")

---

Tags

apcucachememcachedphppsr-16psr-6rediscache

### Embed Badge

![Health badge](/badges/decodelabs-stash/health.svg)

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

###  Alternatives

[laravel/framework

The Laravel Framework.

34.8k543.8M20.0k](/packages/laravel-framework)[symfony/cache

Provides extended PSR-6, PSR-16 (and tags) implementations

4.2k373.5M3.3k](/packages/symfony-cache)[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k9.0M69](/packages/spatie-laravel-responsecache)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

585.6M571](/packages/shopware-core)[laminas/laminas-cache

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

1077.3M154](/packages/laminas-laminas-cache)[infocyph/intermix

A lightweight PHP DI container, invoker, serializer, and utility toolkit.

137.7k2](/packages/infocyph-intermix)

PHPackages © 2026

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