Thursday, June 6, 2013

Basic APC Tutorial

APC is very powerful caching system for PHP. Here are some basics. First run test_apc.php and if you in 5 seconds click on delete link the key "str" will be deleted from cache. If you click on delete link after 5 second the key "str" will expire from APC caching system.



File:test_apc.php
<?php
if (!apc_exists('str')){
    $str = "I am cached for 5 seconds at: ".date('H:i:s');
    apc_store('str', $str, 5);
    echo apc_fetch('str');
}
else{
    echo apc_fetch('str');
}
echo "<p><a href='test_apc_delete.php'>Delete</a> string form cache</p>";
?>

File:test_apc_delete.php
<?php
if (apc_exists('str')){
   if(apc_delete('str')) 
echo "Key `str` is deleted by <i>apc_delete()</i> function.";
} else {
   echo "Key `str` has exipred.";
}
?>
References are at APC offical page.

No comments:

Post a Comment