PHPackages                             thadafinser/psr6-null-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. thadafinser/psr6-null-cache

ActiveLibrary

thadafinser/psr6-null-cache
===========================

PSR-6 cache NullObject implementation, to avoid null checks and for testing

v0.1.0(10y ago)25.0k4MITPHPPHP ~7.0

Since Feb 26Pushed 10y ago1 watchersCompare

[ Source](https://github.com/ThaDafinser/psr6-null-cache)[ Packagist](https://packagist.org/packages/thadafinser/psr6-null-cache)[ RSS](/packages/thadafinser-psr6-null-cache/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (3)Used By (0)

PSR-6 NullObject cache
======================

[](#psr-6-nullobject-cache)

[![Build Status](https://camo.githubusercontent.com/14e5fe2028ce8b942fdb9e1ac24371b24b10fb56e233dce804d4a5a341eaf7d8/68747470733a2f2f7472617669732d63692e6f72672f546861446166696e7365722f707372362d6e756c6c2d63616368652e737667)](https://travis-ci.org/ThaDafinser/psr6-null-cache)[![Code Coverage](https://camo.githubusercontent.com/b4b38a252fc545e4e43ca6a5148c796e071e618f9fc8d6da51ca16a180f8d5e6/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f546861446166696e7365722f707372362d6e756c6c2d63616368652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/ThaDafinser/psr6-null-cache/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/8246152e55cdfc31c786ed8d334301f47bfa21a6ea43b965bdedea515d709eb5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f546861446166696e7365722f707372362d6e756c6c2d63616368652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/ThaDafinser/psr6-null-cache/?branch=master)[![PHP 7 ready](https://camo.githubusercontent.com/e7515743b2f20828d426073516fc5feca527431b6058940041740d70b2359456/687474703a2f2f7068703772656164792e74696d6573706c696e7465722e63682f546861446166696e7365722f707372362d6e756c6c2d63616368652f62616467652e737667)](https://travis-ci.org/ThaDafinser/psr6-null-cache)

[![Latest Stable Version](https://camo.githubusercontent.com/1bf86caff9ce532f7553fbb06f4bb39377cab7d78cd58d82e4875180b2a51e69/68747470733a2f2f706f7365722e707567782e6f72672f746861646166696e7365722f707372362d6e756c6c2d63616368652f762f737461626c65)](https://packagist.org/packages/thadafinser/psr6-null-cache)[![Latest Unstable Version](https://camo.githubusercontent.com/06b7588f0c6c08c08317829e5eb2eac5b04b1cccba49912ecdc2c5674c64be11/68747470733a2f2f706f7365722e707567782e6f72672f746861646166696e7365722f707372362d6e756c6c2d63616368652f762f756e737461626c65)](https://packagist.org/packages/thadafinser/psr6-null-cache)[![License](https://camo.githubusercontent.com/675289abe93e437943f3ba507e1e650cdb5394a45cd42290e3aa79409fcaf1c1/68747470733a2f2f706f7365722e707567782e6f72672f746861646166696e7365722f707372362d6e756c6c2d63616368652f6c6963656e7365)](https://packagist.org/packages/thadafinser/psr6-null-cache)[![Total Downloads](https://camo.githubusercontent.com/836e9e679d18ff11e1060cbf815f8437f238a7c21c639fadea45321105e19b2f/68747470733a2f2f706f7365722e707567782e6f72672f746861646166696e7365722f707372362d6e756c6c2d63616368652f646f776e6c6f616473)](https://packagist.org/packages/thadafinser/psr6-null-cache)

The missing PSR-6 NullObject implementation.

You can use this package, when you want to

- avoid using `null` check logic, read more [here](http://designpatternsphp.readthedocs.org/en/latest/Behavioral/NullObject/README.html)
- need a fake cache implementation for testing

Install
-------

[](#install)

```
composer require thadafinser/psr6-null-cache

```

Example / usage
---------------

[](#example--usage)

Before this package, you needed to allow `null` as a parameter, if you wanted to avoid a package dependency to a specific `PSR-6 cache implementation`

### Old code

[](#old-code)

```
namespace MyPackage;

use Psr\Cache\CacheItemPoolInterface;

class MyCode
{

    public function __construct(CacheItemPoolInterface $cache = null)
    {
        $this->cache = $cache;
    }

    /**
     * Can return an instance of null, which is bad!
     *
     * @return null CacheItemPoolInterface
     */
    public function getCache()
    {
        return $this->cache;
    }

    private function internalHeavyMethod()
    {
        $cacheKey = 'myKey';

        // you need to check first, if there is a cache instance around
        if ($this->getCache() !== null && $this->getCache()->hasItem($cacheKey) === true) {
            // cache is available + it has a cache hit!
            return $this->getCache()->getItem($cacheKey);
        }

        $result = do_something_heavy();

        // you need to check first, if there is a cache instance around
        if ($this->getCache() !== null) {
            $item = $this->getCache()->getItem($cacheKey);
            $item->set($result);
            $this->getCache()->save($item);
        }

        return $result;
    }
}
```

### New code

[](#new-code)

```
namespace MyPackage;

use Psr\Cache\CacheItemPoolInterface;
use Psr6NullCache\NullCacheItemPool;

class MyCode
{

    /**
     * You could require a cache instance, so you can remove the null check in __construct() as well
     *
     * @param CacheItemPoolInterface $cache
     */
    public function __construct(CacheItemPoolInterface $cache = null)
    {
        if($cache === null){
            $cache = new NullCacheItemPool();
        }

        $this->cache = $cache;
    }

    /**
     * @return CacheItemPoolInterface
     */
    public function getCache()
    {
        return $this->cache;
    }

    private function internalHeavyMethod()
    {
        $cacheKey = 'myKey';

        if ($this->getCache()->hasItem($cacheKey) === true) {
            // cache is available + it has a cache hit!
            return $this->getCache()->getItem($cacheKey);
        }

        $result = do_something_heavy();

        $item = $this->getCache()->getItem($cacheKey);
        $item->set($result);
        $this->getCache()->save($item);

        return $result;
    }
}
```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.1% 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

3725d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5c11fd53e7dee0524aa83e44f3928306577f782c37c4fac49ee7254ad17b5f4f?d=identicon)[ThaDafinser](/maintainers/ThaDafinser)

---

Top Contributors

[![ThaDafinser](https://avatars.githubusercontent.com/u/533017?v=4)](https://github.com/ThaDafinser "ThaDafinser (16 commits)")[![stof](https://avatars.githubusercontent.com/u/439401?v=4)](https://github.com/stof "stof (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/thadafinser-psr6-null-cache/health.svg)

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

###  Alternatives

[symfony/symfony

The Symfony PHP framework

31.3k86.3M2.2k](/packages/symfony-symfony)[doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.

9.7k578.4M5.6k](/packages/doctrine-dbal)[symfony/cache

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

4.2k348.9M2.5k](/packages/symfony-cache)[google/auth

Google Auth Library for PHP

1.4k272.7M161](/packages/google-auth)[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M647](/packages/sylius-sylius)[nelmio/api-doc-bundle

Generates documentation for your REST API from attributes

2.3k63.6M232](/packages/nelmio-api-doc-bundle)

PHPackages © 2026

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