定制 stellarwp/memoize 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

stellarwp/memoize

Composer 安装命令:

composer require stellarwp/memoize

包简介

A flexible memoization library for memory caching.

README 文档

README

Tests PHPCS PHPStan Compatibility

A flexible memoization library for memory caching.

Table of Contents

Memoization

Memoization is the process of caching the results of expensive function calls so that they can be reused when the same inputs occur again.

Installation

It's recommended that you install Memoize as a project dependency via Composer:

composer require stellarwp/memoize

We actually recommend that this library gets included in your project using Strauss.

Luckily, adding Strauss to your composer.json is only slightly more complicated than adding a typical dependency, so checkout our strauss docs.

Notes on examples

All namespaces within the examples will be using the StellarWP\Memoize, however, if you are using Strauss, you will need to prefix these namespaces with your project's namespace.

Usage

Simple example

use StellarWP\Memoize\Memoizer;

$memoizer = new Memoizer();

$memoizer->set('foo', 'bar');

if ($memoizer->has('foo')) {
    echo $memoizer->get('foo'); // Outputs: bar
}

// Unsets foo from the memoization cache.
$memoizer->forget('foo');

Setting a nested structure

Memoize allows you to use dot notation to set, get, and forget values from a nested structure. This allows you to easily add/fetch values and then purge individual items or whole collections of items.

use StellarWP\Memoize\Memoizer;

$memoizer = new Memoizer();

$memoizer->set('foo.bar.bork', 'baz');

// This results in the following cache:
// [
//     'foo' => [
//         'bar' => [
//             'bork' => 'baz',
//         ],
//     ],
// ]

// You can fetch the value like so:
$value = $memoizer->get('foo.bar.bork');
echo $value; // Outputs: baz

// You can fetch anywhere up the chain:
$value = $memoizer->get('foo.bar');
echo $value; // Outputs: [ 'bork' => 'baz' ]

$value = $memoizer->get('foo');
echo $value; // Outputs: [ 'bar' => [ 'bork' => 'baz' ] ]

$value = $memoizer->get();
echo $value; // Outputs: [ 'foo' => [ 'bar' => [ 'bork' => 'baz' ] ] ]

Purging a nested structure

use StellarWP\Memoize\Memoizer;

$memoizer = new Memoizer();

$memoizer->set('foo.bar.bork', 'baz');
$memoizer->forget('foo.bar.bork');

// This results in the following cache:
// [
//     'foo' => [
//         'bar' => [],
//     ],
// ]

$memoizer->forget('foo.bar');

// This results in the following cache:
// [
//     'foo' => [],
// ]

$memoizer->forget('foo');

// This results in the following cache:
// []

$memoizer->forget();

// This results in the following cache:
// []

Caching with closures

Memoize also supports using closures as values that get resolved before setting in the cache.

use StellarWP\Memoize\Memoizer;

$memoizer = new Memoizer();

$memoizer->set('foo', static function () {
    return 'bar';
});

echo $memoizer->get('foo'); // Outputs: bar

Using with a dependency injection container

Project class

<?php

declare(strict_types=1);

namespace StellarWP\MyProject;

use StellarWP\Memoize\MemoizerInterface;

// Dependencies automatically auto-wired due to the definitions in ServiceProvider.php via
// $this->container->get( MyProjectClass::class )

/**
 * An example class inside your project using the Memoize library.
 */
class MyProjectClass
{

    private MemoizerInterface $memoizer;

    public function __construct( MemoizerInterface $memoizer )
    {
        $this->memoizer = $memoizer;
    }

    public function get( string $name ): string
    {
        $result = $this->memoizer->get( $name );

        if ( ! $result ) {
            $result = 'some very expensive operation';

            $this->memoizer->set( $name, $result );
        }

        return $result;
    }

    public function delete( string $name ): bool
    {
        $this->memoizer->forget( $name );

        // Run delete operation...

        return true;
    }
}

Service Provider

<?php

declare(strict_types=1);

namespace StellarWP\Memoize;

use StellarWP\ContainerContract\ContainerInterface;
use StellarWP\Memoize\Contracts\DriverInterface;
use StellarWP\Memoize\Contracts\MemoizerInterface;
use StellarWP\Memoize\Drivers\MemoryDriver;

/**
 * Container ServiceProvider to tell the DI Container how to build everything when another
 * instance is requested from the Container that uses our interface.
 *
 * @example $this->container->get( MyProjectClass::class );
 */
final class ServiceProvider
{
    private ContainerInterface $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function register(): void
    {
        $this->container->singleton( DriverInterface::class, MemoryDriver::class );
        $this->container->bind( MemoizerInterface::class, Memoizer::class );
    }
}

Drivers

Memoize comes with a single driver out of the box, but you can also create your own using the StellarWP\Memoize\Contracts\DriverInterface.

MemoryDriver

The MemoryDriver is a simple in-memory driver. Basically, contains an array property in the driver that holds the memoized values. You can manually specify the use of this driver or any other driver like so:

use StellarWP\Memoize\Memoizer;
use StellarWP\Memoize\Drivers\MemoryDriver;

$memoizer = new Memoizer(new MemoryDriver());

Traits

The MemoizeTrait provides a convenient way to add memoization to your classes, ensuring that cached results are isolated to each instance, preventing cross-talk or key collisions between different objects. Ideal for enhancing existing classes with lightweight, instance-specific caching.

💡 The MemoizeTrait uses in-memory storage only and cannot be changed.

<?php

declare(strict_types=1);

namespace StellarWP\MyProject;

use StellarWP\Memoize\MemoizerInterface;
use StellarWP\Memoize\Traits\MemoizeTrait;

/**
 * An example class inside your project.
 */
class MyProjectClass implements MemoizerInterface
{
    use MemoizeTrait;

    public function find( string $id ): string {
       $result = $this->get( $id );

        if ( ! $result ) {
            $result = 'some very expensive operation';

            $this->set( $id, $result );
        }

        return $result;
    }

    public function delete( string $id ): bool
    {
        $this->forget( $id );

        // Run delete operation...

        return true;
    }
}

stellarwp/memoize 适用场景与选型建议

stellarwp/memoize 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 37.11k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 12 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 stellarwp/memoize 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 stellarwp/memoize 我们能提供哪些服务?
定制开发 / 二次开发

基于 stellarwp/memoize 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-2.0
  • 更新时间: 2024-12-19