承接 c01l/phpdecorator-memo 相关项目开发

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

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

c01l/phpdecorator-memo

Composer 安装命令:

composer require c01l/phpdecorator-memo

包简介

Memoization for PHP methods using a Python style Decorator pattern.

README 文档

README

The phpdecorator library extension can be used to store return values in memory depending on its parameters.

This library uses the parameters of a function to build a cache key to cache function call results.

How to use it?

  1. Add Memo attribute to a function.
  2. Specify the how the cache key is built.
class TestClass
{
    #[Memo(["bar"])]
    public function foo(int $bar, int $x): int
    {
        return $x;
    }
}

$obj = new TestClass();
$obj = (new \C01l\PhpDecorator\DecoratorManager())->decorate($obj);

$obj->foo(1,2) // 2
$obj->foo(2,3) // 3
$obj->foo(1,3) // 2, because foo is not really executed again, but the cached version is used

Using complex memo keys

The key can be composed of multiple values, by adding more elements to the array:

#[Memo(["bar", "x"])]
public function foo($bar, $x) { return $x; }

Using no field will result in the function being only called once and different arguments are not considered.

#[Memo]
public function foo($bar, $x) { return $x; }

...

$obj->foo(1,2); // 2
$obj->foo(1,3); // 2
$obj->foo(2,5); // 2
$obj->foo(4,6); // 2

You can use an arbitrary expression in the key definition.

class A {
    public int $x, $y, $z;
    public function getX() { return $x; }
}

...

#[Memo(["bar->z", "bar->getX()"])]
public function foo(A $bar, $x) { return $x; }

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-12-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固