xtain/psr6-null 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

xtain/psr6-null

Composer 安装命令:

composer require xtain/psr6-null

包简介

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

README 文档

README

The missing PSR-6 NullObject implementation.

You can use this package, when you want to

  • avoid using null check logic, read more here
  • need a fake cache implementation for testing

Install

composer require xtain/psr6-null

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

namespace MyPackage;

use Psr\Cache\CacheItemPoolInterface;

class MyCode
{

    public function setCache(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 XTAIN\Psr\Cache\NullCacheItemPool;
use XTAIN\Psr\Cache\CacheItemPoolAwareInterface;
use XTAIN\Psr\Cache\CacheItemPoolAwareTrait;

class MyCode implements CacheItemPoolAwareInterface
{
    use CacheItemPoolAwareTrait;

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

    /**
     * @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;
    }
}

统计信息

  • 总下载量: 47
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-01-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固