在 PHP 中使用自定义项序列化优先级队列

发布于 2024-11-16 17:44:28 字数 1289 浏览 2 评论 0原文

我正在 Zend 应用程序中实现基于 PHP 的 SPLPriorityQueue 的自定义优先级队列。它包含自定义对象 PriorityQueueItens,而不是优先级值之外的纯值。当将队列存储在APC(或memcache)中时,我必须确保队列及其项目是可序列化的,因此我将它们设置为实现可序列化接口使用即将推出的 Zend Framework 2 中的代码

public function serialize()
{
    $data = array();
    while ($this->valid()) {
        $data[] = $this->current();
        $this->next();
    }
    foreach ($data as $item) {
        $this->insert($item['data'], $item['priority']);
    }
    return serialize($data);
}

public function unserialize($data)
{
    foreach (unserialize($data) as $item) {
        $this->insert($item['data'], $item['priority']);
    }
}

从 APC 获取队列并使用 $this->extract() 检索优先级队列中的顶部项目,我没有获得该项目,而是获得了序列化期间创建的数组。

因此,我得到的不是用于存储在队列中的对象的基类 PriorityQueueItem,而是一个带有索引 datapriority 的关联数组(类似于序列化函数中的数组)。为了获取实际的项目,我需要检索数组的 data 部分,而不是将返回的项目视为一个项目,这就是它在不将队列存储在 APC 中时的工作原理以及我假设的情况现在也工作。

这是对象序列化的一个特性还是我以错误的方式处理这个问题?

更新:这里的问题是我有一个单独的函数,除了 extract() 之外,它还做了额外的工作。该函数以数组形式返回该项目,但是当我显式调用 extract() 时,我就按预期获得了该项目。对于已序列化的对象中的公共函数是否需要采取某些预防措施?

I'm implementing a customized priority queue based on PHP's SPLPriorityQueue in a Zend Application. It contains custom objects, PriorityQueueItens, instead of pure values aside the priority value. When storing the queue in APC (or memcache) I have to make sure the queue and its items are serializable, so I've set them to implement the Serializable interface using code from the upcoming Zend Framework 2.

public function serialize()
{
    $data = array();
    while ($this->valid()) {
        $data[] = $this->current();
        $this->next();
    }
    foreach ($data as $item) {
        $this->insert($item['data'], $item['priority']);
    }
    return serialize($data);
}

public function unserialize($data)
{
    foreach (unserialize($data) as $item) {
        $this->insert($item['data'], $item['priority']);
    }
}

After fetching the queue from APC and retrieving the top item in the priority queue, using $this->extract(), I don't get the item but the array that is created during serialization.

So, instead of a PriorityQueueItem, the base class I use for objects stored in the queue, I get an associative array with indices data and priority (similar to the array in the serialize function). To get the actual item I need to retrive the data part of the array instead of treating the returned item as an item, which is how it works when not storing the queue in APC and how I assumed it would work now as well.

Is this a feature of serialization of objects or am I approaching this in a wrong way?

Update: The issue here was that I had a separate function that did extra cruft besides the extract(). This function returned the item as an array, but as soon as I called extract() explicitly I got the item as expected. Are there certain precautions to take with public functions in objects that have been serialized?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城半夏 2024-11-23 17:44:28

您可能混合/切换了这一点:

在您的代码中,您正在序列化 $data 数组,而不是“您的对象”。我不完全确定这一点,因为我不知道 insert() 函数的用途。

但是对于使用 serialized 接口 的对象进行序列化,您将得到什么已从 object::serialize() 返回。

当你序列化一个数组时,你将得到序列化后的数组。 PHP 在后台负责将其存储为您的对象。

You mixed/switched this probably:

In your code you are serializing the $data array, not "your object". I'm not entirely sure of this because I do not know what the insert() function is for.

But for the serialize in an object with the serializable interface you will get back what has been returned from object::serialize().

As you serialize an array, you will get the serialized array back. PHP in the background is taking care that this was stored as your object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文