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

ActiveLibrary[Caching](/categories/caching)

simplecomplex/cache
===================

File-based PSR-16 Simple Cache implementation; cache management; CLI interface.

1.3.3(6y ago)02802MITPHPPHP &gt;=7.0

Since Jul 12Pushed 6y ago2 watchersCompare

[ Source](https://github.com/simplecomplex/php-cache)[ Packagist](https://packagist.org/packages/simplecomplex/cache)[ Docs](https://github.com/simplecomplex/php-cache)[ RSS](/packages/simplecomplex-cache/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (2)Versions (11)Used By (2)

Cache
-----

[](#cache)

- [Installation](#installation)
- [Requirements](#requirements)

### Scope

[](#scope)

Caching of complex variables, and variables which are expensive to generate.
Like configuration, localization and service responses.

Not page caching, no [stampede protection](https://en.wikipedia.org/wiki/Cache_stampede).

### Cache abstraction

[](#cache-abstraction)

**`CacheBroker`** decouples code using cache from the actual PSR-16 cache implementation.

Defines three cache class aliases:

- *variable time-to-live* (default ttl and set() arg ttl)
- *fixed time-to-live* (default ttl, set() arg ttl ignored)
- *persistent* (default ttl 'forever' and set() arg ttl ignored)

Plus three like the above which allow long keys; length 128 instead of the PSR-16 compliant 64.

#### How to use

[](#how-to-use)

Ask `CacheBroker` for an aliased type of cache instance - do *not* instantiate a particular cache class.

Extend `CacheBroker`, if you later want to switch from say file-based to database-based caching.

#### Dependency injection container ID: cache-broker

[](#dependency-injection-container-id-cache-broker)

Recommendation: access (and thus instantiate) the cache broker via DI container ID 'cache-broker'.
See [SimpleComplex Utils](https://github.com/simplecomplex/php-utils) `Dependency`.

### File-based caching

[](#file-based-caching)

**`FileCache`** is a thorough and cautious PSR-16 Simple Cache implementation; file-based.
Coded defensively - key (and other argument) validation.

Addresses:

- default time-to-live; ignore set() arg ttl option; no time-to-live
- garbage collection
- clearing all items or expired items only
- exporting all items, to JSON
- building/replacing a cache store during production (using a 'candidate' store)
- CLI interface for clearing items, e.g. via cron
- concurrency issues (storage-wise only)

### Cache management, replacement and backup

[](#cache-management-replacement-and-backup)

Defines two extensions to the PSR-16 CacheInterface, implemented by `FileCache`.

**`ManageableCacheInterface`**

- is the cache store new or empty?
- setting default time-to-live; setting 'ignore' set() arg ttl
- clearing and exporting
- listing all cache stores

**`BackupCacheInterface`**

- backup/restore
- replacing a store, by building a 'candidate' and switching to that when it's complete

### Example

[](#example)

```
// Bootstrap.
Dependency::genericSet('cache-broker', function () {
    return new \SimpleComplex\Cache\CacheBroker();
});
// ...
// Use.
/** @var \Psr\Container\ContainerInterface $container */
$container = Dependency::container();
/** @var \SimpleComplex\Cache\CacheBroker $cache_broker */
$cache_broker = $container->get('cache-broker');
/**
 * Create or re-initialize a cache store.
 *
 * @var \SimpleComplex\Cache\FileCache $cache_store
 */
$cache_store = $cache_broker->getStore(
    'some-cache-store',
    CacheBroker::CACHE_VARIABLE_TTL
);
unset($cache_broker);
/** @var mixed $whatever */
$whatever = $cache_store->get('some-key', 'the default value');
```

### CLI commands

[](#cli-commands)

```
# List all cache commands and their help.
php cli.php cache -h
# One command's help.
php cli.php cache-xxx -h

# List existing cache stores.
php cli.php cache-list-stores

# Display/get value of a cache item.
php cli.php cache-get store key

# Delete a cache item.
php cli.php cache-delete store key

# Delete all expired items of one or all cache stores.
php cli.php cache-clear-expired

# Delete all items of one or all cache stores.
php cli.php cache-clear

# Backup a cache store.
php cli.php cache-backup store

# Restore a cache store from backup.
php cli.php cache-restore store

# Destroy one or all cache stores.
php cli.php cache-destroy
```

### Installation

[](#installation)

Create a 'private' files directory alongside the document root dir
and make it writable for the webserver user (www-data or apache).

Like:
`/var/www/my-host/`**`http`**
`/var/www/my-host/`**`private`**

On first cache store instantiation, **`FileCache`** will create directory
`private/`**`lib/simplecomplex/file-cache`**

If that directory structure isn't suitable, do either:

- supply **`CacheBroker`** (or **`FileCache`** constructor directly) with a 'path' argument
- extend **`FileCache`** and override it's class constant **`PATH_DEFAULT`**

### Requirements

[](#requirements)

- PHP &gt;=7.0
- 64-bit PHP
- [PSR-16 Simple Cache](https://github.com/php-fig/simple-cache)
- [SimpleComplex Utils](https://github.com/simplecomplex/php-utils)

##### Suggestions

[](#suggestions)

- [SimpleComplex Inspect](https://github.com/simplecomplex/inspect)

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity64

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

Recently: every ~106 days

Total

9

Last Release

2458d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cc4dc35e0d2b8e4619ae2fe2d083cb3c64b09127ecb318c3909025fc0aae1e7f?d=identicon)[jacobfriis](/maintainers/jacobfriis)

---

Top Contributors

[![jacobfriis](https://avatars.githubusercontent.com/u/3807905?v=4)](https://github.com/jacobfriis "jacobfriis (109 commits)")

---

Tags

cachephpphp7psrpsr-16phpsimple-cachepsr-16

### Embed Badge

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

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

###  Alternatives

[sabre/cache

Simple cache abstraction layer implementing PSR-16

541.2M3](/packages/sabre-cache)[voku/simple-cache

Simple Cache library

322.5M7](/packages/voku-simple-cache)

PHPackages © 2026

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