MySQL Cache with PHP Apc

Now, I'm testing PHP APC for cache. It's easy to install if you can use pecl.

Install in Ubuntu 10.10 

Now, I’m using Ubuntu 10.10 on server. Don’t use apt-get install php-apc because apt version is too old. We should use from pecl and we can get latest version. apt version didn’t include apc_exists function.

I install like following

apt-get install php-pear
apt-get install php5-dev
apt-get install libpcre3-dev libpcre++-dev
pecl install apc

After installing , you need to add extension=apc.so in php.ini . And then restart apache ( $sudo /etc/init.d/apache2 restart ) . Check on your phpinfo(); and you will apc. 

MySQL Cache with APC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
$sql="select from prize";
if (apc_exists($sql)) {
print_r( apc_fetch($sql) );
}
else {
$result=mysql_query($sql);
while ($row = mysql_fetch_assoc($user_result)) {
$data[]=$row; // or you can make some process
}
//print the result
print_r($data);
apc_store($sql,$data,86400);//will expire in 86400 seconds
}
?>

You should delete cache after update or delete or insert.

you can delete cache like this

1
2
3
4
5
6
7
8
<?php
//after insert or delete or update
$sql="select from prize";
apc_delete($sql);
?>

After cache, it’s more faster than before. I like apc because it’s easy to use and easy to setup.
comments powered byDisqus