php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #57127 APC does not store array of objects
Submitted: 2006-07-04 19:12 UTC Modified: 2010-07-29 13:14 UTC
From: pecl at seven dot net dot nz Assigned:
Status: Closed Package: APC (PECL)
PHP Version: 5.1.0 OS: Win32
Private report: No CVE-ID: None
 [2006-07-04 19:12 UTC] pecl at seven dot net dot nz
Description:
------------
PHP version is 5.1.4, not 5.1.0.
Apache 2.2.2, Windows XP.
APC version 3.0.10.
Same result on Windows Server 2003.

When storing an array of objects, on retrieval the array indices are kept, but the objects are NULL.

Curiously, It *does* work in one of several much more complex cases I'm using, but not this most basic one.

Reproduce code:
---------------
<?php
$key = 'test';

$original = array (new stdClass ());

apc_store ($key, $original, 3600);
$cached = apc_fetch ($key);

print "<pre>\n";

var_dump ($original);
var_dump ($cached);
?>

Expected result:
----------------
array(1) {
  [0]=>
  object(stdClass)#1 (0) {
  }
}
array(1) {
  [0]=>
  object(stdClass)#1 (0) {
  }
}

Actual result:
--------------
array(1) {
  [0]=>
  object(stdClass)#1 (0) {
  }
}
array(1) {
  [0]=>
  NULL
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-07-04 19:17 UTC] rasmus@php.net
You need the cvs version for this to work, but in general caching objects is a really bad idea because they have to be serialized and deserialized internally and all you can store are the properties anyway.  You are much better off just storing pure arrays.  Much quicker.
 [2006-07-04 19:19 UTC] pecl at seven dot net dot nz
Edit: It doesn't work in a more complex case with a straight array; however it does work when wrapped in an ArrayIterator, for example:

<?php
$original = new ArrayIterator (array (new stdClass ()));
?>
 [2006-07-04 19:27 UTC] pecl at seven dot net dot nz
My usage of it is caching a row (or several rows) from a remote database that needs to be looked up almost every request, but doesn't change very often.

Unserializing something from shared memory is still going to be a lot faster than fetching something from a database, surely?
 [2006-07-04 19:30 UTC] rasmus@php.net
I suppose, but a row from a database is not an object.  It is just data, so wrapping it inside an object which needs to be serialized and unserialized is pure overhead.  Despite that, this works fine in CVS, so no bug here.
 [2007-06-05 05:17 UTC] smith at pooteeweet dot org
I am experiencing the same issue trying to cache soap replies with sequences (which unfortunately are automatically wrapped in stdClass instances). I am running APC 3.0.13, which was released after you marked this bug as bogus.

At any rate, it might be a good idea to add a special handler into APC specifically for stdClass to not have to serialize.
 [2007-09-17 05:43 UTC] d dot albano at gmail dot com
Hi,

i'm using caching with APC and Object Arrays but i have the same problem: when i fetch an array of nulls is returned!

I'm using PHP 5.1.4 on Apache 2.2 on Windows XP PRO SP2 and APC 3.0.15-dev (Revision: 3.150, Aug 30 2007 07:08:38)

Output, using var_dump, is:
array(2) { ["it_IT"]=>  NULL ["DEFAULT"]=>  NULL }
 [2007-09-18 17:18 UTC] shire@php.net
Looks like this is caused because we don't serialize anything under the zval being passed in.  ie: we'll serialize a single object but not an array of objects because we don't check each element.  

Looks like this could be resolved by moving this logic out of apc_cache_store_zval() to a lower level....  

Gopal: any preferences on how to solve this taking into consideration the changes you'd like to make to this code in the near future?  Seems like moving this lower could make things messy.  I could make a patch but I don't want to make things worse.
 [2007-09-18 20:47 UTC] gopalv82 at yahoo dot com
Already working on it ... for a month or so now.

http://t3.dotgnu.info/blog/2007/Aug/11/

And it's taking its own sweet time to come together (and 3.0.15 not being out of the gate isn't helping at all).
 [2007-12-28 08:49 UTC] argiope at gmail dot com
This bug is back.
APC 3.0.16 and 3.0.17-dev is affected.
 [2008-01-11 07:06 UTC] daritter at rttr dot de
Simple Workaround 3.0.16: instead of 

apc_store($key,$array)
$array = apc_fetch($key)

do something like

apc_store($key,(object) $array)
$array = (array) apc_get($key)
 [2008-07-17 12:54 UTC] gopalv82 at yahoo dot com
Fixed in CVS HEAD, please re-test
 [2009-01-27 06:12 UTC] soywiz at gmail dot com
Fails also in 3.0.18 and 3.0.19.

A generic workaround is to store a serialized version of the variable:

list($key, $var) = array('key', unserialize('a:1:{i:0;O:8:"stdClass":1:{s:5:"index";i:0;}}'));

// works
apc_store($key, serialize($var));
$var2 = unserialize(apc_fetch($key));
assert($var == $var2);

// fails
apc_store($key, $var);
$var2 = apc_fetch($key);
assert($var == $var2);
 [2010-02-26 20:13 UTC] nospam at elehost dot com
Confirmed problem still exists on 3.0.19 with Freebsd 7.2

Serialize workaround got beyond the problem.
 [2010-03-30 13:17 UTC] blackstoneblacksword at gmail dot com
Bug still exists in latest stable version APC 3.0.19-3 and
PHP 5.3.2-1 under Ubuntu lucid (development branch).

serializing and unserializing works - but is this an acceptable workaround?
 [2010-03-30 14:04 UTC] pecl at seven dot net dot nz
It has been 3.5 years since I submitted this, and I don't 
often use PHP 
any more so don't really care, but I wouldn't consider this 
bug closed 
if you still need to manually serialize and unserialize it.

I still get an email every now and again people who have run 
into the 
bug and confirm it still occurs.
 [2010-07-02 11:40 UTC] don+php-bugs at tpyo dot net
Just came across this bug when deploying to 5.2.4.

This bug doesn't seem to exist in 5.3.2 - I wouldn't be able to say what version it was actually fixed in, but my guess is somewhere between 5.2.4 and 5.3.2.
 [2010-07-02 11:49 UTC] don+php-bugs at tpyo dot net
Forgot to mention, this is working with APC 3.1.4.
 [2010-07-29 13:14 UTC] gopalv82 at yahoo dot com
Awesome, works with 3.1.4-dev is good news.

Closing.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 06:01:32 2024 UTC