PHPackages                             parshikovpavel/array-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. [Testing &amp; Quality](/categories/testing)
4. /
5. parshikovpavel/array-cache

ActiveLibrary[Testing &amp; Quality](/categories/testing)

parshikovpavel/array-cache
==========================

Non-persistent PSR-6/PSR-16 compatible cache using an array. Useful for mocking the original cache in tests

0.0.1(6y ago)0131MITPHPPHP ^7.1CI failing

Since Oct 10Pushed 6y ago1 watchersCompare

[ Source](https://github.com/parshikovpavel/array-cache)[ Packagist](https://packagist.org/packages/parshikovpavel/array-cache)[ Docs](https://github.com/parshikovpavel/array-cache)[ RSS](/packages/parshikovpavel-array-cache/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (2)Used By (1)

The package for caching data in memory using a PHP array.

As cached data is only available in the current request the package should be used for mocking the original cache in tests.

The cache implementation is compatible with [PSR-6](https://www.php-fig.org/psr/psr-6/)/[PSR-16](https://www.php-fig.org/psr/psr-16/). In addition, `CounterInterface` ([excluded](https://github.com/php-fig/fig-standards/pull/847/) from PSR-16) is included in the library and used for implementation.

The table below shows the correspondence between the library classes and the implemented interfaces.

PSRClassImplements[PSR-6](https://www.php-fig.org/psr/psr-6/)`CacheItemPool``CacheItemPoolInterface`[PSR-6](https://www.php-fig.org/psr/psr-6/)`CacheItem``CacheItemInterface`[PSR-16](https://www.php-fig.org/psr/psr-16/)`Cache``CacheInterface`[PSR-16](https://www.php-fig.org/psr/psr-6/) + [excluded](https://github.com/php-fig/fig-standards/pull/847/) `CounterInterface``CountingCache``CacheInterface` + `CounterInterface`Installation
============

[](#installation)

The recommended method of installing is via Composer.

Run the following command from the project root:

```
composer require parshikovpavel/array-cache --dev
```

Usage
=====

[](#usage)

A detailed description of the interfaces implemented in the library can be found in [PSR-6](https://www.php-fig.org/psr/psr-6/) and [PSR-16](https://www.php-fig.org/psr/psr-16/).

Here are some common usage patterns of the library for testing your application using [PHPUnit](https://phpunit.de/).

PSR-6
-----

[](#psr-6)

According to PSR-6, the package provides `\ppCache\CacheItemPool` class (implementing the `\PSR\Cache\CacheItemPoolInterface` interface) and `\ppCache\CacheItem` class (implementing the `\PSR\Cache\CacheItemInterface` interface).

### Cache fixture

[](#cache-fixture)

Use `\ppCache\CacheItemPool` instance as a fixture. Put the creating of the cache fixture into the setUp() method.

```
final class CacheTest extends TestCase
{
    private $itemPool;

    protected function setUp(): void
    {
        $this->itemPool = new \ppCache\CacheItemPool();
    }

    /* ... */
}
```

### Dependency injection

[](#dependency-injection)

If your application based on the dependency inversion principle and the dependency injection technique, it's very easy to replace a real cache with a mock cache.

Consider the case of constructor injection. Let's assume `Client` class provide a parameter in a constructor to inject a cache instance.

```
final class CacheTest extends TestCase
{
    /* ... */

    public function testFeature(): void
    {
        $client = new Client($this->itemPool);

        /* ... */
    }
}
```

### Get value from cache

[](#get-value-from-cache)

The most common cache use case is a getting value and computing in case of cache miss.

A cache client must first try to get value from cache by `$key`.

- If trying is successful, return `$value`
- otherwise compute `$value` by calling the time-consuming `compute()` function and cache the value for a while

```
final class Client {

    private $itemPool;

    public function __construct(\Psr\Cache\CacheItemPoolInterface $itemPool)
    {
        $this->itemPool = $itemPool;
    }

    private function getValue(string $key, int $ttl = 3600)
    {
        $item = $this->itemPool->getItem($key);
        if (!$item->isHit()) {
            $value = $this->compute();
            $item->set($value);
            $item->expiresAfter($ttl);
            $this->itemPool->save($item);
        }
        return $item->get();
    }

    /* ... */
}
```

PSR-16
------

[](#psr-16)

According to PSR-16, the package provides `\ppCache\Cache` class (implementing the `\Psr\SimpleCache\CacheInterface` interface).

### Cache fixture

[](#cache-fixture-1)

Use `\ppCache\Cache` instance as a fixture. Put the creating of the cache fixture into the setUp() method.

```
final class CacheTest extends TestCase
{
    private $cache;

    protected function setUp(): void
    {
        $this->cache = new \ppCache\Cache();
    }

    /* ... */
}
```

### Dependency injection

[](#dependency-injection-1)

Similarly, inject a cache instance into the client constructor:

```
final class CacheTest extends TestCase
{
    /* ... */

    public function testFeature(): void
    {
        $client = new Client($this->cache);

        /* ... */
    }
}
```

### Get value from cache

[](#get-value-from-cache-1)

The algorithm is the same as the one for `\ppCache\CacheItemPool` but the implementation is a bit simpler.

```
final class Client
{
    private $cache;

    public function __construct(\Psr\SimpleCache\CacheInterface $cache)
    {
        $this->cache = $cache;
    }

    private function getValue(string $key, int $ttl = 3600)
    {
        if (null === ($value = $this->cache->get($key))) {
            $value = $this->compute();
            $this->cache->set($key, $value, $ttl);
        }

        return $value;
    }

    /* ... */
}
```

PSR-16 + CounterInterface
-------------------------

[](#psr-16--counterinterface)

The package provides the `\ppCache\CountingCache` class which implements both the `\Psr\SimpleCache\CacheInterface`(introduced PSR-16) and the `CounterInterface` ([excluded](https://github.com/php-fig/fig-standards/pull/847/) from PSR-16). `CounterInterface` definition is taken from [PGP-FIG repository](https://github.com/php-fig/fig-standards/pull/847/commits/30471f36bd642529ebbb728747b3a0defc3cfed5)and is in this package.

The `\ppCache\CountingCache` implementation decorates a `\ppCache\Cache` instance and supplements its implementation with atomic increment-decrement methods.

### Cache fixture

[](#cache-fixture-2)

```
final class CacheTest extends TestCase
{
    private $countingCache;

    protected function setUp(): void
    {
        $this->countingCache = new \ppCache\CountingCache();
    }

    /* ... */
}
```

### Dependency injection

[](#dependency-injection-2)

```
final class CacheTest extends TestCase
{
    /* ... */

    public function testFeature(): void
    {
        $client = new Client($this->countingCache);

        /* ... */
    }
}
```

### Value increment and decrement

[](#value-increment-and-decrement)

```
final class Client
{
    private $countingCache;

    public function __construct(\ppCache\CountingCache $countingCache)
    {
        $this->countingCache = $countingCache;
    }

    private function changeValue(string $key): void
    {
        /* ... */

        $newValue = $this->countingCache->increment($key);

        /* ... */

        $newValue = $this->countingCache->decrement($key);

        /* ... */
    }

    /* ... */
}
```

Unit testing
============

[](#unit-testing)

There are unit tests in the `./tests` directory. You can run all tests with the following command:

```
$ ./vendor/bin/phpunit tests/ --testdox

ppCache\CacheItemPool
 ✔ Throws exception for invalid key
 ✔ Detects missing item
 ✔ Saves detects retrieves an eternal item
 ✔ Detects an expired item

ppCache\Cache
 ✔ Throws exception for invalid key
 ✔ Detects missing item
 ✔ Saves detects retrieves an eternal item
 ✔ Detects an expired item
 ✔ Gets items with a specified expiration time
 ✔ Supports multiple functions
 ✔ Performs deletion and clearing

ppCache\CountingCache
 ✔ Increments a value
 ✔ Decrements a value
```

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

2403d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/27277505?v=4)[Pavel Parshikov](/maintainers/parshikovpavel)[@parshikovpavel](https://github.com/parshikovpavel)

---

Top Contributors

[![parshikovpavel](https://avatars.githubusercontent.com/u/27277505?v=4)](https://github.com/parshikovpavel "parshikovpavel (22 commits)")

---

Tags

mockingarraymockcachepsr-16psr6psr-6psr16

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[laminas/laminas-cache

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

1076.9M130](/packages/laminas-laminas-cache)[cache/integration-tests

Integration tests for PSR-6 and PSR-16 cache implementations

393.4M95](/packages/cache-integration-tests)[matthiasmullie/scrapbook

Scrapbook is a PHP cache library, with adapters for e.g. Memcached, Redis, Couchbase, APCu, SQL and additional capabilities (e.g. transactions, stampede protection) built on top.

3212.5M32](/packages/matthiasmullie-scrapbook)[cache/array-adapter

A PSR-6 cache implementation using a php array. This implementation supports tags

548.3M151](/packages/cache-array-adapter)[cache/simple-cache-bridge

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

423.1M27](/packages/cache-simple-cache-bridge)[codeigniter4/cache

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

1320.1k](/packages/codeigniter4-cache)

PHPackages © 2026

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