Is PDO incompatible with caching mechanismes like APC?
Is PDO incompatible with caching mechanismes like APC?
am 30.03.2008 04:36:03 von DFS
Hi
APC doesn't like it when I store the output of a SELECT:
===
include("apc.php");
$dbh = new PDO("sqlite:./test.sqlite");
$sql = "CREATE TABLE IF NOT EXISTS mytable (name TEXT)";
$dbh->exec($sql);
$sql = "INSERT INTO mytable VALUES (?)";
$insert = $dbh->prepare($sql);
$insert->execute(array("dead"));
$sql = "INSERT INTO mytable VALUES (?)";
$insert = $dbh->prepare($sql);
$insert->execute(array("beef"));
$sql = "SELECT * FROM mytable";
store("rows",$dbh->query($sql));
$rows = fetch("rows");
foreach($rows as $row) {
print $row['name'] . "
";
}
$dbh = null;
===
PHP Fatal error: Uncaught exception 'PDOException' with message 'You
cannot serialize or unserialize PDOStatement instances' in
/usr/local/www/apache22/data/apc.php:9\nStack trace:\n#0 [internal
function]: PDOStatement->__sleep()\n#1
/usr/local/www/apache22/data/apc.php(9): apc_store('rows',
Object(PDOStatement), 0)\n#2
/usr/local/www/apache22/data/test.php(21): store('rows',
Object(PDOStatement))\n#3 {main}\n thrown in
/usr/local/www/apache22/data/apc.php on line 9
===
Does it mean that PDO won't work with APC, and I should look at
another caching tool?
Thank you.
Re: Is PDO incompatible with caching mechanismes like APC?
am 30.03.2008 05:54:08 von Jeremy
Gilles Ganault wrote:
> Hi
>
> APC doesn't like it when I store the output of a SELECT:
>
>
>
> Does it mean that PDO won't work with APC, and I should look at
> another caching tool?
>
> Thank you.
Before an object is stored it has to be serialized. Generally, internal
classes (like PDOStatement) cannot be serialized.
It seems like what you want to do is store an array of the rows, which
is perfectly possible. Just use $dbh->query(...)->fetchAll() or
something similar. The query() method returns a PDOStatement object -
while it can be iterated using foreach (thanks to SPL) it is not an
array and cannot be serialized.
Jeremy
Re: Is PDO incompatible with caching mechanismes like APC?
am 30.03.2008 13:26:45 von DFS
On Sat, 29 Mar 2008 20:54:08 -0700, Jeremy wrote:
>It seems like what you want to do is store an array of the rows, which
>is perfectly possible. Just use $dbh->query(...)->fetchAll() or
>something similar.
Thanks for the tip. This code works:
$sql = "SELECT * FROM mytable";
$sth = $dbh->prepare($sql);
$sth->execute();
store("rows",$sth->fetchAll());
$rows = fetch("rows");
foreach($rows as $row) {
print $row['name'] . "";
}
Re: Is PDO incompatible with caching mechanismes like APC?
am 30.03.2008 14:13:30 von DFS
On Sun, 30 Mar 2008 13:26:45 +0200, Gilles Ganault
wrote:
>Thanks for the tip. This code works:
It works, but according to "ab", it's much slower to use APC than
hitting the SQLite file directly :-/
========
# ab -kc 10 -t 30 http://localhost/test_apc.php
Percentage of the requests served within a certain time (ms)
50% 105
66% 1054
75% 2066
80% 3089
90% 4347
95% 8136
98% 10140
99% 12148
100% 13115 (longest request)
========
# ab -kc 10 -t 30 http://192.168.0.2/test_direct.php
Percentage of the requests served within a certain time (ms)
50% 29
66% 30
75% 31
80% 32
90% 2038
95% 6072
98% 14049
99% 17056
100% 17199 (longest request)
========
Does APC start making sense with a lot more users, or is SQLite just
much faster than MySQL?
Re: Is PDO incompatible with caching mechanismes like APC?
am 30.03.2008 15:56:32 von Jerry Stuckle
Gilles Ganault wrote:
> On Sun, 30 Mar 2008 13:26:45 +0200, Gilles Ganault
> wrote:
>> Thanks for the tip. This code works:
>
> It works, but according to "ab", it's much slower to use APC than
> hitting the SQLite file directly :-/
>
> ========
> # ab -kc 10 -t 30 http://localhost/test_apc.php
>
> Percentage of the requests served within a certain time (ms)
> 50% 105
> 66% 1054
> 75% 2066
> 80% 3089
> 90% 4347
> 95% 8136
> 98% 10140
> 99% 12148
> 100% 13115 (longest request)
> ========
> # ab -kc 10 -t 30 http://192.168.0.2/test_direct.php
>
> Percentage of the requests served within a certain time (ms)
> 50% 29
> 66% 30
> 75% 31
> 80% 32
> 90% 2038
> 95% 6072
> 98% 14049
> 99% 17056
> 100% 17199 (longest request)
> ========
>
> Does APC start making sense with a lot more users, or is SQLite just
> much faster than MySQL?
>
This is off topic in this newsgroup.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================