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
Kewobject. However, sinceKewcan 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 ifsize()is0or not.
Or, to visualize it a little differently:
- Creative:
add()/push(). - Destructive:
get()/pop(). - Non-destructive:
next()/top()andlast()/bottom() - State:
size()andisEmpty()
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 (Defaulttrue). Enforces Type locking based on the first item added to the list. Passingfalsehere will allow you to add items of different types to the list.nextable- Boolean (defaulttrue). Allows you to 'next()' the list, giving you the next item without removing it. Passingfalsehere will disable thenext()method.lastable- Boolean (defaulttrue). Allows you to 'last()' the list, giving you the last item without removing it. Passingfalsehere will disable thenext()method.sizeable- Boolean (defaulttrue). Allows you tosize()the list. Passingfalsehere will no longer allow you to see the size of the list.stack- Boolean (defaultfalse). Passingtruereverses the order of processing, turning the list from First in, First out to Last in, First out.readable- Boolean (defaulttrue). Grouping ofnextable,lastableandsizeable. Passingfalsehere is like passingfalseto those three and will limit you to onlyaddandgetvalues.
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 returnstrueif there are ten active processing sessions available. - You then refactor everything calling
startProcessing($image)method to instead call theaddToProcessingQueue($image)method. All it does, in turn, isadd()to its ownKewobject. - 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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-12-12