承接 phasync/objectpool 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

phasync/objectpool

Composer 安装命令:

composer require phasync/objectpool

包简介

A library for implementing efficient object pooling.

README 文档

README

An efficient object pool implementation for PHP objects.

Example

With any class that can benefit from object pooling:

class Vector implements phasync\Util\ObjectPoolInterface {
    use phasync\Util\ObjectPoolTrait;

    public string $name;
    public ?PDO $dbConnection;

    /**
     * Instead of creating instances via the constructor, instances must be
     * created via a static method (such as this `create()` function). 
     */
    public static function create(string $name, PDO $dbConnection): static {
        $instance = static::popInstance() ?? new static();
        $instance->name = $name;
        $instance->dbConnection = $dbConnection;
    }

    /**
     * The constructor MUST be declared as private or protected, to ensure
     * you don't leak memory by adding an infinite number of instances to
     * the pool without ever recovering them.
     */
    private function __construct() {}

    /**
     * This function is used to return the instance to the object pool.
     * The function must ensure that a clean object is returned to the
     * object pool, removing any non-scalar values - even if the properties
     * would be overwritten when retrieved. This is to avoid memory leaks.
     */
    public function returnToPool(): void {
        /**
         * It is ESSENTIAL to unset references to values such as
         * `$this->dbInstance` before returning the object to the pool.
         */
        $this->name = '';
        $this->dbConnection = null;
        $this->pushInstance();
    }
}

Warning

Only use this for internal objects that your code uses a lot, and which other developers don't directly work with. This is the only way to ensure that there are no remaining references to the instance when returning the object to the pool. You MUST avoid the case where a developer is retaining a reference to the instance when you add it to the object pool.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-11