定制 ksamborski/apcu-memo 二次开发

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

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

ksamborski/apcu-memo

Composer 安装命令:

composer require ksamborski/apcu-memo

包简介

README 文档

README

#apcumemo PHP library for memoization function results based on it's arguments.

Requirements

You need to have acpu extension installed for php >= 7.0.0

Installation

composer require ksamborski/apcu-memo

Basic usage

Functional paradigm teaches us that pure functions are the way to go. They provide two main features:

  • because their result depends only on their arguments they can be easly tested (you don't need any mocks or stubs)
  • because they have no side effects they can be easly cached

Consider this example:

use APCuMemo\APCuMemo;

function sumrange($a, $b)
{
    return APCuMemo::memoize(
        function(...$_) use ($a, $b) {
            return array_reduce(range($a, $b), function ($product, $item) { return $product + $item; }, 1);
        },
        [ 'ttl' => 5 ],
        "sumrange",
        $a,
        $b
    );
}

$start = microtime(true);
echo sumrange((int) $_GET['a'], (int) $_GET['b']);
$elapsed = microtime(true) - $start;
echo " " . ($elapsed * 1000) . " ms";

We have a simple function that sums integers from $a to $b. It can take some time but when we compute it for the first time then we can cache it. Notice the 'ttl' parameter. It is set to 5 seconds and means that if nobody asks us for the same range within 5 seconds it will forget the result. But every request for cached value will reset the ttl. That way we can have only mostly used values in cache.

Let's see it in action. For $a = 1 and $b = 1831233 it will return something like:

1676708065762 283.94412994385 ms

And the followed requests will return:

1676708065762 0.030040740966797 ms

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-10-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固