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

ActiveLibrary[Caching](/categories/caching)

tobento/service-cache
=====================

Providing PSR-6 and PSR-16 caches for PHP applications.

2.0(5mo ago)099MITPHPPHP &gt;=8.4

Since Jun 29Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/tobento-ch/service-cache)[ Packagist](https://packagist.org/packages/tobento/service-cache)[ Docs](https://www.tobento.ch)[ RSS](/packages/tobento-service-cache/feed)WikiDiscussions 2.x Synced today

READMEChangelog (3)Dependencies (11)Versions (5)Used By (0)

Cache Service
=============

[](#cache-service)

Providing [PSR-6](https://www.php-fig.org/psr/psr-6/) and [PSR-16](https://www.php-fig.org/psr/psr-16/) caches for PHP applications.

Table of Contents
-----------------

[](#table-of-contents)

- [Getting started](#getting-started)
    - [Requirements](#requirements)
    - [Highlights](#highlights)
- [Documentation](#documentation)
    - [PSR 6 Cache](#psr-6-cache)
        - [Available Cache Item Pools](#available-cache-item-pools)
            - [File Storage Cache Item Pool](#file-storage-cache-item-pool)
            - [Storage Cache Item Pool](#storage-cache-item-pool)
            - [Array Cache Item Pool](#array-cache-item-pool)
            - [Psr16 Cache Item Pool](#psr16-cache-item-pool)
        - [Cache Item Pools](#cache-item-pools)
        - [Interfaces](#interfaces)
            - [Cache Item Pool Factory Interface](#cache-item-pool-factory-interface)
            - [Cache Item Pools Interface](#cache-item-pools-interface)
    - [PSR 16 Simple Cache](#psr-16-simple-cache)
        - [Available Caches](#available-caches)
            - [Psr6 Cache](#psr6-cache)
        - [Caches](#caches)
        - [Interfaces Simple](#interfaces-simple)
            - [Cache Factory Interface](#cache-factory-interface)
            - [Caches Interface](#caches-interface)
    - [Shared Interfaces](#shared-interfaces)
        - [Can Delete Expired Items](#can-delete-expired-items)
- [Credits](#credits)

---

Getting started
===============

[](#getting-started)

Add the latest version of the cache service project running this command.

```
composer require tobento/service-cache

```

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

[](#requirements)

- PHP 8.4 or greater

Highlights
----------

[](#highlights)

- Framework-agnostic, will work with any project
- Decoupled design

Documentation
=============

[](#documentation)

PSR 6 Cache
-----------

[](#psr-6-cache)

### Available Cache Item Pools

[](#available-cache-item-pools)

#### File Storage Cache Item Pool

[](#file-storage-cache-item-pool)

The file storage cache item pool using the [File Storage Service](https://github.com/tobento-ch/service-file-storage) to store the items.

First, you will need to install `service-file-storage`:

```
composer require tobento/service-file-storage

```

Then, create the [File Storage](https://github.com/tobento-ch/service-file-storage#flysystem-storage) you wish to use for pool:

```
use Tobento\Service\Cache\FileStorageCacheItemPool;
use Tobento\Service\Cache\CanDeleteExpiredItems;
use Tobento\Service\Clock\SystemClock;
use Tobento\Service\FileStorage\StorageInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Clock\ClockInterface;

$pool = new FileStorageCacheItemPool(

    // Any storage where to store cache items:
    storage: $storage, // StorageInterface

    // A path used as the path prefix for the storage:
    path: 'cache', // string

    // The clock used for calculating expiration:
    clock: new SystemClock(), // ClockInterface

    // The default Time To Live in seconds, null forever:
    ttl: null, // null|int
);

var_dump($pool instanceof CacheItemPoolInterface);
// bool(true)

var_dump($pool instanceof CanDeleteExpiredItems);
// bool(true)
```

Check out the [Can Delete Expired Items](#can-delete-expired-items) interface to learn more about it.

#### Storage Cache Item Pool

[](#storage-cache-item-pool)

The storage cache item pool using the [Storage Service](https://github.com/tobento-ch/service-storage) to store the items.

First, you will need to install `service-storage`:

```
composer require tobento/service-storage

```

Then, create the [Storage](https://github.com/tobento-ch/service-storage#storages) you wish to use for pool:

```
use Tobento\Service\Cache\StorageCacheItemPool;
use Tobento\Service\Cache\CanDeleteExpiredItems;
use Tobento\Service\Clock\SystemClock;
use Tobento\Service\Storage\StorageInterface;
use Tobento\Service\Storage\JsonFileStorage;
use Tobento\Service\Storage\Tables\Tables;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Clock\ClockInterface;

// Create the storage:
$tables = new Tables();
$tables->add('cache_items', ['id', 'data', 'expiration', 'namespace'], 'id');

$storage = new JsonFileStorage(
    dir: __DIR__.'/tmp/',
    tables: $tables
);

$pool = new StorageCacheItemPool(

    // Any storage where to store cache items:
    storage: $storage, // StorageInterface

    // A namespace used as prefix for cache item keys:
    namespace: 'default', // string

    // The clock used for calculating expiration:
    clock: new SystemClock(), // ClockInterface

    // The default Time To Live in seconds, null forever:
    ttl: null, // null|int

    // Specify the table name:
    table: 'cache_items', // string
);

var_dump($pool instanceof CacheItemPoolInterface);
// bool(true)

var_dump($pool instanceof CanDeleteExpiredItems);
// bool(true)
```

Check out the [Can Delete Expired Items](#can-delete-expired-items) interface to learn more about it.

**Recommended table column types for (database) storage**

ColumnTypeidVARCHAR(255) NOT NULL PRIMARY KEYdataMEDIUMBLOB NOT NULLexpirationTIMESTAMPnamespaceVARCHAR(100) NOT NULL#### Array Cache Item Pool

[](#array-cache-item-pool)

The cache items will be stored in memory and not persisted outside the running PHP process in any way. Might be useful for testing purposes.

```
use Tobento\Service\Cache\ArrayCacheItemPool;
use Tobento\Service\Clock\SystemClock;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Clock\ClockInterface;

$pool = new ArrayCacheItemPool(

    // The clock used for calculating expiration:
    clock: new SystemClock(), // ClockInterface

    // The default Time To Live in seconds, null forever:
    ttl: null, // null|int
);

var_dump($pool instanceof CacheItemPoolInterface);
// bool(true)
```

#### Psr16 Cache Item Pool

[](#psr16-cache-item-pool)

The Psr16 cache item pool using the defined Psr16 cache to store items.

```
use Tobento\Service\Cache\Psr16CacheItemPool;
use Tobento\Service\Cache\CanDeleteExpiredItems;
use Tobento\Service\Clock\SystemClock;
use Psr\SimpleCache\CacheInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Clock\ClockInterface;

$pool = new Psr16CacheItemPool(

    cache: $cache, // CacheInterface

    // A namespace used as prefix for cache item keys:
    namespace: 'default', // string

    // The clock used for calculating expiration:
    clock: new SystemClock(), // ClockInterface

    // The default Time To Live in seconds, null forever:
    ttl: null, // null|int
);

var_dump($pool instanceof CacheItemPoolInterface);
// bool(true)

var_dump($pool instanceof CanDeleteExpiredItems);
// bool(true)
```

### Cache Item Pools

[](#cache-item-pools)

#### Create Pools

[](#create-pools)

```
use Tobento\Service\Cache\CacheItemPools;
use Tobento\Service\Cache\CacheItemPoolsInterface;

$pools = new CacheItemPools();

var_dump($pools instanceof CacheItemPoolsInterface);
// bool(true)
```

#### Add Pools

[](#add-pools)

**add**

```
use Psr\Cache\CacheItemPoolInterface;

$pools->add(
    name: 'primary',
    pool: $pool, // CacheItemPoolInterface
);
```

**register**

You may use the register method to only create the pool if requested.

```
use Psr\Cache\CacheItemPoolInterface;

$pools->register(
    name: 'name',
    pool: function(string $name): CacheItemPoolInterface {
        // create the pool:
        return $pool;
    },
);
```

#### Get Pool

[](#get-pool)

If the pool does not exist or could not get created it throws a `CacheException::class`.

```
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\CacheException;

$pool = $pools->get(name: 'file');

var_dump($pool instanceof CacheItemPoolInterface);
// bool(true)

$pools->get(name: 'unknown');
// throws CacheException
```

You may use the `has` method to check if a pool exists.

```
var_dump($pools->has('name'));
// bool(false)
```

You may use the `getNames` method to get all pool names.

```
var_dump($pools->getNames());
// array(1) { [0]=> string(4) "file" }
```

#### Default Pools

[](#default-pools)

You may add default pools for your application design.

```
use Tobento\Service\Cache\CacheItemPools;
use Tobento\Service\Cache\CacheItemPoolsInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\CacheException;

$pools = new CacheItemPools();

// add "file" pool:
$pools->add(name: 'file', pool: $pool);

// add default:
$pools->addDefault(name: 'primary', pool: 'file');

// get default pool for the specified name.
$primaryPool = $pools->default('primary');

var_dump($primaryPool instanceof CacheItemPoolInterface);
// bool(true)

var_dump($pools->hasDefault('primary'));
// bool(true)

var_dump($pools->getDefaults());
// { ["primary"]=> string(4) "file" }

$pools->default('unknown');
// throws CacheException
```

### Interfaces

[](#interfaces)

#### Cache Item Pool Factory Interface

[](#cache-item-pool-factory-interface)

You may use this interface for creating pools.

```
namespace Tobento\Service\Cache;

use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\CacheException;

interface CacheItemPoolFactoryInterface
{
    /**
     * Create a new CacheItemPool based on the configuration.
     *
     * @param string $name
     * @param array $config
     * @return CacheItemPoolInterface
     * @throws CacheException
     */
    public function createCacheItemPool(string $name, array $config = []): CacheItemPoolInterface;
}
```

#### Cache Item Pools Interface

[](#cache-item-pools-interface)

```
namespace Tobento\Service\Cache;

use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\CacheException;

interface CacheItemPoolsInterface
{
    /**
     * Add a pool.
     *
     * @param string $name The pool name.
     * @param CacheItemPoolInterface $pool
     * @return static $this
     */
    public function add(string $name, CacheItemPoolInterface $pool): static;

    /**
     * Register a pool.
     *
     * @param string $name The pool name.
     * @param callable $pool
     * @return static $this
     */
    public function register(string $name, callable $pool): static;

    /**
     * Returns the pool by name.
     *
     * @param string $name The pool name
     * @return CacheItemPoolInterface
     * @throws CacheException
     */
    public function get(string $name): CacheItemPoolInterface;

    /**
     * Returns true if the pool exists, otherwise false.
     *
     * @param string $name The pool name.
     * @return bool
     */
    public function has(string $name): bool;

    /**
     * Adds a default name for the specified pool.
     *
     * @param string $name The default name.
     * @param string $pool The pool name.
     * @return static $this
     */
    public function addDefault(string $name, string $pool): static;

    /**
     * Returns the default pools.
     *
     * @return array ['general' => 'files']
     */
    public function getDefaults(): array;

    /**
     * Returns the pool for the specified default name.
     *
     * @param string $name The type such as pdo.
     * @return CacheItemPoolInterface
     * @throws CacheException
     */
    public function default(string $name): CacheItemPoolInterface;

    /**
     * Returns true if the default pool exists, otherwise false.
     *
     * @param string $name The default name.
     * @return bool
     */
    public function hasDefault(string $name): bool;

    /**
     * Returns the names.
     *
     * @return array
     */
    public function getNames(): array;
}
```

PSR 16 Simple Cache
-------------------

[](#psr-16-simple-cache)

### Available Caches

[](#available-caches)

#### Psr6 Cache

[](#psr6-cache)

The Psr6 cache using the defined Psr6 pool to store items.

```
use Tobento\Service\Cache\Simple\Psr6Cache;
use Tobento\Service\Cache\ArrayCacheItemPool;
use Tobento\Service\Cache\CanDeleteExpiredItems;
use Tobento\Service\Clock\SystemClock;
use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface;
use Psr\Clock\ClockInterface;

$pool = new ArrayCacheItemPool(
    clock: new SystemClock(),
);

$cache = new Psr6Cache(

    pool: $pool, // CacheItemPoolInterface

    // A namespace used as prefix for cache item keys:
    namespace: 'default', // string

    // The default Time To Live in seconds, null forever:
    ttl: null, // null|int
);

var_dump($cache instanceof CacheInterface);
// bool(true)

var_dump($cache instanceof CanDeleteExpiredItems);
// bool(true)
```

Check out the [Can Delete Expired Items](#can-delete-expired-items) interface to learn more about it.

### Caches

[](#caches)

#### Create Caches

[](#create-caches)

```
use Tobento\Service\Cache\Simple\Caches;
use Tobento\Service\Cache\Simple\CachesInterface;

$caches = new Caches();

var_dump($caches instanceof CachesInterface);
// bool(true)
```

#### Add Caches

[](#add-caches)

**add**

```
use Psr\SimpleCache\CacheInterface;

$caches->add(
    name: 'primary',
    cache: $cache, // CacheInterface
);
```

**register**

You may use the register method to only create the cache if requested.

```
use Psr\SimpleCache\CacheInterface;

$caches->register(
    name: 'name',
    cache: function(string $name): CacheInterface {
        // create the cache:
        return $cache;
    },
);
```

#### Get Cache

[](#get-cache)

If the cache does not exist or could not get created it throws a `CacheException::class`.

```
use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\CacheException;

$cache = $caches->get(name: 'file');

var_dump($cache instanceof CacheInterface);
// bool(true)

$caches->get(name: 'unknown');
// throws CacheException
```

You may use the `has` method to check if a cache exists.

```
var_dump($caches->has('name'));
// bool(false)
```

You may use the `getNames` method to get all cache names.

```
var_dump($caches->getNames());
// array(1) { [0]=> string(4) "file" }
```

#### Default Caches

[](#default-caches)

You may add default caches for your application design.

```
$caches = new Caches();

// add "file" cache:
$caches->add(name: 'file', cache: $cache);

// add default:
$caches->addDefault(name: 'primary', cache: 'file');

// get default cache for the specified name.
$primaryPool = $caches->default('primary');

var_dump($primaryPool instanceof CacheInterface);
// bool(true)

var_dump($caches->hasDefault('primary'));
// bool(true)

var_dump($caches->getDefaults());
// { ["primary"]=> string(4) "file" }

$caches->default('unknown');
// throws CacheException
```

### Interfaces Simple

[](#interfaces-simple)

#### Cache Factory Interface

[](#cache-factory-interface)

You may use this interface for creating caches.

```
namespace Tobento\Service\Cache\Simple;

use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\CacheException;

interface CacheFactoryInterface
{
    /**
     * Create a new Cache based on the configuration.
     *
     * @param string $name
     * @param array $config
     * @return CacheInterface
     * @throws CacheException
     */
    public function createCache(string $name, array $config = []): CacheInterface;
}
```

#### Caches Interface

[](#caches-interface)

```
namespace Tobento\Service\Cache\Simple;

use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\CacheException;

interface CachesInterface
{
    /**
     * Add a cache.
     *
     * @param string $name The cache name.
     * @param CacheInterface $cache
     * @return static $this
     */
    public function add(string $name, CacheInterface $cache): static;

    /**
     * Register a cache.
     *
     * @param string $name The cache name.
     * @param callable $cache
     * @return static $this
     */
    public function register(string $name, callable $cache): static;

    /**
     * Returns the cache by name.
     *
     * @param string $name The cache name
     * @return CacheInterface
     * @throws CacheException
     */
    public function get(string $name): CacheInterface;

    /**
     * Returns true if the cache exists, otherwise false.
     *
     * @param string $name The cache name.
     * @return bool
     */
    public function has(string $name): bool;

    /**
     * Adds a default name for the specified cache.
     *
     * @param string $name The default name.
     * @param string $cache The cache name.
     * @return static $this
     */
    public function addDefault(string $name, string $cache): static;

    /**
     * Returns the default caches.
     *
     * @return array ['general' => 'files']
     */
    public function getDefaults(): array;

    /**
     * Returns the cache for the specified default name.
     *
     * @param string $name The type such as pdo.
     * @return CacheInterface
     * @throws CacheException
     */
    public function default(string $name): CacheInterface;

    /**
     * Returns true if the default cache exists, otherwise false.
     *
     * @param string $name The default name.
     * @return bool
     */
    public function hasDefault(string $name): bool;

    /**
     * Returns the names.
     *
     * @return array
     */
    public function getNames(): array;
}
```

### Shared Interfaces

[](#shared-interfaces)

#### Can Delete Expired Items

[](#can-delete-expired-items)

```
namespace Tobento\Service\Cache;

interface CanDeleteExpiredItems
{
    /**
     * Removes all expired items from the pool or cache.
     *
     * @return bool
     *   True if the items were successfully removed. False if there was an error.
     */
    public function deleteExpiredItems(): bool;
}
```

Credits
=======

[](#credits)

- [Tobias Strub](https://www.tobento.ch)
- [All Contributors](../../contributors)

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance70

Regular maintenance activity

Popularity11

Limited adoption so far

Community7

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

Total

5

Last Release

177d ago

Major Versions

1.x-dev → 2.02025-09-26

PHP version history (2 changes)1.0.0PHP &gt;=8.0

2.0PHP &gt;=8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/055d6a1b5c2384bb179c75ab0b55914231d898fdc4dffeb30770f81200e52206?d=identicon)[TOBENTOch](/maintainers/TOBENTOch)

---

Top Contributors

[![tobento-ch](https://avatars.githubusercontent.com/u/16684832?v=4)](https://github.com/tobento-ch "tobento-ch (7 commits)")

---

Tags

phppackagecachepsr-16psr-6tobento

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[laminas/laminas-cache

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

1066.7M108](/packages/laminas-laminas-cache)[cache/simple-cache-bridge

A PSR-6 bridge to PSR-16. This will make any PSR-6 cache compatible with SimpleCache.

433.1M24](/packages/cache-simple-cache-bridge)[codeigniter4/cache

PSR-6 and PSR-16 Cache Adapters for CodeIgniter 4

1319.5k](/packages/codeigniter4-cache)

PHPackages © 2026

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