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

ActiveLibrary[Caching](/categories/caching)

phlak/stash
===========

Simple PHP caching library

8.0.0(10mo ago)826.7k↑37.5%3[1 issues](https://github.com/PHLAK/Stash/issues)[1 PRs](https://github.com/PHLAK/Stash/pulls)MITPHPPHP &gt;= 8.1CI passing

Since Apr 7Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/PHLAK/Stash)[ Packagist](https://packagist.org/packages/phlak/stash)[ GitHub Sponsors](https://github.com/sponsors/PHLAK)[ Fund](https://paypal.me/ChrisKankiewicz)[ RSS](/packages/phlak-stash/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (10)Dependencies (5)Versions (19)Used By (0)

 [![Stash](stash.png)](stash.png)

 Lightweight PHP caching library • Created by [Chris Kankiewicz](https://www.ChrisKankiewicz.com) ([@PHLAK.dev](https://bsky.app/profile/phlak.dev))

 [![Join our Community](https://camo.githubusercontent.com/073a08ec4c3c801a8e24c53184d95a6562d74582854cb46320bcd76ef48ea543/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4a6f696e5f7468652d436f6d6d756e6974792d3762313666662e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/PHLAK/Stash/discussions) [![Become a Sponsor](https://camo.githubusercontent.com/00da07edf5fbff7528a4743d85563603f9284f02680e0ab1e73652e680878548/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4265636f6d655f612d53706f6e736f722d6363343139352e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/users/PHLAK/sponsorship) [![One-time Donation](https://camo.githubusercontent.com/e9b5aa71ffdb17943c10c6d6b4a3132b66a938495331e488ecbdad1f3c078879/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4d616b655f612d446f6e6174696f6e2d3030366262362e7376673f7374796c653d666f722d7468652d6261646765)](https://paypal.me/ChrisKankiewicz)
 [![Latest Stable Version](https://camo.githubusercontent.com/76f979b5257143d6c2acb169477159957aac3f4420854149b60c4bd0101d04cb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f50484c414b2f53746173682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/PHLAK/Stash) [![Total Downloads](https://camo.githubusercontent.com/863ed890a920486f126f36a9f452ac88951d5efa58b245870662ede74e3e3d6a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f50484c414b2f53746173682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/PHLAK/Stash) [![License](https://camo.githubusercontent.com/3c70c8f65f103a0024c4e01d4d714538084dcc9dd9b2830ce734caf0d5e16731/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f50484c414b2f53746173682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/PHLAK/Stash) [![GitHub branch checks state](https://camo.githubusercontent.com/7b7c72f080652825543d715164c4c3fb8fed51820dff83ff5d40fe5c9f546792/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636865636b732d7374617475732f50484c414b2f53746173682f6d61737465723f7374796c653d666c61742d737175617265)](https://github.com/PHLAK/Stash/actions)

---

Introduction
------------

[](#introduction)

Stash is a lightweight PHP caching library supporting multiple, interchangeable caching back-ends and an expressive (Laravel inspired) API.

Supported caching back-ends:

- **File** - File-based caching. Stores cache items as files in a directory on disk.
- **Memcached** - High-performance, distributed memory object caching system
- **Redis** - In-memory data structure store.
- **APCu** - PHP's native APC User Cache.
- **Ephemeral** - A transient, in-memory array that only exists for the lifetime of the script.

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

[](#requirements)

- [PHP](https://php.net) &gt;= 8.1

Install with Composer
---------------------

[](#install-with-composer)

```
composer require phlak/stash
```

Initializing the Client
-----------------------

[](#initializing-the-client)

First, import Stash:

```
use PHLAK\Stash;
```

Then instantiate Stash for your back-end of choice with the named constructor:

```
$stash = Stash\Cache::file($config);
$stash = Stash\Cache::memcached($config);
$stash = Stash\Cache::redis($config);
$stash = Stash\Cache::apcu($config);
$stash = Stash\Cache::ephemeral();
```

The `$config` parameter accepts a driver-specific [closure](https://secure.php.net/manual/en/class.closure.php)for setting configuration options for your chosen driver. Refer to the specific documentation about each driver below for more info. Not all drivers require a configuration function.

---

#### File Cache

[](#file-cache)

The file cache configuration closure must call `$this->setCacheDir($path)` where `$path` is a path to a valid directory in which your cache files will be stored.

```
$stash = Stash\Cache::file(function (): void {
    $this->setCacheDir('path/to/cache');
});
```

#### Memcached

[](#memcached)

The Memcached configuration closure receives an instance of the [Memcached object](https://www.php.net/manual/en/class.memcached.php) as it's only parameter, you can use this parameter to connect and configure Memcached. At a minimum you must connect to one or more Memcached servers via the `addServer()` or `addServers()` methods.

Reference the [PHP Memcached documentation](https://secure.php.net/manual/en/book.memcached.php)for additional configuration options.

```
$stash = Stash\Cache::memcached(function (Memcached $memcached): void {
    $memcached->addServer('localhost', 11211);
    // $memcached->setOption(Memcached::OPT_PREFIX_KEY, 'some_prefix');
});
```

#### Redis

[](#redis)

The Redis configuration closure receives an instance of the [Redis object](https://github.com/phpredis/phpredis#class-redis) as it's only parameter, you can use this parameter to connect to and configure Redis. At a minimum you must connect to one or more Redis servers via the `connect()` or `pconnect()` methods.

Reference the [phpredis documentation](https://github.com/phpredis/phpredis#readme)for additional configuration options.

```
$stash = Stash\Cache::redis(function (Redis $redis): void {
    $redis->pconnect('localhost', 6379);
    // $redis->setOption(Redis::OPT_PREFIX, 'some_prefix');
});
```

#### APCu

[](#apcu)

The APCu driver caches items in PHPs APC user cache.

```
$stash = Stash\Cache::apcu();
```

The APCu driver does not require a configuration closure. However, if you wish to set a cache prefix you may pass a configuration closure that calls `$this->setPrefix($prefix)` where `$prefix` is a string of your desired prefix.

```
$stash = Stash\Cache::apcu(function (): void {
    $this->setPrefix('some_prefix');
});
```

#### Ephemeral

[](#ephemeral)

The Ephemeral driver caches items in a PHP array that exists in memory only for the lifetime of the script. The Ephemeral driver does not take a configuration closure.

```
$stash = Stash\Cache::ephemeral();
```

Usage
-----

[](#usage)

### `Cacheable::put( string $key , mixed $data [, $ttl = 0 ] ) : bool`

[](#cacheableput-string-key--mixed-data--ttl--0----bool)

Add an item to the cache for a specified duration.

##### Examples

[](#examples)

```
// Cache a value for 5 minutes
$stash->put('foo', 'some value', 300);

// Cache a value indefinitely
$stash->put('bar', false);
```

---

### `Cacheable::forever( string $key , mixed $data) : bool`

[](#cacheableforever-string-key--mixed-data--bool)

Add an item to the cache permanently.

##### Examples

[](#examples-1)

```
$stash->forever('foo', 'some value');
```

---

### `Cacheable::get( string $key [, $default = false ] ) : mixed`

[](#cacheableget-string-key--default--false----mixed)

Retrieve an item from the cache.

##### Examples

[](#examples-2)

```
$stash->get('foo');

// Return 'default' if 'bar' doesn't exist
$stash->get('bar', 'default');
```

---

### `Cacheable::has( string $key ) : bool`

[](#cacheablehas-string-key---bool)

Check if an item exists in the cache.

##### Examples

[](#examples-3)

```
$stash->has('foo');
```

---

### `Cacheable::remember( string $key , int $ttl , Closure $closure ) : mixed`

[](#cacheableremember-string-key--int-ttl--closure-closure---mixed)

Retrieve item from cache or, when item does not exist, execute a closure. The result of the closure is then stored in the cache for the specified duration and returned for immediate use.

##### Examples

[](#examples-4)

```
$stash->remember('foo', 60, function() {
    return new FooClass();
});
```

---

### `Cacheable::rememberForever( string $key , Closure $closure ) : mixed`

[](#cacheablerememberforever-string-key--closure-closure---mixed)

Retrieve item from cache or, when item does not exist, execute a closure. The result of the closure is then stored in the cache permanently.

##### Examples

[](#examples-5)

```
$stash->rememberForever('pokemon', function() {
    return new Pokemon($name, $description);
});
```

---

### `Cacheable::increment( string $key [, int $value = 1 ] ) : mixed`

[](#cacheableincrement-string-key--int-value--1----mixed)

Increment an integer already stored in the cache.

##### Examples

[](#examples-6)

```
// Increment by 1
$stash->increment('foo');

// Increment by 10
$stash->increment('bar', 10);
```

---

### `Cacheable::decrement( string $key [, int $value = 1 ] ) : mixed`

[](#cacheabledecrement-string-key--int-value--1----mixed)

Decrement an integer already stored in the cache.

##### Examples

[](#examples-7)

```
 // Decrements by 1
$stash->decrement('foo');

 // Decrements by 10
$stash->decrement('bar', 10);
```

---

### `Cacheable::touch( string|array $key [, int $ttl = 0 ] ) : bool`

[](#cacheabletouch-stringarray-key--int-ttl--0----bool)

Extend the expiration time for an item in the cache.

##### Examples

[](#examples-8)

```
 // Extend the expiration by 5 minutes
$stash->touch('foo', 300);

 // Extend the expiration indefinitely
$stash->touch('bar');

// Extend the expiration of multiple items by 5 minutes
$stash->touch(['foo', 'bar', 'baz'], 300);
```

---

### `Cacheable::forget( string $key ) : bool`

[](#cacheableforget-string-key---bool)

Remove an item from the cache.

##### Examples

[](#examples-9)

```
$stash->forget('foo');
```

---

### `Cacheable::flush() : bool`

[](#cacheableflush--bool)

Delete all items from the cache.

##### Examples

[](#examples-10)

```
$stash->flush();
```

TTL Helper
----------

[](#ttl-helper)

For convenience, Stash provides a `TTL` helper class to convert units of time to seconds. This is useful for setting the `$ttl` parameter in various methods.

##### Examples

[](#examples-11)

```
use PHLAK\Stash\TTL;

// Cache a value for 5 minutes
$stash->put('foo', 'some value', TTL::minutes(5));

// Remember an item for one day
$stash->remember('bar', TTL::days(1), fn (): string => 'Some value');

// Update the expiration time of an item to 3 hour
$stash->touch('baz', TTL::hours(3));
```

Changelog
---------

[](#changelog)

A list of changes can be found on the [GitHub Releases](https://github.com/PHLAK/Stash/releases) page.

Troubleshooting
---------------

[](#troubleshooting)

For general help and support join our [GitHub Discussions](https://github.com/PHLAK/Stash/discussions) or reach out on [Bluesky](https://bsky.app/profile/phlak.dev).

Please report bugs to the [GitHub Issue Tracker](https://github.com/PHLAK/Stash/issues).

Copyright
---------

[](#copyright)

This project is licensed under the [MIT License](https://github.com/PHLAK/Stash/blob/master/LICENSE).

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance63

Regular maintenance activity

Popularity35

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity80

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 96.4% 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 ~212 days

Recently: every ~681 days

Total

17

Last Release

319d ago

Major Versions

3.0.1 → 4.0.02017-09-10

4.1.0 → 5.0.02017-12-16

5.1.0 → 6.0.02018-04-03

6.0.1 → 7.0.02020-02-08

7.0.0 → 8.0.02025-07-25

PHP version history (5 changes)1.0.0PHP &gt;=5.4

4.0.0PHP &gt;=5.6

6.0.0PHP &gt;=7.0

7.0.0PHP &gt;=7.1

8.0.0PHP &gt;= 8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/53531?v=4)[Chris Kankiewicz](/maintainers/PHLAK)[@PHLAK](https://github.com/PHLAK)

---

Top Contributors

[![PHLAK](https://avatars.githubusercontent.com/u/53531?v=4)](https://github.com/PHLAK "PHLAK (240 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (4 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (3 commits)")[![dependabot-support](https://avatars.githubusercontent.com/u/112581971?v=4)](https://github.com/dependabot-support "dependabot-support (1 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

---

Tags

apcucachingfile-cachememcachedphpredis

###  Code Quality

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k8.7M64](/packages/spatie-laravel-responsecache)[statamic/cms

The Statamic CMS Core Package

4.8k3.5M901](/packages/statamic-cms)[awssat/laravel-visits

Laravel Redis visits counter for Eloquent models

975169.8k2](/packages/awssat-laravel-visits)[artisansdk/ratelimiter

A leaky bucket rate limiter and corresponding middleware with route-level granularity compatible with Laravel.

1513.6M1](/packages/artisansdk-ratelimiter)[imdhemy/google-play-billing

Google Play Billing

491.4M5](/packages/imdhemy-google-play-billing)[bitrix24/b24phpsdk

An official PHP library for the Bitrix24 REST API

10139.4k5](/packages/bitrix24-b24phpsdk)

PHPackages © 2026

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