I am trying to store splqueue to memcache (NOT memcached). The following sample code is a simple test for this purpose.
$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
When I run this code, I got 2 in line a and 0 in line b. So it probably means storing data structure in memcache doesn’t work.
So I have following three questions.
-
Did I do anything wrong or there is another way to store splqueue in memcache?
-
I also found SplObjectStorage for Spl data structures. Can this be a solution for my problem?
-
Can memcached (NOT memcache) store data structures?
Memcache::set()will serialize non-scalar values.SplQueuedoes not appear to implement the SPLSerializableinterface, and so cannot be relied upon to serialize/unserialize correctly. You could extendSplQueue, implementSerializable, and then construct some appropriate serialization/unserialization strategy for this purpose.SplObjectStoragedoes implementSerializable, and so I would expect it to come in and out ofMemcacheproperly.For other SPL structures, you should check for implementation of
Serializable.Hope this helps.