定制 brekitomasson/kew 二次开发

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

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

brekitomasson/kew

Composer 安装命令:

composer require brekitomasson/kew

包简介

Simple implementation of FIFO/LIFO in PHP.

README 文档

README

A simple little package to help you set up and handle Last In-First Out and Last In-Last Out scenarios.

Step The First:

~/your_projects/folder> composer require brekitomasson/kew

Step The Second:

$foo = new BrekiTomasson\Kew\Kew();
$foo->add('red');
$foo->add('blue');
$foo->add('green);
$foo->next();
> 'red'
$foo->next();
> 'red'
$foo->get();
> 'red'
$foo->get();
> 'green'
$foo->size();
> 1
$foo->add('yellow');
$foo->size();
> 2
$foo->next();
> 'blue'
$foo->last();
> 'yellow'
$foo->get();
'yellow'

Description & Syntax

Note: I use the term "list" in several places in this document. In most cases, this just means an instantiation of the Kew object. However, since Kew can be used both as a Queue and as a Stack, I'm using the more generic 'list' for both.

All in all, this is an extremely straight-forward package. Simply instantiate a new Kew (Yes, pronounced like "queue". I know, so very clever...) object into a variable, and you will have the following methods at your disposal:

  • add() - Adds one or more items to the list, in the order you list them.
  • push() - Alias for "add()".
  • get() - Gets the next item in line from the list, removing it from the list.
  • pop() - Alias for "get()".
  • next() - Shows you what the next value in the list is without removing it.
  • top() - Alias for "next()".
  • last() - Shows you what the last value in the list is at this moment.
  • bottom() - Alias for "last()".
  • size() - Gives you the number of items in the list.
  • isEmpty() - Returns true/false depending on if size() is 0 or not.

Or, to visualize it a little differently:

  • Creative: add() / push().
  • Destructive: get() / pop().
  • Non-destructive: next() / top() and last() / bottom()
  • State: size() and isEmpty()

Philosophy & Limitations

  • The only values you have access to are the first and last items on the list.
  • You can only add new values to the end of the list.
  • You can only remove values from the beginning of the list.
  • You can only remove one value at a time from the list.
  • You cannot add items to the list on creation, all lists are created empty.
  • You can view the size of the list, but not its contents (except of course, the first and last items in the list).
  • Values in the list must be of the same type, as determined by the first item added to the list. (See Options for an exception to this rule)

Options

Despite lists being a fairly straight-forward data type, there are a couple of things that you can change during construction of your list - only during construction. To set any of these values, pass an associative array during construction of the list containing one or more of these values:

  • typed - Boolean (Default true). Enforces Type locking based on the first item added to the list. Passing false here will allow you to add items of different types to the list.
  • nextable - Boolean (default true). Allows you to 'next()' the list, giving you the next item without removing it. Passing false here will disable the next() method.
  • lastable - Boolean (default true). Allows you to 'last()' the list, giving you the last item without removing it. Passing false here will disable the next() method.
  • sizeable - Boolean (default true). Allows you to size() the list. Passing false here will no longer allow you to see the size of the list.
  • stack - Boolean (default false). Passing true reverses the order of processing, turning the list from First in, First out to Last in, First out.
  • readable - Boolean (default true). Grouping of nextable, lastable and sizeable. Passing false here is like passing false to those three and will limit you to only add and get values.

Examples, AKA "Stacks and Queues"

Imagine the following two scenarios:

Example 1 - Queue

You're building an app to automate the processing and prettification of user's pictures before they post them to an online service. Unfortunately, despite the fast network speeds that allow people to upload hundreds of pictures in just a minute, the processing takes about a minute per picture. Instead of having to wait for all pictures to be done, you want to make them available to the user as processing finishes each picture.

Enter Kew! To get this to work, you could do something like this:

  • You've decided that only ten workers can be active at a time due to CPU/RAM available to you, so you set up an isBusy() method that returns true if there are ten active processing sessions available.
  • You then refactor everything calling startProcessing($image) method to instead call the addToProcessingQueue($image) method. All it does, in turn, is add() to its own Kew object.
  • You then refactor the processing job to no longer be called from an outside method. Instead, it runs on a schedule, which fires once a second, calling code like:
if ($this->isBusy()) {
    return false;
} else {
  if ($this->kewObject->isEmpty()) {
      return false;
  } else {
      $this->startProcessing($this->kewObject->get());
  }
}

... or, more simply:

if (!$this->isBusy() && !$this->kewObject->isEmpty()) {
    $this->startProcessing($this->kewObject->get());
}

Example 2 - Stack

You're expanding the social aspects of your picture processing tool by allowing people to navigate pictures posted by other users. It's easy to navigate to a new user or new picture, of course, but you need some way for users to go back in their navigation history ...

Enter Kew! Every time a user starts a session, they're assigned a new Kew object, instantiated with the option ['stack' => true]. Whenever they click to another page in the app, the current page (the one that they are leaving, not the one that they are navigating to) is add():ed to the stack. Then, if they ever need to go back, all you need is to get() from the stack. If they want to continue backwards, just continue to get() from the stack until they are where they want to be.

TODO / Future development

  • Implement the stack functionality.
  • Implement a 'max size' feature (off by default) with these options:
  • If max size is reached, return exception.
  • If max size is reached, bump item from queue/stack.

brekitomasson/kew 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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