APC caching with PHP
Join the DZone community and get the full member experience.
Join For Freewe started saving into the database more and more, even small pieces of data. years passed, the size of sites grew, and people began to notice that the performance of sites were falling under the too frequent reading of data from the database. and we came up with a new optimization strategy – caching (where we store data in the cache files on the server). interesting, isn`t it? this looks like ‘forward, to the past’

but why did it happen? it’s simple – the speed of hard drives has increased, even outgrown the velocity of the databases. at the moment i am talking about mysql. other types of databases are even faster. but in any case, progress is not standing still. now people have learned to use the server memory for data storage. ram is much faster than hard disk, and the price of memory falls all the time.
today i have prepared an example on using apc with php.
live demo
download in package
now – download the source files and lets start coding !
step 1. php
i made this useful class for you. we will use this class to work with memory using an apc caching system.
classes/apc.caching.php
<? class cacheapc { var $ittl = 600; // time to live var $benabled = false; // apc enabled? // constructor function cacheapc() { $this->benabled = extension_loaded('apc'); } // get data from memory function getdata($skey) { $bres = false; $vdata = apc_fetch($skey, $bres); return ($bres) ? $vdata :null; } // save data to memory function setdata($skey, $vdata) { return apc_store($skey, $vdata, $this->ittl); } // delete data from memory function deldata($skey) { $bres = false; apc_fetch($skey, $bres); return ($bres) ? apc_delete($skey) : true; } } ?>
i prepared here several necessary functions which we will use: getdata, setdata and deldata. now, lets check the first example file:
index.php
<?php $adata = array( 'name' => 'table', 'color' => 'brown', 'size' => array( 'x' => 200, 'y' => 120, 'z' => 150, ), 'strength' => 10, ); require_once('classes/apc.caching.php'); $ocache = new cacheapc(); echo 'initial data: <pre>'; // lets see what we have print_r($adata); echo '</pre>'; if ($ocache->benabled) { // if apc enabled $ocache->setdata('my_object', $adata); // saving data to memory $ocache->setdata('our_class_object', $ocache); // saving object of our class into memory too echo 'now we saved all in memory, click <a href="ndex2.php">here</a> to check what we have in memory'; } else { echo 'seems apc not installed, please install it to perform tests'; } ?>
in this file you can see that i am saving 2 objects in memory: some predefined array and class object. now, lets check second example file:
index2.php
<?php require_once('classes/apc.caching.php'); $ocache = new cacheapc(); if ($ocache->benabled) { // if apc enabled $amemdata = $ocache->getdata('my_object'); // getting data from memory $amemdata2 = $ocache->getdata('our_class_object'); // getting data from memory about our class echo 'data from memory: <pre>'; // lets see what we have from memory print_r($amemdata); echo '</pre>'; echo 'data from memory of object of cacheapc class: <pre>'; print_r($amemdata2); echo '</pre>'; echo 'as you can see - all data read successfully, now lets remove data from memory and check results, click <a href="index3.php">here</a> to continue'; } else { echo 'seems apc not installed, please install it to perform tests'; } ?>
here we are only reading data from memory. and, as we see – all data is successfully read from the memory. now, lets check last example file:
index3.php
<?php require_once('classes/apc.caching.php'); $ocache = new cacheapc(); if ($ocache->benabled) { // if apc enabled $ocache->deldata('my_object'); // removing data from memory $ocache->deldata('our_class_object'); // removing data from memory $amemdata = $ocache->getdata('my_object'); // lets try to get data again $amemdata2 = $ocache->getdata('our_class_object'); echo 'data from memory: <pre>'; // lets see what we have from memory print_r($amemdata); echo '</pre>'; echo 'data from memory of object of cacheapc class: <pre>'; print_r($amemdata2); echo '</pre>'; echo 'as you can see - all data successfully removed. great !'; } else { echo 'seems apc not installed, please install it to perform tests'; } ?>
live demo
download in archive
conclusion
today, i told you how we can use the server’s memory with apc caching. i hope you share any thoughts on optimizing your website(s). for example you can cache some frequently-used data (information about users on your system, or some settings of the website etc.) good luck in your work!
source:
http://www.script-tutorials.com/how-to-use-apc-caching-with-php/
Opinions expressed by DZone contributors are their own.
Comments