2017 © Pedro Peláez
 

library psr6-null-cache

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

image

thadafinser/psr6-null-cache

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

  • Friday, April 8, 2016
  • by ThaDafinser
  • Repository
  • 1 Watchers
  • 2 Stars
  • 124 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

PSR-6 NullObject cache

Build Status Code Coverage Scrutinizer Code Quality PHP 7 ready, (*1)

Latest Stable Version Latest Unstable Version License Total Downloads, (*2)

The missing PSR-6 NullObject implementation., (*3)

You can use this package, when you want to - avoid using null check logic, read more here - need a fake cache implementation for testing, (*4)

Install

composer require thadafinser/psr6-null-cache

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, (*5)

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

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;
    }
}

The Versions

08/04 2016

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Martin Keckeis

26/02 2016

v0.1.0

0.1.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Martin Keckeis

26/02 2016

dev-feature/memory

dev-feature/memory

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Martin Keckeis