可以memcache存储PHP中的数据结构,例如splqueue


Can memcache store data structures in PHP such as splqueue?

我正在尝试将splqueue存储到memcache(而不是memcached)。以下示例代码是用于此目的的简单测试。

$mc = new Memcache();
$mc->addServer("127.0.0.1", 11300);
$mc->addServer("127.0.0.1", 11301);
$mc->addServer("127.0.0.1", 11302);
$q = new SplQueue();
$q->enqueue(10);
$q->enqueue(20);
$q->count(); // line a
$mc->set("spl_queue", $q);
$p = $mc->get("spl_queue");
$p->count(); // line b

当我运行这段代码时,我在a行得到了2,在b行得到了0。所以这可能意味着在memcache中存储数据结构不起作用。

所以我有以下三个问题。

  1. 我做错了什么吗?或者有其他方法可以将splqueue存储在memcache中?

  2. 我还为Spl数据结构找到了SplObjectStorage。这能解决我的问题吗?

  3. memcached(NOT memcache)可以存储数据结构吗?

Memcache::set()将序列化非标量值。SplQueue似乎没有实现SPL Serializable接口,因此不能依赖它来正确序列化/取消序列化。您可以扩展SplQueue,实现Serializable,然后为此构建一些适当的序列化/非序列化策略。

SplObjectStorage确实实现了Serializable,所以我希望它能正确地进出Memcache

对于其他SPL结构,您应该检查Serializable的实现。

希望这能有所帮助。